<?php

namespace App\DataTables;

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

    /**
     * Get the query source of dataTable.
     *
     * @return QueryBuilder<Fornitore>
     */
    public function query(Fornitore $model): QueryBuilder
    {
        if($this->attivita_id){
            return $model->newQuery()->where('attivita_id', $this->attivita_id)->orderBy('nome', '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-fornitore')){
            array_push($buttons, Button::make('create')
                ->editor('editor')
                ->formTitle('Crea nuovo fornitore')
                ->className('btn btn-sm btn-primary mb-4')
                ->text('<i class="fas fa-plus"></i> Nuovo fornitore'));
        }
        return $this->builder()
                    ->setTableId($this->dataTableVariable)
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->orderBy(1)
                    ->responsive(true)
                    ->selectStyleSingle()
                    ->language(asset('assets/Italian.json'))
                    ->dom(count($buttons) == 0 ? 'rtip' : 'Bfrtip')
                    ->buttons($buttons)
                    ->searchPanes(true)
                    ->editor(
                        Editor::make()
                            ->fields([
                                Fields\Hidden::make('attivita_id')->default($this->attivita_id),
                                Fields\Text::make('nome')->label('Nome'),
                                Fields\Text::make('email')->label('Email'),
                                Fields\Text::make('telefono')->label('Telefono'),
                                Fields\Text::make('indirizzo')->label('Indirizzo'),
                                Fields\Text::make('citta')->label('Città'),
                                Fields\Text::make('provincia')->label('Provincia'),
                                Fields\Text::make('cap')->label('CAP'),
                                Fields\Text::make('paese')->label('Paese'),
                            ])
                    )
                    ->initComplete("function(settings, json){
                        initComplete_fornitore();
                    }");
    }

    /**
     * Get the dataTable columns definition.
     */
    public function getColumns(): array
    {
        return [
            Column::make('id')->visible(false),
            Column::make('nome')->responsivePriority(1),
            Column::make('email')->responsivePriority(2),
            Column::make('telefono')->responsivePriority(1),
            Column::make('indirizzo')->responsivePriority(2),
            Column::make('citta')->responsivePriority(2),
            Column::make('provincia')->responsivePriority(2),
            Column::make('cap')->responsivePriority(2),
            Column::make('paese'),
            Column::computed('action')->responsivePriority(1)
                ->title('')
                ->exportable(false)
                ->printable(false)
                ->width(60)
                ->addClass('text-center'),
        ];
    }

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