<?php

namespace App\DataTables;

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

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

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

        return $this->builder()
                    ->setTableId($this->dataTableVariable)
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->orderBy(1)
                    ->responsive()
                    ->selectStyleSingle()
                    ->language(asset('assets/Italian.json'))
                    ->dom(count($buttons) == 0 ? 'rtip' : 'Bfrtip')
                    ->buttons($buttons)
                    ->initComplete("function(settings, json){
                        initComplete_bacheca();
                    }")
                    ->editor(
                        Editor::make()
                            ->fields([
                                Fields\Hidden::make('attivita_id')->label('Attività')
                                ->default($this->attivita_id),
                                Fields\Text::make('nome')->label('Nome'),
                                Fields\Text::make('descrizione')->label('Descrizione'),
                                Fields\Image::make('immagine')->label('Immagine'),
                                Fields\Boolean::make('is_attiva')->label('Attiva')->default(true),

                            ])
                    );
    }

    /**
     * Get the dataTable columns definition.
     */
    public function getColumns(): array
    {
        return [
            Column::make('id')->visible(false),
            Column::make('nome')->title('Nome'),
            Column::make('descrizione')->title('Descrizione'),
            Column::make('colore')->title('Colore'),
            Column::make('tipo')->title('Tipo'),
            Column::make('info')->title('Info'),
            Column::computed('action')
                  ->title('')
                  ->exportable(false)
                  ->printable(false)
                  ->width(60)
                  ->addClass('text-center'),
        ];
    }

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