<?php

namespace App\DataTables;

use App\Models\RigaOrdine;
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;

class RigaOrdineDataTable extends DataTable
{
    public function __construct()
    {
        $this->dataTableVariable = 'dataTable_rigaordine';
        // $this->ordine_id = $ordine_id;
    }
    /**
     * Build the DataTable class.
     *
     * @param QueryBuilder<RigaOrdine> $query Results from query() method.
     */
    public function dataTable(QueryBuilder $query): EloquentDataTable
    {
        return (new EloquentDataTable($query))
            // ->addColumn('action', 'rigaordine.action')
            ->addColumn('piatto', function ($query) {
                return $query->piatto->nome;
            })
            ->addColumn('quantita', function ($query) {
                return $query->quantita;
            })
            ->addColumn('prezzo', function ($query) {
                return $query->prezzo;
            })
            ->addColumn('note', function ($query) {
                return $query->note;
            })
            ->addColumn('aumenta', function ($query) {
                return view('punto_vendita.cassa._partials.carrello.cmd_carrello', ['cmd' => 'aumenta', 'piatto_id' => $query->piatto_id , 'riga_ordine_id' => $query->id]);
            })
            ->addColumn('diminuisci', function ($query) {
                return view('punto_vendita.cassa._partials.carrello.cmd_carrello', ['cmd' => 'diminuisci', 'piatto_id' => $query->piatto_id , 'riga_ordine_id' => $query->id]);
            })
            ->addColumn('elimina', function ($query) {
                return view('punto_vendita.cassa._partials.carrello.cmd_carrello', ['cmd' => 'elimina', 'piatto_id' => $query->piatto_id , 'riga_ordine_id' => $query->id]);
            })
            ->addColumn('prezzo_display', function ($query) {
                return '€ ' . number_format($query->prezzo, 2, ',', '.');
            })
            ->addColumn('quantita_display', function ($query) {
                return view('punto_vendita.cassa._partials.carrello.cmd_carrello', ['cmd' => 'quantita', 'piatto_id' => $query->piatto_id , 'riga_ordine_id' => $query->id]);
            })
            ->addColumn('piatto_display', function ($query) {
                return view('punto_vendita.cassa._partials.carrello.cmd_carrello', ['cmd' => 'piatto', 'piatto_id' => $query->piatto_id , 'riga_ordine_id' => $query->id]);
            })
            ->addColumn('menu_display', function ($query) {
                return view('punto_vendita.cassa._partials.carrello.cmd_carrello', ['cmd' => 'menu', 'piatto_id' => $query->piatto_id , 'riga_ordine_id' => $query->id]);
            })
            ->addColumn('note_display', function ($query) {
                return view('punto_vendita.cassa._partials.carrello.cmd_carrello', ['cmd' => 'note', 'piatto_id' => $query->piatto_id , 'riga_ordine_id' => $query->id]);
            })
            ->addColumn('riga_ordine', function ($query) {
                return view('punto_vendita.cassa._partials.carrello.piatto_ordinato', ['riga_ordine' => $query]);
            })
            ->addColumn('vista_catalogo', function ($query) {
                return view('punto_vendita.cassa.viste.catalogo._partials.carrello-item', ['riga_ordine' => $query]);
            })
            ->addColumn('colonna_action', function ($query) {
                return view('punto_vendita.cassa._partials.carrello.cmd_carrello', ['cmd' => 'colonna_action', 'piatto_id' => $query->piatto_id , 'riga_ordine_id' => $query->id]);
            })->addColumn('carrello_item', function ($query) {
                return view('punto_vendita.cassa._partials.carrello.item', ['riga_ordine' => $query, 'piatto_id' => $query->piatto_id , 'riga_ordine_id' => $query->id]);
            })
            ->setRowId('id')
            ->rawColumns(['carrello_item']);
    }

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

    /**
     * Optional method if you want to use the html builder.
     */
    public function html(): HtmlBuilder
    {
        $buttons = [];
        $ajaxUrl = request()->fullUrl();
        
            array_push($buttons, Button::raw('Azzerare carrello')
                ->className('btn btn-sm btn-danger mb-4')
                ->style('margin-bottom: 10px;')
                ->action('function(){
                    azzeraCarrello();
                }'));
        
        return $this->builder()
                    ->setTableId($this->dataTableVariable)
                    ->columns($this->getColumns())
                    // URL esplicita: mantiene query string (es. punto_vendita_id)
                    // evitando redirect del controller show durante le richieste draw.
                    ->minifiedAjax($ajaxUrl)
                    ->language(asset('assets/Italian.json'))
                    // ->dom(count($buttons) == 0 ? 'rtip' : 'Brtip')
                    ->dom('rtip')
                    // ->buttons($buttons)
                    ->orderBy(0, 'asc')
                    ->paging(false)
                    ->selectStyleSingle()
                    ->responsive($this->vista === 'catalogo')
                    // ->editor(
                    //     Editor::make()
                    //         ->fields([
                    //             Fields\Text::make('quantita')->label('Quantità'),
                    //             Fields\Text::make('note')->label('Note'),
                    //         ])
                    // )
                    ->initComplete("function(settings, json){
                        initComplete_rigaordine();
                    }");
    }

    /**
     * Get the dataTable columns definition.
     */
    public function getColumns(): array
    {
        return [
            Column::make('id')->visible(false),
            Column::make('carrello_item')->title('')->searchable(true)->data('carrello_item')->addClass('text-start col-5 col-sm-5 col-md-5 col-lg-5 col-xl-5 col-xxl-5'),
        ];
 
        if($this->vista == 'catalogo'){
            return [
                Column::make('id')->visible(false),
                Column::make('vista_catalogo')->title('')->searchable(true)->data('vista_catalogo')->addClass('text-start col-5 col-sm-5 col-md-5 col-lg-5 col-xl-5 col-xxl-5'),
            ];
        }

        return [
            Column::make('id')->visible(false),
            // Column::computed('riga_ordine')->title('')->searchable(true),
            Column::make('piatto')->title('Piatto')->searchable(true)->data('piatto_display')->addClass('text-start col-5 col-sm-5 col-md-5 col-lg-5 col-xl-5 col-xxl-5'),
            // Column::make('diminuisci')->title('')->addClass('text-center')->width('10%'),
            Column::make('quantita')->title('Quantità')->width('10%')->attributes(['class' => 'text-center w-25 w-50 w-md-25 w-sm-50'])->addClass('text-center')->data('quantita_display'),
            // Column::make('aumenta')->title('')->addClass('text-center')->width('10%'),
            Column::make('prezzo')->title('Prezzo')->data('prezzo_display')->addClass('text-center'),
            // Column::make('note')->title('Note')->addClass('text-center')->width('10%')->visible(false),
            // Column::make('elimina')->title('')->addClass('text-center')->width('10%'),
            // Column::make('colonna_action')->title('')->addClass('text-center')->width('10%')->data('colonna_action'),
            Column::computed('menu')->title('')->addClass('text-center details-control')->width('10%')->data('menu_display'),
        ];
    }

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