<?php

namespace App\DataTables;

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

class ListinoPrezziDataTable extends DataTable
{
    public function __construct()
    {
        $this->dataTableVariable = 'dataTable_listino_prezzi';
    }
    /**
     * Build the DataTable class.
     *
     * @param QueryBuilder<ListinoPrezzi> $query Results from query() method.
     */
    public function dataTable(QueryBuilder $query): EloquentDataTable
    {
        return (new EloquentDataTable($query))
            ->addColumn('action', function($entity){
                return view('listino_prezzi.menu');
            })
            ->addColumn('tipo_dispositivo_display', function($entity){
                return view('listino_prezzi._partials.badge' , ['tipo_dispositivo' => $entity->tipo_dispositivo]);
            })
            ->addColumn('tipo_prezzo_display', function($entity){
                return view('listino_prezzi._partials.badge' , ['tipo_prezzo' => $entity->tipo_prezzo]);
            })
            ->addColumn('prezzo_display', function($entity){
                return '€ '.number_format($entity->prezzo, 2, ',', '.');
            })
            ->setRowId('id');
    }

    /**
     * Get the query source of dataTable.
     *
     * @return QueryBuilder<ListinoPrezzi>
     */
    public function query(ListinoPrezzi $model): QueryBuilder
    {
        return $model->newQuery();
    }

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

    $buttons = [];
    if(Auth::user()->hasRole('superadmin')){
        array_push($buttons,
         Button::make('create')
         ->text('Crea voce')
         ->className('btn btn-primary')
         ->editor('editor')
         ->formTitle('Crea nuova voce del listino prezzo')
         ->formButtons([
            Button::raw('Annulla')
            ->className('btn btn-secondary ml-2')
            ->actionClose(),
            Button::raw('Salva')
            ->className('btn btn-success ml-2')
            ->actionHandler('create')
         ])

         );
    }

        return $this->builder()
                    ->setTableId($this->dataTableVariable)
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->language(asset('assets/Italian.json'))
                    ->orderBy(1)
                    ->dom('Bfrtip')
                    ->editor(Editor::make()
                    ->fields([
                        Fields\Text::make('label')->label('Label'),
                        Fields\Text::make('descrizione')->label('Descrizione'),
                        Fields\Select2::make('tipo_dispositivo')->label('Tipo Dispositivo')->options(\App\Models\Dispositivo::getTipoDispositivo()->pluck('value','label')),
                        Fields\Number::make('prezzo')->label('Prezzo'),
                        Fields\Select2::make('tipo_prezzo')->label('Tipo Prezzo')->options(ListinoPrezzi::getTipiPrezzo()->pluck('value','label')),
                        Fields\Text::make('note')->label('Note'),
                    ]))
                    ->selectStyleSingle()
                    ->responsive(true)
                    ->initComplete('function(){
                        initComplete_listino_prezzi();
                    }')
                    ->buttons($buttons);
    }

    /**
     * Get the dataTable columns definition.
     */
    public function getColumns(): array
    {
        return [
            Column::make('id'),
            Column::make('label')->title('Nome'),
            Column::make('descrizione'),
            Column::make('tipo_dispositivo')->data('tipo_dispositivo_display'),
            Column::make('prezzo')->data('prezzo_display'),
            Column::make('tipo_prezzo')->data('tipo_prezzo_display'),
            Column::make('note'),
            Column::computed('action')
                  ->exportable(false)
                  ->printable(false)
                  ->width(60)
                  ->addClass('text-center'),
        ];
    }

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