<?php

namespace App\DataTables;

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

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

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

            ->addColumn('key', fn(ContractSetting $row) => e($row->key))

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

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

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

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

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

            ->addIndexColumn();
    }

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

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

        return $this->builder()
            ->setTableId('contract-settings-table')
            ->columns($this->getColumns())
            ->minifiedAjax()
            ->ajax([
                'url'  => route('admin.contract-settings.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('key', __('dashboard.key'), true, true, true, true),
            addCol('title', __('dashboard.title'), 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 'ContractSetting_' . 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,
        ];
    }
}
