<?php

namespace App\DataTables;

use App\Enums\ParkingStatusEnum;
use App\Models\Parking;
use Yajra\DataTables\Services\DataTable;

class ParkingDataTable extends DataTable
{
    public function dataTable($query)
    {
        return datatables()
            ->eloquent($query)
            ->addColumn('checkbox', fn (Parking $row) =>
                '<input type="checkbox" class="row-checkbox" value="' . $row->id . '">'
            )
            ->addColumn('car_number', function (Parking $row) {
                $link = route('admin.parkings.show', $row->id);
                return '<a href="' . $link . '" class="fw-medium text-primary">' . e($row->car_number) . '</a>';
            })
            ->addColumn('member', fn (Parking $row) => e($row->member?->name ?? '-'))
            ->addColumn('make', fn (Parking $row) => e($row->make ?? '-'))
            ->addColumn('model', fn (Parking $row) => e($row->model ?? '-'))
            ->addColumn('qr_code', fn (Parking $row) => e($row->qr_code ?? '-'))
            ->addColumn('status', fn (Parking $row) => ParkingStatusEnum::badge($row->status))
            ->addColumn('action', fn (Parking $row) =>
                view('admin.parkings.partials.actions', ['row' => $row])->render()
            )
            ->rawColumns(['checkbox', 'car_number', 'status', 'action'])
            ->addIndexColumn();
    }

    public function query(Parking $model)
    {
        return $model->newQuery()->forIndex()->with('member')->applyFilters();
    }

    public function html()
    {
        $data = $this->getExportData();
        return $this->builder()
            ->setTableId('parkings-table')
            ->columns($this->getColumns())
            ->minifiedAjax()
            ->ajax([
                'url'  => route('admin.parkings.datatable'),
                'data' => dtFilters($data['exportFilters'])
            ])
            ->orderBy(1, 'desc')
            ->language([
                'url' => asset('/dashboard_assets/js/datatable/datatables/langs/' . app()->getLocale() . '.json'),
            ])
            ->scrollX(true)
            ->dom('Blfrtip')
            ->parameters([
                'select' => ['style' => 'multi', 'selector' => 'td:first-child'],
                'buttons' => dtButtons([]),
            ] + dtLength(10));
    }

    protected function getColumns(): array
    {
        return [
            computedCol('checkbox', '<input type="checkbox" id="check-all">', 'text-center checkbox-col', false, false, 8),
            addCol('car_number', __('Car Number'), true, true, true, true),
            addCol('member', __('Member'), true, false, true, true),
            addCol('make', __('Make'), true, false, true, true),
            addCol('model', __('Model'), true, false, true, true),
            addCol('qr_code', __('QR Code'), true, false, true, true),
            addCol('status', __('dashboard.status'), true, false, true, true),
            computedCol('action', __('dashboard.actions')),
        ];
    }

    protected function filename(): string
    {
        return 'Parking_' . date('YmdHis');
    }

    private function getExportData(): array
    {
        return [
            'exportFilters' => [
                'period'           => '.report',
                'status'           => '#status-filter',
                'orderBy'          => '#orderBy-filter',
                'created_at:range' => '#range-date',
            ],
        ];
    }
}
