<?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;

    public function dataTable($query)
    {
        return $this->modifier(datatables()->eloquent($query));
    }

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

    public function addButtons($datatables)
    {
        return $datatables->addColumn('action', $this->getHtmlButtons());
    }

    public function getHtmlButtons()
    {
        return self::getButtonEdit().self::getButtonDel();
    }

    public function getButtonEdit($field = 'id', $title = 'Modifier')
    {
        return view('components.form.buttons.edit', [
            'dataId' => '{{$'.$field.'}}',
            'class' => 'btn-sm mr-2',
            'title' => $title,
        ]);
    }

    public function getButtonShow($field = 'id', $title = 'Voir')
    {
        return view('components.form.buttons.show', [
            'dataId' => '{{$'.$field.'}}',
            'class' => 'btn-sm mr-2',
            'title' => $title,
        ]);
    }

    public function getButtonDel($field = 'id', $title = 'Effacer')
    {
        return view('components.form.buttons.delete', [
            'dataId' => '{{$'.$field.'}}',
            'class' => 'btn-sm mr-2',
            'title' => $title,
        ]);
    }

    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)
    {
        if (request()->has('filters.'.$field)) {
            return request()->input('filters.'.$field);
        }

        return request()->has($field) ? request()->input($field) : false;
    }

    public function buildQuery($model)
    {
        return $model->newQuery();
    }

    public function html()
    {
        return $this->buildHtml(strtolower($this->model_name).'-table');
    }

    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' => $this->url ?? '',
            ])
            ->dom($this->getDom())
            ->orderBy($this->sortedColumn, $this->sortedOrder)
            ->buttons($this->getButtons());
    }

    public function getButtons()
    {
        return [
            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()
    {
        return 'rt'.$this->getDatatablesFooterDefault();
    }

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

    public function getDatatablesHeaderDefault()
    {
        return "<'row dt-toolbar-header'<'col-lg-4'l><'col-lg-4'B><'col-lg-4 text-right add'f>>";
    }

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

    protected function getColumns()
    {
        return [
            $this->makeColumnButtons(),
        ];
    }

    protected function filename(): string
    {
        return self::buildFilename($this->model_name);
    }

    protected function buildFilename($name)
    {
        return $name.'_'.date('YmdHis');
    }
}
