<?php

namespace App\DataTables;

use App\Medium;
use Illuminate\Support\Facades\Auth;
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 MediumDataTable extends DataTable
{
    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
        /*return datatables()
            ->eloquent($query)
            ->addColumn('action', '
                <a class="action-icon" href="/suchwort-bearbeiten/{{$id}}" title="Grobtechnik bearbeiten"><i class="fa fa-pencil" aria-hidden="true"></i></a>
                <a class="action-icon" href="javascript:confirmDeleteMedium({{$id}})" title="Grobtechnik löschen"><i class="fa fa-trash" aria-hidden="true"></i></a>
            ');*/
        return datatables()
            ->eloquent($query);
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Medium $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(Medium $model)
    {
        return $model->newQuery()
            ->leftJoin('t_medium_category', 't_medium.t_medium_category_id', '=', 't_medium_category.id')
            ->select('t_medium.id', '.t_medium.searchword_de', 't_medium.searchword_en', 't_medium.searchword_fr', 't_medium.t_medium_category_id', 't_medium_category.name');
    }

    /**
     * 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 == '') ? 5 : $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-medium')
            ->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-medium_filter');
                    $('#buttonLine').insertBefore('#data-table-medium_filter');
                    $('#filterContainer').show();
                    $('#buttonLine').show();
                }
            ");
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            /*Column::computed('action')
                ->exportable(false)
                ->printable(false)
                ->width(60)
                ->addClass('text-center'),*/
            Column::computed('action')
                ->title('')
                ->render('function(){
                    var actions = \'\';
                    var page = $("#data-table-medium").DataTable().page.info().page+1;
                    var length = $("#data-table-medium").DataTable().page.info().length;
                    var sortc = $("#data-table-medium").DataTable().order()[0][0];
                    var sortd = $("#data-table-medium").DataTable().order()[0][1];

                    actions += \' <a class="action-icon" href="/suchwort-bearbeiten/\'+full.id+\'/?page=\'+page+\'&len=\'+length+\'&sortc=\'+sortc+\'&sortd=\'+sortd+\'" title="Suchwort bearbeiten"><i class="fa fa-pencil" aria-hidden="true"></i></a> \';
                    actions += \' <a class="action-icon" href="javascript:confirmDeleteMedium(\'+full.id+\', \\\'\'+page+\'\\\', \\\'\'+length+\'\\\', \\\'\'+sortc+\'\\\', \\\'\'+sortd+\'\\\')" title="Suchwort 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('searchword_de', 't_medium.searchword_de')->title('Suchwort DE *'),
            Column::make('searchword_en', 't_medium.searchword_en')->title('Suchwort EN *'),
            Column::make('searchword_fr', 't_medium.searchword_fr')->title('Suchwort FR *'),
            //Column::make('t_medium_category_id', 't_medium_category_id')->title('Grobtechnik ID'),
            Column::make('name', 't_medium_category.name')->title('Grobtechnik'),
            Column::make('id', 't_medium.id')->title('ID')->width(60),
        ];
    }

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