<?php

namespace App\DataTables;

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

class OpzioneFormDataTable extends DataTable
{
    public function __construct()
    {
        $this->dataTableVariable = 'dataTable_opzione_form';
    }

    /**
     * Build the DataTable class.
     *
     * @param QueryBuilder<OpzioneForm> $query Results from query() method.
     */
    public function dataTable(QueryBuilder $query): EloquentDataTable
    {
        return (new EloquentDataTable($query))
            ->addColumn('action', function($entity){
                return view('evento_form.opzioni.action', ['entity' => $entity]);
            })
            ->setRowId('id');
    }

    /**
     * Get the query source of dataTable.
     *
     * @return QueryBuilder<OpzioneForm>
     */
    public function query(OpzioneForm $model): QueryBuilder
    {
        return $model->newQuery()->where('evento_form_id', $this->evento_form_id);
    }

    /**
     * Optional method if you want to use the html builder.
     */
    public function html(): HtmlBuilder
    {

        $buttons = [];
        if(Auth::user()->can('create-opzione_form')){
            array_push($buttons, Button::make('create')
                ->editor('editor')
                ->className('btn btn-sm btn-success mb-4')
                ->formTitle('Nuova opzione form')
                ->formButtons([
                    Button::raw('Annulla')
                    ->className('btn btn-secondary ml-2')
                    ->actionClose(),
                    Button::raw('Salva')
                    ->className('btn btn-success ml-2')
                    ->actionHandler('create')
                ])
                ->text('<i class="fas fa-plus"></i> Nuova opzione form'));
        }
        return $this->builder()
                    ->setTableId($this->dataTableVariable)
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->ajax(route('opzione-form.index', ['evento_form_id' => $this->evento_form_id]))
                    ->editor(
                        Editor::make()
                            ->fields([
                                Fields\Hidden::make('id'),
                                Fields\Hidden::make('evento_form_id')->default($this->evento_form_id),
                                Fields\Text::make('label')->label('Label'),
                                Fields\Number::make('costo')->label('Costo'),
                                Fields\Number::make('posti_disponibili')->label('Posti disponibili'),
                            ])
                    )
                    ->initComplete("function(settings, json){
                        initComplete_opzione_form();
                    }")
                    ->responsive(true)
                    ->orderBy(1)
                    ->selectStyleSingle()
                    ->language(asset('assets/Italian.json'))
                    ->buttons($buttons)
                    ->dom(count($buttons) === 0 ? 'rtip' : 'Bfrtip');
    }

    /**
     * Get the dataTable columns definition.
     */
    public function getColumns(): array
    {
        return [
            Column::make('id')->visible(false),
            Column::make('label')->title('Nome'),
            Column::make('costo')->title('Costo (€)'),
            Column::make('posti_disponibili')->title('Posti disponibili (0 = illimitato)'),
            Column::computed('action')->addClass('text-center')->title('')
                  ->width(60)
                  ->addClass('text-center'),
        ];
    }

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