<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Dashboard extends CI_Controller {

    public function __construct() {
        parent::__construct();
        if (!$this->session->userdata('logged_in')) {
            redirect('auth');
        }
        $this->load->model('Dashboard_model');
    }

    public function index() {
        $role = $this->session->userdata('role');

        if ($role === 'superadmin') {
            $data['stats'] = $this->Dashboard_model->get_superadmin_stats();
            $this->load->view('templates/header');
            $this->load->view('templates/sidebar');
            $this->load->view('dashboard/superadmin', $data);
            $this->load->view('templates/footer');

        } elseif ($role === 'admin_opd') {
            $unor_id = $this->session->userdata('unor_id');
            if (empty($unor_id)) {
                $this->session->set_flashdata('error', 'Data OPD tidak ditemukan.');
                redirect('auth/logout');
            }
            $data['stats'] = $this->Dashboard_model->get_admin_opd_stats($unor_id);
            $this->load->view('templates/header');
            $this->load->view('templates/sidebar');
            $this->load->view('dashboard/admin_opd', $data);
            $this->load->view('templates/footer');

        } else { // role == 'user'
            $pegawai_id = $this->session->userdata('pegawai_id');
            if (empty($pegawai_id)) {
                $this->session->set_flashdata('error', 'Sesi tidak valid.');
                redirect('auth/logout');
            }

            // === HANDLE AKSI ===
            if ($this->input->post('action') === 'claim_with_level') {
                $level = $this->input->post('level');
                if (in_array($level, ['top_leader', 'middle_leader', 'low_leader'])) {
                    if ($this->Dashboard_model->claim_as_pimpinan_with_level($pegawai_id, $level)) {
                        $this->session->set_flashdata('success', 'Berhasil klaim sebagai ' . ucfirst(str_replace('_', ' ', $level)));
                    } else {
                        $this->session->set_flashdata('error', 'Gagal menyimpan level kepemimpinan.');
                    }
                } else {
                    $this->session->set_flashdata('error', 'Level kepemimpinan tidak valid.');
                }
                redirect('dashboard');

            } elseif ($this->input->post('action') === 'unclaim') {
                $this->Dashboard_model->unclaim_pimpinan($pegawai_id);
                $this->session->set_flashdata('success', 'Klaim sebagai pimpinan dibatalkan.');
                redirect('dashboard');

            } elseif ($this->input->post('action') === 'tambah_bawahan') {
                $nip = $this->input->post('nip_bawahan');
                if ($nip && $this->Dashboard_model->tambah_bawahan($pegawai_id, $nip)) {
                    $this->session->set_flashdata('success', 'Bawahan berhasil ditambahkan.');
                } else {
                    $this->session->set_flashdata('error', 'NIP tidak valid atau sudah ada.');
                }
                redirect('dashboard');

            } elseif ($this->input->post('action') === 'hapus_bawahan') {
                $bawahan_id = $this->input->post('bawahan_id');
                if ($bawahan_id) {
                    $this->Dashboard_model->hapus_bawahan($pegawai_id, $bawahan_id);
                    $this->session->set_flashdata('success', 'Bawahan berhasil dihapus.');
                }
                redirect('dashboard');
            }

            // === PERSIAPKAN DATA UNTUK VIEW ===
            $data['profil'] = $this->db->get_where('pegawai', ['id_peg' => $pegawai_id])->row();
            $data['is_pimpinan'] = $this->Dashboard_model->is_pimpinan($pegawai_id);
            $data['level'] = $data['is_pimpinan'] ? $this->Dashboard_model->get_pimpinan_level($pegawai_id) : null;
            $data['daftar_bawahan'] = $data['is_pimpinan'] ? $this->Dashboard_model->get_bawahan_list($pegawai_id) : [];
            $data['bawahan_list'] = $data['is_pimpinan'] ? $this->Dashboard_model->get_bawahan_for_select($pegawai_id) : [];
            $data['role'] = 'user';

            $this->load->view('templates/header');
            $this->load->view('templates/sidebar');
            $this->load->view('dashboard/user_asn', $data);
            $this->load->view('templates/footer');
        }
    }
}