<?php

namespace App\Controllers;

use App\Models\OpdModel;

class Pegawai extends BaseController
{
    public function index()
    {
        $level = session()->get('level');
        $id_opd_session = session()->get('id_opd');
        $opdModel = new OpdModel();

        $data = [
            'title'    => 'Manajemen Pegawai',
            'level'    => $level,
            'opd_list' => ($level == 'kabupaten')
                ? $opdModel->findAll()
                : $opdModel->where('id_opd', $id_opd_session)->findAll()
        ];

        return view('pegawai/index', $data);
    }

    public function get_lokasi_by_opd($id_opd = null)
    {
        $db = \Config\Database::connect();
        if (!$id_opd || $id_opd == "null") return $this->response->setJSON([]);
        $data = $db->table('opd_lokasi')->where('id_opd', $id_opd)->get()->getResultArray();
        return $this->response->setJSON($data);
    }

    // --- UPDATE: MENDUKUNG MULTI-LOKASI ---
    public function update_lokasi()
    {
        $db = \Config\Database::connect();
        $id_pegawai = $this->request->getPost('id_pegawai');
        $id_lokasi_array = $this->request->getPost('id_lokasi'); // Ini berupa array

        // Gabungkan array [1,2] jadi string "1,2"
        $lokasi_string = (!empty($id_lokasi_array)) ? implode(',', $id_lokasi_array) : null;

        $db->table('pegawai')->where('id_pegawai', $id_pegawai)->update(['id_lokasi' => $lokasi_string]);
        return $this->response->setJSON(['status' => 'success', 'message' => 'Multi-Lokasi berhasil diperbarui!']);
    }

