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

class Profil extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('Pegawai_model');
        $this->load->library('session');

        if (!$this->session->userdata('id_peg')) {
            redirect('auth/login'); // Sesuaikan dengan login Anda
        }
    }

    public function index() {
        $id_peg = $this->session->userdata('id_peg');
        $data['pegawai'] = $this->Pegawai_model->get_pegawai_lengkap($id_peg);

        if (!$data['pegawai']) {
            show_error('Data pegawai tidak ditemukan.');
        }

        if ($data['pegawai']->is_pimpinan) {
            $data['bawahan'] = $this->Pegawai_model->get_daftar_bawahan($id_peg);
        } else {
            $data['bawahan'] = [];
        }

        $this->load->view('template/header');
        $this->load->view('profil/index', $data);
        $this->load->view('template/footer');
    }

    public function klaim_pimpinan() {
        $id_peg = $this->session->userdata('id_peg');
        $result = $this->Pegawai_model->klaim_sebagai_pimpinan($id_peg);

        if ($result['status'] === 'success') {
            $this->session->set_flashdata('success', $result['message']);
        } else {
            $this->session->set_flashdata('error', $result['message']);
        }

        redirect('profil');
    }

    public function batalkan_klaim() {
        $id_peg = $this->session->userdata('id_peg');
        $success = $this->Pegawai_model->batalkan_klaim_pimpinan($id_peg);

        if ($success) {
            $this->session->set_flashdata('success', 'Klaim sebagai pimpinan berhasil dibatalkan.');
        } else {
            $this->session->set_flashdata('error', 'Gagal membatalkan klaim.');
        }

        redirect('profil');
    }
}