<?php

namespace App\DataTables;

use App\Models\Allergene;
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 AllergeneDataTable extends DataTable
{
    public function __construct(){
        $this->dataTableVariable = 'dataTable_allergene';
    }
    /**
     * Build the DataTable class.
     *
     * @param QueryBuilder<Allergene> $query Results from query() method.
     */
    public function dataTable(QueryBuilder $query): EloquentDataTable
    {
        return (new EloquentDataTable($query))
            ->addColumn('action', function($entity){
                return view('allergene.menu', ['entity' => $entity]);
            })
            ->addColumn('disponibile_display', function($entity){
                return view('_partials.available', ['available' => $entity->disponibile]);
            })
            // ->addColumn('icona_display', function($entity){
            //     return view('allergene._partials.icone', ['allergeni' => [$entity->nome]]);
            // })
            ->setRowId('id');
    }

    /**
     * Get the query source of dataTable.
     *
     * @return QueryBuilder<Allergene>
     */
    public function query(Allergene $model): QueryBuilder
    {
        return $model->newQuery()->orderBy('nome', 'asc');
    }

    /**
     * Optional method if you want to use the html builder.
     */
    public function html(): HtmlBuilder
    {
        $buttons = [];
        if(Auth::user()->can('create-allergene')){
            array_push($buttons, Button::make('create')
            ->editor('editor')
            ->formTitle('Crea nuovo allergene')
            ->className('btn btn-sm btn-primary mb-4')
            ->text('<i class="fas fa-plus"></i> Nuovo allergene'));
        }
        return $this->builder()
                    ->setTableId($this->dataTableVariable)
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->responsive(true)
                    ->orderBy(1)
                    ->selectStyleSingle()
                    ->buttons($buttons)
                    ->editor(
                        Editor::make()
                        ->fields([
                            Fields\Text::make('nome')->label('Nome'),
                            Fields\Text::make('icona')->label('Icona'),
                            Fields\Text::make('colore')->label('Colore'),
                            Fields\Boolean::make('disponibile')->label('Disponibile')->default(true),
                        ])
                    )
                    ->initComplete("function(settings, json){
                        initComplete_allergene();
                    }")
                    ->language(asset('assets/Italian.json'))
                    ->dom(count($buttons)==0?'rtip':'Bfrtip')
                    ->responsive()
                    ->selectStyleSingle();
    }

    /**
     * Get the dataTable columns definition.
     */
    public function getColumns(): array
    {
        return [
            Column::make('id')->title('ID')->width('10%')->visible(false),
            // Column::make('icona_display')->title('Icona')->width('10%'),
            Column::computed('disponibile_display')->title('Disponibile')->addClass('text-center')->width('10%')->responsivePriority(1), // icona in tabella
            Column::make('nome')->title('Nome')->responsivePriority(1),
            Column::make('descrizione')->title('Descrizione')->responsivePriority(2),
            Column::make('disponibile')->title('Disponibile')->visible(false)->addClass('text-center'), // valore grezzo per l'editor
            Column::computed('action')->title('')->width('10%')->addClass('text-center')->responsivePriority(1),
        ];
    }

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