<?php

namespace App\DataTables;

use App\Models\Tavolo;
use Illuminate\Database\Eloquent\Builder as QueryBuilder;
use Yajra\DataTables\EloquentDataTable;
use Yajra\DataTables\Html\Builder as HtmlBuilder;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Html\Editor\Editor;
use Yajra\DataTables\Html\Editor\Fields;
use Yajra\DataTables\Services\DataTable;
use Illuminate\Support\Facades\Auth;

class TavoloDataTable extends DataTable
{
    public function __construct(){
        $this->dataTableVariable = 'dataTable_tavolo';
    }
    /**
     * Build the DataTable class.
     *
     * @param QueryBuilder<Tavolo> $query Results from query() method.
     */
    public function dataTable(QueryBuilder $query): EloquentDataTable
    {
        return (new EloquentDataTable($query))
            ->addColumn('action', function($entity){
                return view('tavolo.menu', ['entity' => $entity]);
            })
            ->addColumn('is_attivo_display', function($entity){
                return view('_partials.available', ['available' => $entity->is_attivo]);
            })
            ->setRowId('id');
    }

    /**
     * Get the query source of dataTable.
     *
     * @return QueryBuilder<Tavolo>
     */
    public function query(Tavolo $model): QueryBuilder
    {
        if($this->attivita_id){
            return $model->newQuery()->where('attivita_id', $this->attivita_id);
        }else{
            return $model->newQuery()->whereRaw('1 = 0');
        }
    }

    /**
     * Optional method if you want to use the html builder.
     */
    public function html(): HtmlBuilder
    {
        $buttons = [];
        if(Auth::user()->can('create-tavolo')){
            array_push($buttons, Button::make('create')
                ->editor('editor')
                ->formTitle('Crea nuovo tavolo')
                ->className('btn btn-sm btn-primary mb-4')
                ->text('<i class="fas fa-plus"></i> Nuovo tavolo'));
        }

        return $this->builder()
                    ->setTableId($this->dataTableVariable)
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->language(asset('assets/Italian.json'))
                    ->dom(count($buttons) == 0 ? 'rtip' : 'Bfrtip')
                    ->buttons($buttons)
                    ->orderBy(1)
                    ->selectStyleSingle()
                    ->responsive(true)
                    ->editor(
                        Editor::make()
                            ->fields([
                                Fields\Hidden::make('attivita_id')->label('Attività')->default($this->attivita_id),
                                Fields\Text::make('label')->label('Label'),
                                Fields\Text::make('posti')->label('Posti'),
                                Fields\Text::make('ubicazione')->label('Ubicazione'),
                                // Fields\Text::make('info')->label('Info'),
                                Fields\Boolean::make('is_attivo')->label('Attivo')->default(true),
                            ])
                    )
                    ->initComplete("function(settings, json){
                        initComplete_tavolo();
                    }");
    }

    /**
     * Get the dataTable columns definition.
     */
    public function getColumns(): array
    {
        return [
            Column::make('is_attivo_display')->title(''),
            Column::make('label')->title('Nome')->responsivePriority(1),
            Column::make('posti')->title('Posti')->responsivePriority(1),
            Column::make('ubicazione')->title('Ubicazione')->responsivePriority(1),
            Column::computed('action')
                  ->title('')
                  ->exportable(false)
                  ->printable(false)
                  ->width(60)
                  ->addClass('text-center'),
            Column::make('id')->visible(false),
        ];
    }

    /**
     * Get the filename for export.
     */
    protected function filename(): string
    {
        return 'Tavolo_' . date('YmdHis');
    }
}
