<?php

namespace App\Actions\Admin\Master;

use App\Actions\Shared\BaseAction;
use App\Models\jurusan;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Yajra\DataTables\DataTables;

class KomptKeahlianAction extends BaseAction
{
    public function __construct()
    {
        $this->modelClass = jurusan::class;
    }

    public function validate(array $data, $tipe = 'store', $pk_id = null)
    {
        $rules = [
            'nama_jurusan' => 'required|string|max:100',
        ];

        if ($tipe === 'store') {
            $rules['kode_jurusan'] = 'required|string|max:255|unique:jurusans,kode_jurusan';
        } else {
            $rules['kode_jurusan'] = 'required|string|max:255|unique:jurusans,kode_jurusan,' . $pk_id;
        }

        $validator = Validator::make($data, $rules, $this->validateMessages()); // ✅ gunakan pesan custom

        if ($validator->fails()) {
            throw new ValidationException($validator);
        }

        return $validator->validated();
    }

    public function validateMessages()
    {
        return [
            'kode_jurusan.required'   => 'Kode Kompetensi wajib diisi.',
            'nama_jurusan.required'   => 'Nama Kompetensi wajib diisi.',
            'kode_jurusan.unique' => 'Kode Kompetensi sudah digunakan. Silakan gunakan kode lain.',
        ];
    }

    // Override fungsi create dengan validasi
    public function create(array $data)
    {
        $validated = $this->validate($data);
        return parent::create($validated);
    }

    public function update(int $id, array $data)
    {
        $validated = $this->validate($data, 'update', $id);
        return parent::update($id, $validated);
    }

    // Fungsi untuk mengambil data dengan DataTables
    public function table()
    {
        $data = jurusan::select(['id', 'kode_jurusan', 'nama_jurusan', 'bidang_keahlian']);
        return DataTables::of($data)
            ->addIndexColumn()
            ->addColumn('action', function ($row) {
                $btnMore = '';
                $btnview = '';
                // $btnview .= '<a class="btn btn-icon" href="javascript:void(0);" data-permision="user-update" onclick="onDeleteInduka(this)" data-params="' . base64_encode(json_encode($row)) . '">
                //                 <i class="ti ti-trash ti-md"></i>
                //             </a>';

                $btnMore .= '<a class="dropdown-item" href="javascript:void(0);" data-permision="user-update" onclick="onEdit(this)" data-params="' . base64_encode(json_encode($row)) . '">Edit</a>';
                $btnMore .= '<div class="dropdown-divider"></div>';
                $btnMore .= '<a class="dropdown-item" href="javascript:void(0);" data-permision="user-update" onclick="onDelete(this)" data-params="' . base64_encode(json_encode($row)) . '">Delete</a>';

                $btnDropdown = $btnMore === '' ? '' : '
                        <div class="dropdown">
                            <a href="javascript:;" class="btn dropdown-toggle hide-arrow btn-icon p-0" data-bs-toggle="dropdown" aria-expanded="false"><i class="ti ti-dots-vertical ti-md"></i></a>
                            <div class="dropdown-menu dropdown-menu-end">
                                ' . $btnMore . '
                            </div>
                        </div>
                ';

                return '
                    <div class="d-flex align-items-center">
                        ' . $btnview . '
                        ' . $btnDropdown . '
                    </div>
                    ';
            })
            ->rawColumns(['action'])
            ->make(true);
    }
}
