<?php

namespace Modules\Permohonan\Repositories;

use App\Models\Permohonan;
use Illuminate\Support\Str;
use Yajra\DataTables\DataTables;
use App\Repositories\BaseRepository;
use Modules\Permohonan\Repositories\PermohonanRepositoryInterface;

class PermohonanRepository extends BaseRepository implements PermohonanRepositoryInterface
{
    protected $table = 'PermohonanTable';

    public function __construct(Permohonan $model)
    {
        $this->mainModel = $model;
    }

    public function datatable()
    {
        $data = $this->mainModel->orderBy('id', 'desc');

        return DataTables::of($data)
            ->addIndexColumn()
            ->editColumn('id', fn($row) => encryptString($row->id))
            ->addColumn('jenis_permohonan', fn($row) => ($row->jenis_form_layanan_id == '1') ? __('lang.permohonan_info') : __('lang.keberatan_info'))
            ->editColumn('ket_permohonan_info', fn($row) => $row->isi_permintaan_informasi)
            ->editColumn('status', fn($row) => $row->status_text)
            ->editColumn('jawaban', fn($row) => $row->jawaban)
            ->addColumn('action', function ($row) {
                $btn = [
                    [
                        'url' => route('permohonan.show', ['id' => encryptString($row->id),]),
                        'label' => '<i class="fa-regular fa-eye me-2"></i></i>' . __('lang.detail'),
                    ],
                    [
                        'url' => route('permohonan.edit', ['id' => encryptString($row->id)]),
                        'label' => '<i class="fa-regular fa-edit me-2"></i></i>' . __('lang.edit'),
                    ],
                    [
                        'class' => 'delete-action',
                        'attributes' => [
                            'data-url' => route('permohonan.destroy', encryptString($row->id)),
                            'data-title' => __('lang.delete_data', ['data' => $row->name,]),
                            'data-table' => $this->table,
                        ],
                        'label' => '<i class="bx bx-trash me-2"></i>' . __('lang.delete'),
                    ]
                ];

                return generateActionDropdown($btn);
            })
            ->rawColumns(['jenis_permohonan', 'action'])
            ->make('true');
    }

    public function datatableUser()
    {
        $data = $this->mainModel->where('user_id', auth()->user()->id)->orderBy('id', 'desc');

        return DataTables::of($data)
            ->addIndexColumn()
            ->editColumn('id', fn($row) => encryptString($row->id))
            ->addColumn('jenis_permohonan', fn($row) => ($row->jenis_form_layanan_id == '1')? __('lang.permohonan_info') : __('lang.keberatan_info') )
            ->editColumn('ket_permohonan_info', fn($row) => $row->isi_permintaan_informasi)
            ->editColumn('status', fn($row) => $row->status_text)
            ->editColumn('jawaban', fn($row) => $row->jawaban)
            ->addColumn('action', function ($row) {
                return '<a type="button" href="' . route('user.detilPermohonan', ['id' => encryptString($row->id)]) . '" class="mb-1 mt-1 me-1 btn btn-primary"><i class="fa-regular fa-eye"></i> '.__('lang.detail').'</a>';
            })
            ->rawColumns(['jenis_permohonan','action'])
            ->make('true');
    }
}