    public function ajax_list()
    {
        $db = \Config\Database::connect();
        $request = \Config\Services::request();
        $level = session()->get('level');
        $id_opd_session = session()->get('id_opd');
        $filter_opd = $request->getPost('filter_opd');

        $builder = $db->table('pegawai')
            ->select('pegawai.*, opd.nama_opd')
            ->join('opd', 'pegawai.id_opd = opd.id_opd');

        if ($level == 'opd') {
            $builder->where('pegawai.id_opd', $id_opd_session);
        } elseif (!empty($filter_opd)) {
            $builder->where('pegawai.id_opd', $filter_opd);
        }

        $searchValue = $request->getPost('search')['value'];
        if ($searchValue) {
            $builder->groupStart()->like('pegawai.nama_lengkap', $searchValue)->orLike('pegawai.nip', $searchValue)->groupEnd();
        }

        $totalFiltered = $builder->countAllResults(false);
        $start = $request->getPost('start');
        $length = $request->getPost('length');

        // LOGIKA ESELON ASLI BAPAK
        $builder->orderBy("CASE 
        WHEN TRIM(eselon) = 'II.a' THEN 1 WHEN TRIM(eselon) = 'II.b' THEN 2
        WHEN TRIM(eselon) = 'III.a' THEN 3 WHEN TRIM(eselon) = 'III.b' THEN 4
        WHEN TRIM(eselon) = 'IV.a' THEN 5 WHEN TRIM(eselon) = 'IV.b' THEN 6
        WHEN level_jabatan LIKE '%Fungsional%' THEN 7
        WHEN level_jabatan LIKE '%Staf%' OR TRIM(eselon) = 'Non Eselon' THEN 8
        WHEN TRIM(eselon) = '-' THEN 9 ELSE 10 END", "ASC", false);

        $builder->orderBy('nama_lengkap', 'ASC');
        $result = $builder->limit($length, $start)->get()->getResult();

        $data = [];
        $no = $start + 1;
        foreach ($result as $p) {
            $status_wajah = !empty($p->face_vector) ? '<span class="badge bg-success-subtle text-success px-3">AKTIF</span>' : '<span class="badge bg-danger-subtle text-danger px-3">BELUM ADA</span>';

            // --- TOMBOL HAPUS WAJAH ---
            $btnHapusWajah = '';
            if (!empty($p->face_vector) && $level == 'kabupaten') {
                $btnHapusWajah = '<a href="' . base_url('pegawai/hapus_wajah/' . $p->id_pegawai) . '" 
                                class="btn btn-sm btn-white border text-danger" 
                                onclick="return confirm(\'Hapus data biometrik wajah pegawai ini?\')" 
                                title="Hapus Wajah">
                                <i class="fas fa-user-slash"></i>
                              </a>';
            }

            // Logika Tampilan Lokasi (Cek apakah ada koma)
            $label_lokasi = "Kantor Induk";
            if (!empty($p->id_lokasi)) {
                $count = count(explode(',', $p->id_lokasi));
                $label_lokasi = ($count > 1) ? '<span class="badge bg-info">' . $count . ' Lokasi Terdaftar</span>' : 'Lokasi Khusus';
            }

            // PENYISIPAN $btnHapusWajah KE DALAM $opsi
            $opsi = '<div class="btn-group shadow-sm">' .
                $btnHapusWajah .
                '<button onclick="openEnroll(\'' . $p->id_pegawai . '\', \'' . $p->nama_lengkap . '\')" class="btn btn-sm btn-white border text-primary" title="Registrasi Wajah"><i class="fas fa-camera"></i></button>
                    <button onclick="openLokasi(\'' . $p->id_pegawai . '\', \'' . $p->id_opd . '\', \'' . $p->nama_lengkap . '\', \'' . $p->id_lokasi . '\')" class="btn btn-sm btn-white border text-info" title="Penugasan Lokasi"><i class="fas fa-map-marker-alt"></i></button>
                    <button onclick="openEdit(\'' . $p->id_pegawai . '\')" class="btn btn-sm btn-white border text-warning" title="Edit Data"><i class="fas fa-edit"></i></button>
                    <a href="' . base_url('pegawai/hapus/' . $p->id_pegawai) . '" class="btn btn-sm btn-white border text-danger" onclick="return confirm(\'Hapus?\')" title="Hapus Pegawai"><i class="fas fa-trash"></i></a>
                 </div>';

            $row = [];
            $row[] = '<span class="ps-3 small text-muted">' . $no++ . '.</span>';
            $row[] = '<div><div class="fw-bold small">' . $p->nama_lengkap . '</div><div class="text-muted small" style="font-size: 10px;">NIP. ' . $p->nip . '</div></div>';
            $row[] = '<div><div class="small fw-bold text-dark">' . $p->jabatan . '</div><div class="text-primary fw-bold" style="font-size: 9px;"><i class="fas fa-building me-1"></i>' . $label_lokasi . '</div></div>';
            $row[] = '<div class="text-center"><span class="badge bg-info-subtle text-info">' . ($p->status_asn ?? '-') . '</span></div>';
            $row[] = $status_wajah;
            $row[] = $opsi;
            $data[] = $row;
        }

        return $this->response->setJSON([
            "draw" => intval($request->getPost('draw')),
            "recordsTotal" => $db->table('pegawai')->countAllResults(),
            "recordsFiltered" => $totalFiltered,
            "data" => $data
        ]);
    }
    public function simpan()
    {
        $db = \Config\Database::connect();
        $data = [
            'id_opd'        => $this->request->getPost('id_opd'),
            'nip'           => $this->request->getPost('nip'),
            'nama_lengkap'  => $this->request->getPost('nama_lengkap'),
            'jabatan'       => $this->request->getPost('jabatan'),
            'level_jabatan' => $this->request->getPost('level_jabatan'),
            'pangkat'       => $this->request->getPost('pangkat'),
            'eselon'        => $this->request->getPost('eselon'),
            'status_asn'    => $this->request->getPost('status_asn'),
            'password'      => password_hash($this->request->getPost('password'), PASSWORD_DEFAULT),
        ];

        $db->table('pegawai')->insert($data);
        return redirect()->to('pegawai')->with('success', 'Pegawai baru berhasil ditambahkan.');
    }

    public function update()
    {
        $db = \Config\Database::connect();
        $id = $this->request->getPost('id_pegawai');

        $data = [
            'id_opd'        => $this->request->getPost('id_opd'),
            'nip'           => $this->request->getPost('nip'),
            'nama_lengkap'  => $this->request->getPost('nama_lengkap'),
            'jabatan'       => $this->request->getPost('jabatan'),
            'level_jabatan' => $this->request->getPost('level_jabatan'),
            'pangkat'       => $this->request->getPost('pangkat'),
            'eselon'        => $this->request->getPost('eselon'),
            'status_asn'    => $this->request->getPost('status_asn'),
        ];

        if ($this->request->getPost('password')) {
            $data['password'] = password_hash($this->request->getPost('password'), PASSWORD_DEFAULT);
        }

        $db->table('pegawai')->where('id_pegawai', $id)->update($data);
        return redirect()->to('pegawai')->with('success', 'Data pegawai berhasil diperbarui.');
    }

    public function edit_data($id)
    {
        $db = \Config\Database::connect();
        $data = $db->table('pegawai')->where('id_pegawai', $id)->get()->getRowArray();
        return $this->response->setJSON($data);
    }

    public function hapus($id)
    {
        $db = \Config\Database::connect();
        $db->table('pegawai')->where('id_pegawai', $id)->delete();
        return redirect()->to('pegawai')->with('success', 'Data pegawai berhasil dihapus.');
    }

    public function simpan_face_vector()
    {
        $db = \Config\Database::connect();
        $id = $this->request->getPost('id_pegawai');
        $vector = $this->request->getPost('face_vector');
        $db->table('pegawai')->where('id_pegawai', $id)->update(['face_vector' => $vector]);
        return $this->response->setJSON(['status' => 'success', 'msg' => 'Wajah Berhasil Disimpan!']);
    }

    public function ajax_generate_password()
    {
        if (session()->get('level') != 'kabupaten') {
            return $this->response->setJSON(['status' => 'error', 'message' => 'Akses ditolak!']);
        }

        $db = \Config\Database::connect();
        $limit = 50;
        $query_count = $db->table('pegawai')
            ->groupStart()
            ->where('CHAR_LENGTH(password) <', 60)
            ->orWhere('password', NULL)
            ->groupEnd();

        $total_sisa = $query_count->countAllResults(false);
        $pegawai = $query_count->limit($limit)->get()->getResultArray();

        if (empty($pegawai)) {
            return $this->response->setJSON(['status' => 'finish', 'message' => 'Semua password sudah aman.']);
        }

        $berhasil = 0;
        foreach ($pegawai as $p) {
            $hashPassword = password_hash($p['nip'], PASSWORD_DEFAULT);
            $db->table('pegawai')->where('id_pegawai', $p['id_pegawai'])->update(['password' => $hashPassword]);
            $berhasil++;
        }

        return $this->response->setJSON([
            'status' => 'process',
            'processed' => $berhasil,
            'remaining' => $total_sisa - $berhasil
        ]);
    }

    public function hapus_wajah($id)
    {
        // Gunakan koneksi database langsung seperti di ajax_list
        $db = \Config\Database::connect();

        // Set face_vector menjadi null
        $db->table('pegawai')
            ->where('id_pegawai', $id)
            ->update(['face_vector' => null]);

        return redirect()->to('/pegawai')->with('success', 'Data biometrik wajah berhasil direset. Silakan scan ulang.');
    }
}
