<?php

namespace App\DataTables;

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

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

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

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

            ->addColumn('code', fn(Country $row) =>
                '<span class="badge bg-secondary-subtle text-secondary">' . $row->code . '</span>'
            )

            ->addColumn('phone_code', fn(Country $row) =>
                $row->phone_code ? '+' . $row->phone_code : '--'
            )

            ->addColumn('cities_count', fn(Country $row) =>
                '<span class="badge bg-info-subtle text-info">' . $row->cities_count . '</span>'
            )

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

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

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

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

            ->addIndexColumn();
    }

    public function query(Country $model)
    {
        return $model->newQuery()
            ->withCount('cities')
            ->applyFilters();
    }

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

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

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

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