<?php

namespace App\DataTables;

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

class UserDataTable extends DataTable
{
    protected function getModelClass(): string
    {
        return User::class;
    }

    public function dataTable($query)
    {
        return datatables()
            ->eloquent($query)

            ->addColumn('name', fn(User $row) =>
                editCol($row?->name)
            )

            ->addColumn('email', fn(User $row) =>
                editCol($row?->email)
            )

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

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

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

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

            ->addIndexColumn();
    }

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

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

        return $this->builder()
            ->setTableId('users-table')
            ->columns($this->getColumns())
            ->minifiedAjax()
            ->ajax([
                'url'  => route('admin.users.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([
                    'excel' => [
                        'action' => dtExportAction(
                            $this->getModelClass(),
                            1,
                            $data['exportFilters'],
                            $data['exportColumns'],
                            $data['exportHeaders']
                        ),
                    ],
                ]),
            ] + dtLength(10));
    }

    protected function getColumns(): array
    {
        return [
            computedCol('checkbox', '<input type="checkbox" id="check-all">', 'text-center checkbox-col', false, false, 8),
            addCol('id', '#', true, true),
            addCol('name', __('dashboard.name'), true, true, true, true),
            addCol('email', __('dashboard.email'), 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 'User_' . date('YmdHis');
    }

    private function getExportData(): array
    {
        $exportFilters = [
            'is_active'        => '#status-filter',
            'created_at:range' => '#range-date',
        ];

        $exportColumns = [
            'id',
            'name',
            'email',
            'phone',
            'is_active',
            'created_at',
        ];

        $headers = [
            __('dashboard.id'),
            __('dashboard.name'),
            __('dashboard.email'),
            __('dashboard.phone'),
            __('dashboard.status'),
            __('dashboard.created_at'),
        ];

        return [
            'exportFilters' => $exportFilters,
            'exportColumns' => $exportColumns,
            'exportHeaders' => $headers
        ];
    }
}
