<?php

namespace App\DataTables;

use App\Models\Impostazioni;
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 ImpostazioniDataTable extends DataTable
{
    public function __construct(){
        $this->dataTableVariable = 'dataTable_impostazioni';
    }
    /**
     * Build the DataTable class.
     *
     * @param QueryBuilder<Impostazioni> $query Results from query() method.
     */
    public function dataTable(QueryBuilder $query): EloquentDataTable
    {
        return (new EloquentDataTable($query))
            ->addColumn('action', function($entity){
                return view('impostazioni.menu', ['entity' => $entity]);
            })
            ->setRowId('id');
    }

    /**
     * Get the query source of dataTable.
     *
     * @return QueryBuilder<Impostazioni>
     */
    public function query(Impostazioni $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()->hasRole('superadmin')){
            array_push($buttons, Button::make('create')
                ->editor('editor')
                ->className('btn btn-sm btn-primary mb-4')
                ->formTitle('Crea nuova impostazione')
                ->text('<i class="fas fa-plus"></i> Nuova impostazione'));
        }

        $editorFields = [
            Fields\Hidden::make('attivita_id')->default($this->attivita_id),
            Fields\Text::make('label')->label('Label'),
            Fields\Text::make('valore')->label('Valore'),
            Fields\Text::make('descrizione')->label('Descrizione'),
            Fields\Text::make('opzioni')->label('Opzioni'),
        ];
        
        if(Auth::user()->hasRole('superadmin')){
            $editorFields[] = Fields\Text::make('chiave')->label('Chiave');
            $editorFields[] = Fields\Select::make('tipo')->label('Tipo')->options(Impostazioni::getTipi());
        }

        return $this->builder()
                    ->setTableId($this->dataTableVariable)
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->orderBy(1)
                    ->selectStyleSingle()
                    ->buttons($buttons)
                    ->language(asset('assets/Italian.json'))
                    ->dom(count($buttons) == 0 ? 'rtip' : 'Bfrtip')
                    ->editor(
                        Editor::make()->fields($editorFields)
                    )
                    ->initComplete("function(settings, json){
                        initComplete_impostazioni();
                    }") ;
    }

    /**
     * Get the dataTable columns definition.
     */
    public function getColumns(): array
    {
        $columns = [
            Column::make('id')->visible(false),
        ];

        if(Auth::user()->hasRole('superadmin')){
            $columns[] = Column::make('chiave');
            $columns[] = Column::make('tipo')->title('Tipo');
        }

        $columns[] = Column::make('label');
        $columns[] = Column::make('valore')->title('Valore')->visible(false);
        $columns[] = Column::make('descrizione')->title('Descrizione');
        $columns[] = Column::make('opzioni')->title('Opzioni')->visible(false);
        $columns[] = Column::computed('action')
                  ->title('')
                  ->exportable(false)
                  ->printable(false)
                  ->width(60)
                  ->addClass('text-center')
                  ->responsivePriority(1);

        return $columns;
    }

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