<?php

namespace App\DataTables;

use App\Enums\MemberStatusEnum;
use App\Enums\RelationEnum;
use App\Models\Dependent;
use Yajra\DataTables\Services\DataTable;

class DependentDataTable extends DataTable
{
    public function dataTable($query)
    {
        return datatables()
            ->eloquent($query)
            ->addColumn('checkbox', fn (Dependent $row) =>
                '<input type="checkbox" class="row-checkbox" value="' . $row->id . '">'
            )
            ->addColumn('name', function (Dependent $row) {
                $link = route('admin.dependents.show', $row->id);
                return '<a href="' . $link . '" class="fw-medium text-primary">' . e($row->name) . '</a>';
            })
            ->addColumn('relation', fn (Dependent $row) => RelationEnum::label($row->relation))
            ->addColumn('member', fn (Dependent $row) => e($row->member?->name ?? '-'))
            ->addColumn('phone', fn (Dependent $row) => e($row->phone ?? '-'))
            ->addColumn('status', fn (Dependent $row) => MemberStatusEnum::badge($row->status))
            ->addColumn('created_at', fn (Dependent $row) => $row->created_at ? UpdateDate($row->created_at) : '-')
            ->addColumn('action', fn (Dependent $row) =>
                view('admin.dependents.partials.actions', ['row' => $row])->render()
            )
            ->rawColumns(['checkbox', 'name', 'status', 'action'])
            ->addIndexColumn();
    }

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

    public function html()
    {
        $data = $this->getExportData();
        return $this->builder()
            ->setTableId('dependents-table')
            ->columns($this->getColumns())
            ->minifiedAjax()
            ->ajax([
                'url'  => route('admin.dependents.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('name', __('dashboard.name'), true, true, true, true),
            addCol('relation', __('Relation'), true, false, true, true),
            addCol('member', __('Member'), true, false, true, true),
            addCol('phone', __('dashboard.phone'), true, true, true, true),
            addCol('status', __('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 'Dependent_' . date('YmdHis');
    }

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