<?php

namespace App\DataTables;

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

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

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

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

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

            ->addColumn('native_name', fn(Language $row) =>
                $row->native_name ?? '--'
            )

            ->addColumn('direction', fn(Language $row) =>
                $row->direction === 'rtl'
                    ? '<span class="badge bg-info">RTL</span>'
                    : '<span class="badge bg-secondary">LTR</span>'
            )

            ->addColumn('is_default', fn(Language $row) =>
                $row->is_default
                    ? '<span class="badge bg-warning">' . __('dashboard.default') . '</span>'
                    : ''
            )

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

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

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

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

            ->addIndexColumn();
    }

    public function query(Language $model)
    {
        return $model->newQuery()
            ->applyFilters();
    }

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

        return $this->builder()
            ->setTableId('languages-table')
            ->columns($this->getColumns())
            ->minifiedAjax()
            ->ajax([
                'url'  => route('admin.languages.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('native_name', __('dashboard.native_name'), true, false, true, true),
            addCol('direction', __('dashboard.direction'), true, false, true, true),
            addCol('is_default', __('dashboard.default'), 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 'Language_' . 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,
        ];
    }
}
