<?php

namespace App\DataTables;

use App\GenderLookup;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Services\DataTable;
use Yajra\DataTables\Html\Editor\Fields;
use Yajra\DataTables\Html\Editor\Editor;

class GenderLookupDataTable extends DataTable
{
    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
        return datatables()
            ->eloquent($query);
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\GenderLookup $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(GenderLookup $model)
    {
        return $model->newQuery()
            ->select('id',
                'firstname',
                'country',
                'gender'
            );
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\DataTables\Html\Builder
     */
    public function html()
    {
        $page = ($this->pageParam == '') ? null : $this->pageParam;
        $len = ($this->lenParam == '') ? 10 : $this->lenParam;
        $sortc = ($this->sortcParam == '') ? 4 : $this->sortcParam;
        $sortd = ($this->sortdParam == '') ? 'asc' : $this->sortdParam;

        if ($this->pageParam != null) {

            $displayStart = ($page - 1) * $len;
        } else {

            $displayStart = 0;
        }

        return $this->builder()
            ->setTableId('data-table-gender-lookup')
            ->addTableClass('cell-border data-table display table table-data-width nowrap')
            ->pageLength($this->pageLength)
            ->lengthChange(false)
            ->displayStart($displayStart)
            ->lengthMenu([ 10, 25, 50, 100 ])
            ->columns($this->getColumns())
            ->minifiedAjax()
            ->parameters([
                'dom' => 'Blfrtip',
                'buttons' => ['pageLength'],
            ])
            ->orderBy($sortc, $sortd)
            ->language('/plugins/datatables/i18n/German.json')
            ->initComplete("
                function (settings, json) {
                    $('#filterContainer').insertAfter('#data-table-gender-lookup_filter');
                    $('#buttonLine').insertBefore('#data-table-gender-lookup_filter');
                    $('#filterContainer').show();
                    $('#buttonLine').show();
                }
            ");
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            Column::computed('action')
                ->title('')
                ->render('function(){
                    var actions = \'\';
                    var page = $("#data-table-gender-lookup").DataTable().page.info().page+1;
                    var length = $("#data-table-gender-lookup").DataTable().page.info().length;
                    var sortc = $("#data-table-gender-lookup").DataTable().order()[0][0];
                    var sortd = $("#data-table-gender-lookup").DataTable().order()[0][1];

                    actions += \' <a class="action-icon" href="/vorname-bearbeiten/\'+full.id+\'/?page=\'+page+\'&len=\'+length+\'&sortc=\'+sortc+\'&sortd=\'+sortd+\'"><i class="fa fa-pencil" aria-hidden="true"></i></a> \';
                    actions += \' <a class="action-icon" href="javascript:confirmDeleteGenderLookup(\'+full.id+\', \\\'\'+full.firstname+\'\\\', \\\'\'+page+\'\\\', \\\'\'+length+\'\\\', \\\'\'+sortc+\'\\\', \\\'\'+sortd+\'\\\')" title="Vorname löschen"><i class="fa fa-trash" aria-hidden="true"></i></a> \';

                    return actions;
                }')
                ->exportable(false)
                ->printable(false)
                ->width(60)
                ->addClass('text-center'),
            Column::make('firstname', 'firstname')->title('Vorname *'),
            Column::make('gender', 'gender')
                ->title('Geschlecht *')
                ->render('function(){

                    if(type === \'display\') {
                        var dropdown = \'\';
                        var genderLookupId = full.id;

                        dropdown += \'<select onchange="dynamicAssignGender(\'+ genderLookupId +\', this)" style="width: 140px;">\';
                        dropdown += \'<option value="">- Bitte wählen -</option>\';
                        dropdown += \'<option value="1" \'+ (full.gender == 1 || full.gender == 1 ? \'selected\' : \'\' ) +\'>weiblich</option>\';
                        dropdown += \'<option value="2" \'+ (full.gender == 2 || full.gender == 2 ? \'selected\' : \'\' ) +\'>männlich</option>\';
                        dropdown += \'<option value="3" \'+ (full.gender == 3 || full.gender == 3 ? \'selected\' : \'\' ) +\'>mehrdeutig</option>\';
                        dropdown += \'</select>\';
                    } else {
                        return data;
                    }

                    return dropdown;
                }')
                ->orderable(true),
            Column::make('country', 'country')->title('Land *'),
            Column::make('id', 'id')->title('ID')->width(60),
        ];
    }

    /**
     * Get filename for export.
     *
     * @return string
     */
    protected function filename()
    {
        return 'GenderLookup_' . date('YmdHis');
    }
}
