<?php

namespace App\DataTables;

use App\Models\City;
use Yajra\DataTables\Services\DataTable;

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

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

            ->addColumn('name', function(City $row) {
                $name = $row->getTranslation('name', app()->getLocale());
                $link = route('admin.cities.show', $row->id);
                return '<a href="' . $link . '" class="fw-medium text-primary">' . e($name) . '</a>';
            })

            ->addColumn('country', function(City $row) {
                if ($row->country) {
                    $link = route('admin.countries.show', $row->country->id);
                    return '<a href="' . $link . '" class="text-info">' . $row->country->getTranslation('name', app()->getLocale()) . '</a>';
                }
                return '--';
            })

            ->addColumn('is_active', fn(City $row) =>
                $row->is_active
                    ? '<span class="badge bg-success">' . __('dashboard.active') . '</span>'
                    : '<span class="badge bg-danger">' . __('dashboard.inactive') . '</span>'
            )

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

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

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

            ->addIndexColumn();
    }

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

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

        return $this->builder()
            ->setTableId('cities-table')
            ->columns($this->getColumns())
            ->minifiedAjax()
            ->ajax([
                'url'  => route('admin.cities.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('country', __('dashboard.country'), true, true, 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 'City_' . date('YmdHis');
    }

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

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