<?php

namespace App\DataTables;

use App\Models\PrintJob;
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 PrintJobDataTable extends DataTable
{
    public function __construct()
    {
        $this->dataTableVariable = 'dataTable_print_job';
    }
    /**
     * Build the DataTable class.
     *
     * @param QueryBuilder<PrintJob> $query Results from query() method.
     */
    public function dataTable(QueryBuilder $query): EloquentDataTable
    {
        return (new EloquentDataTable($query))
            ->addColumn('action', function($entity){
                return view('print_job.menu', ['entity' => $entity]);
            })
            ->addColumn('stato_display', function($entity){
                // return PrintJob::getStatoLabel($entity->stato);
                return view('prenotazione._partials.stato-prenotazione', ['stato' => $entity->stato]);
            })
            ->addColumn('tipo_display', function($entity){
                return PrintJob::getTipoLabel($entity->tipo);
            })
            ->addColumn('dispositivo_id_display', function($entity){
                return $entity->dispositivo->nome;
            })
            ->addColumn('ordine_id_display', function($entity){
                return $entity->ordine->riferimento??$entity->ordine->id;
            })
            ->addColumn('payload_display', function($entity){
                return $entity->payload;
            })
            ->addColumn('stampante_id_display', function($entity){
                $stampante = $entity->stampante;
                $endpoint = null;
                if($stampante !== null && $stampante->endpoint_id !== null){
                    $endpoint = \App\Models\Endpoint::find($stampante->endpoint_id)->label;
                };

                return ($stampante->nome ?? 'ND' ). ' (EP: ' . ($endpoint ?? 'ND') .')';
            })
            ->addColumn('created_at_display', function($entity){ return $entity->created_at->format('d/m/Y H:i:s'); })
            ->addColumn('updated_at_display', function($entity){ return $entity->updated_at->format('d/m/Y H:i:s'); })
            ->setRowId('id');
    }

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

    /**
     * Optional method if you want to use the html builder.
     */
    public function html(): HtmlBuilder
    {
        $buttons = [];

        return $this->builder()
                    ->setTableId($this->dataTableVariable)
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->language(asset('assets/Italian.json'))
                    ->dom(count($buttons) == 0 ? 'rtip' : 'Bfrtip')
                    ->buttons($buttons)
                    ->responsive(true)
                    ->orderBy(1)
                    ->selectStyleSingle();
                    }

    /**
     * Get the dataTable columns definition.
     */
    public function getColumns(): array
    {
        return [
            Column::make('id'),
            Column::make('stato_display'),
            Column::make('tipo_display'),
            Column::make('payload'),
            Column::make('dispositivo_id_display'),
            Column::make('ordine_id_display'),
            Column::make('stampante_id_display'),

            Column::make('created_at_display')->title('Creato il'),
            Column::make('updated_at_display')->title('Aggiornato il'),
            Column::computed('action')
                  ->exportable(false)
                  ->printable(false)
                  ->width(60)
                  ->addClass('text-center'),
        ];
    }

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