<?php

namespace App\DataTables;

use App\Enums\StatusEnum;
use App\Models\Contract;
use Yajra\DataTables\Services\DataTable;

class ContractDataTable extends DataTable
{
    public function dataTable($query)
    {
        return datatables()
            ->eloquent($query)

            ->addColumn('checkbox', fn(Contract $row) =>
                '<input type="checkbox" class="row-checkbox" value="' . $row->id . '">'
            )

            ->addColumn('serial_number', function(Contract $row) {
                $link = route('admin.contracts.show', $row->id);
                return '<a href="' . $link . '" class="fw-medium text-primary">' . e($row->serial_number) . '</a>';
            })

            ->addColumn('name', fn(Contract $row) => e($row->name))

            ->addColumn('phone', fn(Contract $row) => e($row->phone))


            ->addColumn('subscription_type', function(Contract $row) {
                return $row->subscriptionType
                    ? $row->subscriptionType->getTranslation('name', app()->getLocale())
                    : '--';
            })

            ->addColumn('is_active', fn(Contract $row) =>
                StatusEnum::badge($row->is_active)
            )

            ->addColumn('created_at', fn(Contract $row) =>
                UpdateDate($row->created_at)
            )

            ->addColumn('action', fn(Contract $row) =>
                view('admin.contracts.partials.actions', ['row' => $row])->render()
            )

            ->rawColumns([
                'checkbox',
                'serial_number',
                'is_active',
                'action',
            ])

            ->addIndexColumn();
    }

    public function query(Contract $model)
    {
        return $model->newQuery()
            ->with('subscriptionType')
            ->applyFilters();
    }

    public function html()
    {
        $data = $this->getExportData();

        return $this->builder()
            ->setTableId('contracts-table')
            ->columns($this->getColumns())
            ->minifiedAjax()
            ->ajax([
                'url'  => route('admin.contracts.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('serial_number', __('dashboard.serial_number'), true, true, true, true),
            addCol('name', __('dashboard.name'), true, true, true, true),
            addCol('phone', __('dashboard.phone'), true, true, true, true),
            addCol('subscription_type', __('dashboard.subscription_type'), true, false, true, true),
            addCol('is_active', __('dashboard.status'), true, false, true, true),
            addCol('created_at', __('dashboard.created_at'), true, false, true, true),
            computedCol('action', __('dashboard.actions')),
        ];
    }

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

    private function getExportData(): array
    {
        $exportFilters = [
            'period'              => '.report',
            'is_active'           => 'input[name="is_active"]',
            'request_type'        => '#request-type-filter',
            'subscription_type_id' => '#subscription-type-filter',
            'orderBy'             => '#orderBy-filter',
            'created_at:range'    => '#range-date',
        ];

        return [
            'exportFilters' => $exportFilters,
        ];
    }
}
