<?php

namespace Modules\Pengguna\Repositories;

use App\Models\User;
use Modules\Role\Entities\Role;
use Yajra\DataTables\DataTables;
use App\Repositories\BaseRepository;

class PenggunaRepository extends BaseRepository implements PenggunaRepositoryInterface
{
    protected $table = 'PenggunaTable';

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

    public function datatable()
    {
        $data = $this->mainModel->with('role')->orderBy('id', 'asc')->get();

        return DataTables::of($data)
            ->addIndexColumn()
            ->editColumn('id', fn ($row) => encryptString($row->id))
            ->editColumn('name', fn ($row) => $row->name)
            ->editColumn('role_id', fn ($row) => $row->role->name)
            ->addColumn('status', function ($row) {
                return $row->is_active == "B" ? '<span class="badge bg-danger"><i class="mdi mdi-close-circle-outline"></i> ' . __('lang.blocked') . '</span>' : '<span class="badge bg-success"><i class="mdi mdi-check-circle-outline"></i> ' . __('lang.active') . '</span>';
            })
            ->editColumn('last_login', fn ($row) => $row->last_login ? fullLongDate($row->last_login) : '-')
            ->addColumn('action', function ($row) {
                $btn = [
                    [
                        'class' => 'call-modal',
                        'attributes' => [
                            'data-url' => route('pengguna.edit', ['id' => encryptString($row->id),]),
                            'data-title' => __('lang.edit_data', ['data' => __('lang.users'), 'value' => $row->name,]),
                            'data-table' => $this->table,
                        ],
                        'label' => '<i class="bx bx-edit me-2"></i>' . __('lang.edit'),
                    ],
                    [
                        'class' => 'delete-action',
                        'attributes' => [
                            'data-url' => route('pengguna.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(['action', 'status'])
            ->make('true');
    }
}
