<?php

namespace App\DataTables;

use App\Models\Testi;
use App\Models\Attivita;
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;
use Illuminate\Support\Facades\Session;

class TestiDataTable extends DataTable
{
    public function __construct(){
        $this->dataTableVariable = 'dataTable_testi';
    }
    /**
     * Build the DataTable class.
     *
     * @param QueryBuilder<Testi> $query Results from query() method.
     */
    public function dataTable(QueryBuilder $query): EloquentDataTable
    {
        return (new EloquentDataTable($query))
            ->addColumn('action', function($entity){
                return view('testi.menu', ['entity' => $entity]);
            })
            ->setRowId('id');
    }

    /**
     * Get the query source of dataTable.
     *
     * @return QueryBuilder<Testi>
     */
    public function query(Testi $model): QueryBuilder
    {
        if($this->attivita_id){
            return $model->newQuery()->where('attivita_id', $this->attivita_id)->orderBy('titolo', 'asc');
        }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-testi')){
            array_push($buttons, Button::make('create')
                ->editor('editor')
                ->formTitle('Crea nuovo testo')
                ->className('btn btn-sm btn-primary mb-4')
                ->text('<i class="fas fa-plus"></i> Nuovo testo'));
        }

        return $this->builder()
                    ->setTableId($this->dataTableVariable)
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->buttons($buttons)
                    ->responsive(true)
                    ->orderBy(1)
                    ->selectStyleSingle()
                    ->language(asset('assets/Italian.json'))
                    ->dom(count($buttons) == 0 ? 'rtip' : 'Bfrtip')
                    ->buttons($buttons)
                    ->editor(
                        Editor::make()
                            ->fields([
                                Fields\Select2::make('attivita_id')->label('Attività')->options(Attivita::all()->pluck('id', 'nome')),
                                Fields\Text::make('titolo')->label('Titolo'),
                                Fields\Text::make('descrizione')->label('Descrizione'),
                                Fields\Boolean::make('is_attivo')->label('Attivo')->default(true),
                                // Fields\Select::make('ambiente')->label('Ambiente')->options(),
                                // Fields\Select::make('tipo')->label('Tipo')->options(Testi::getTipi()->pluck('value', 'label')),
                                Fields\TextArea::make('testo')->label('Testo'),
                            ])
                    )
                    ->initComplete("function(settings, json){
                        initComplete_testi();
                    }");
    }

    /**
     * Get the dataTable columns definition.
     */
    public function getColumns(): array
    {
        return [
            Column::make('id')->title('ID')->width('10%')->visible(false),
            Column::make('titolo')->title('Titolo'),
            Column::make('descrizione')->title('Descrizione'),
            // Column::make('testo')->title('Testo'),
            Column::computed('action')->title('')->width('10%')->addClass('text-center'),
        ];
    }

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