<?php

namespace App\Datatables;

use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Services\DataTable;

class ParentDataTable extends DataTable
{
    public $autoWidth = false;

    public $colReorder = false;

    public $fixedColumns = false;

    public $fixedHeader = false;

    public $rowReorder = false;

    public $rowReorderSelector;          // ['selector' => 'tr']

    public $scrollCollapse = false;

    public $scrollX = false;

    public $sortedColumn = 0;

    public $sortedOrder = 'asc';

    public $stateSave = true;

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

    public function modifier($datatables)
    {
        return $this->addButtons($datatables->setRowId('{{$id}}'));
    }

    /**
     * Add buttons DataTable class.
     *
     * @param  mixed  $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function addButtons($datatables)
    {
        return $datatables->addColumn('action', $this->getHtmlButtons());
    }

    public function getHtmlButtons()
    {
        $buttons = '';
        $buttons .= self::getButtonEdit();
        $buttons .= self::getButtonDel();

        return $buttons;
        // return view('components.datatables.buttons.row_action');
    }

    public function getButtonEdit()
    {
        return '<button type="button" data-id="{{$id}}" class="btn btn-xs btn-primary btn-edit mr-2"><i class="fa fa-fw fa-pencil-alt"></i></button>';
    }

    public function getButtonShow()
    {
        return '<button type="button" data-id="{{$id}}" class="btn btn-xs btn-secondary btn-show mr-2"><i class="fa fa-fw fa-eye"></i></button>';
    }

    public function getButtonDel()
    {
        return '<button type="button" data-id="{{$id}}" class="btn btn-xs btn-danger btn-del"><i class="fa fa-fw fa-trash"></i></button>';
    }

    public function makeColumnButtons()
    {
        return Column::computed('action')
            ->title('')
            ->exportable(false)
            ->printable(false)
            ->searchable(false)
            ->width(74)
            ->addClass('text-center text-nowrap');
    }

    public static function isFilteredByField($field)
    {
        return (request()->has('filters.'.$field)) ? request()->input('filters.'.$field) : (request()->has($field) ? request()->input($field) : false);
    }

    /**
     * Get query source of dataTable.
     *
     * @param  \App\Family  $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function buildQuery($model)
    {
        return $model->newQuery();
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\DataTables\Html\Builder
     */
    public function html()
    {
        return $this->buildHtml(strtolower($this->model_name).'-table');
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\DataTables\Html\Builder
     */
    public function buildHtml($table_id = false, $selector = false)
    {
        $table_id = $table_id ? $table_id : strtolower($this->model_name).'-table';
        $selector = $selector ? $selector : '#'.$this->model_name.'-filters';

        return $this->builder()
            ->setTableId($table_id)
            ->parameters($this->getParameters())
            ->columns($this->getColumns())
            ->ajax([
                'data' => 'function(d) { d.filters = $("'.$selector.'").serializeJSON(); }',
                'url' => isset($this->url) ? $this->url : '',
            ])
            ->dom($this->getDom())
            ->orderBy($this->sortedColumn, $this->sortedOrder)
            ->buttons($this->getButtons());
    }

    public function getButtons()
    {
        return [
            // Button::make('export'),
            Button::make('print'),
            Button::make('colvis'),
            Button::make('columnsToggle'),
        ];
    }

    public function getParameters()
    {
        $data = [
            'autoWidth' => $this->autoWidth,
            'colReorder' => $this->colReorder,
            'fixedColumns' => $this->fixedColumns,
            'fixedHeader' => $this->fixedHeader,
            'pageLength' => 10,
            'searchDelay' => 500,
            'scrollX' => $this->scrollX,
            'scrollCollapse' => $this->scrollCollapse,
            'stateSave' => $this->stateSave,
        ];
        if ($this->rowReorder) {
            $data['rowReorder'] = ['selector' => $this->rowReorderSelector];
        }

        return $data;
    }

    public function getDom()
    {
        $dom = '';
        // $dom .= $this->getDatatablesHeaderDefault();
        $dom .= 'rt';
        $dom .= $this->getDatatablesFooterDefault();

        return $dom;
    }

    public function getDatatablesHeader()
    {
        return view('components.datatables.header');
    }

    public function getDatatablesHeaderDefault()
    {
        // return "<div class'row'><div class='col'></div></div>";
        /*

        $dom = 't<"row datatable-pager light"<"col-md-12"'
            . '<"datatable-more-export-buttons filter-buttons pull-left">'
            . '<"datatable-more-export-favorites-buttons filter-buttons pull-left">'
            . '<"datatable-more-export-basket-buttons filter-buttons pull-left">'
            . '<"datatable-download-buttons filter-buttons pull-left">'
            . '>>'
            . '<"dt-toolbar-footer"<"col"i><"col pull-right datatable-pager light nopadding-right"p>>';

        */

        $dom = "<'row dt-toolbar-header'<'col-lg-4'l><'col-lg-4'B><'col-lg-4 text-right add'f>>";

        return $dom;
        // return 't<"row datatable-pager light"<"col-md-12"<"datatable-more-export-buttons filter-buttons pull-left"><"datatable-more-export-favorites-buttons filter-buttons pull-left"><"datatable-more-export-basket-buttons filter-buttons pull-left"><"datatable-download-buttons filter-buttons pull-left">>><"dt-toolbar-footer"<"col-md-6"i><"col-md-6 pull-right datatable-pager light nopadding-right"p>>';
    }

    public function getDatatablesFooterDefault()
    {
        return "<'row pt-3 dt-toolbar-footer'<'col-md-6'i><'col-md-6'p>>";
    }

    /**
     * Get filename for export.
     */
    protected function filename(): string
    {
        return self::buildFilename($this->model_name);
    }

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