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

class Pengaduan extends CI_Controller {

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

    public function index() {
        $data['title'] = 'Saran & Pengaduan';
        $role = $this->session->userdata('role');
        $user_id = $this->session->userdata('user_id');
        
        if(in_array($role, ['admin', 'rt'])) {
            $data['pengaduan'] = $this->Pengaduan_model->get_all();
            $data['is_admin'] = true;
            $data['count_unread'] = $this->Pengaduan_model->count_unread();
        } else {
            $data['pengaduan'] = $this->Pengaduan_model->get_all(null, $user_id);
            $data['is_admin'] = false;
        }
        
        $this->load->view('templates/header', $data);
        $this->load->view('templates/sidebar', $data);
        $this->load->view('pengaduan/index', $data);
        $this->load->view('templates/footer');
    }

    public function tambah() {
        $data['title'] = 'Kirim Saran/Pengaduan';

        $this->form_validation->set_rules('judul', 'Judul', 'required');
        $this->form_validation->set_rules('isi', 'Isi', 'required');

        if($this->form_validation->run() == FALSE) {
            $this->load->view('templates/header', $data);
            $this->load->view('templates/sidebar', $data);
            $this->load->view('pengaduan/tambah', $data);
            $this->load->view('templates/footer');
        } else {
            $config['upload_path'] = './uploads/pengaduan/';
            $config['allowed_types'] = 'jpg|jpeg|png|pdf';
            $config['max_size'] = 5120;
            
            if(!is_dir($config['upload_path'])) mkdir($config['upload_path'], 0777, true);
            
            $lampiran = '';
            if(!empty($_FILES['lampiran']['name'])) {
                $this->load->library('upload', $config);
                if($this->upload->do_upload('lampiran')) {
                    $lampiran = $this->upload->data('file_name');
                }
            }

            $data_insert = [
                'kode_tiket' => $this->Pengaduan_model->generate_kode_tiket(),
                'user_id' => $this->session->userdata('user_id'),
                'judul' => $this->input->post('judul'),
                'kategori' => $this->input->post('kategori'),
                'isi' => $this->input->post('isi'),
                'privasi' => $this->input->post('privasi'),
                'lampiran' => $lampiran
            ];

            $this->Pengaduan_model->insert($data_insert);
            $this->session->set_flashdata('success', 'Pengaduan berhasil dikirim dengan kode: '.$data_insert['kode_tiket']);
            redirect('pengaduan');
        }
    }

    public function detail($id) {
        $data['title'] = 'Detail Pengaduan';
        $data['item'] = $this->Pengaduan_model->get_by_id($id);
        $data['is_admin'] = in_array($this->session->userdata('role'), ['admin', 'rt']);
        
        if(!$data['item']) show_404();
        
        // Cek akses untuk pengaduan privat
        if($data['item']->privasi == 'privat' && !$data['is_admin'] && $data['item']->user_id != $this->session->userdata('user_id')) {
            $this->session->set_flashdata('error', 'Anda tidak memiliki akses');
            redirect('pengaduan');
        }
        
        $this->load->view('templates/header', $data);
        $this->load->view('templates/sidebar', $data);
        $this->load->view('pengaduan/detail', $data);
        $this->load->view('templates/footer');
    }

    public function balas($id) {
        $this->_check_admin();
        
        $this->form_validation->set_rules('balasan', 'Balasan', 'required');
        
        if($this->form_validation->run()) {
            $this->Pengaduan_model->balas($id, [
                'balasan' => $this->input->post('balasan'),
                'status' => $this->input->post('status'),
                'dibalas_oleh' => $this->session->userdata('user_id'),
                'tanggal_balasan' => date('Y-m-d H:i:s')
            ]);
            $this->session->set_flashdata('success', 'Balasan berhasil dikirim');
        }
        redirect('pengaduan/detail/'.$id);
    }

    private function _check_admin() {
        $role = $this->session->userdata('role');
        if(!in_array($role, ['admin', 'rt'])) {
            $this->session->set_flashdata('error', 'Akses ditolak');
            redirect('pengaduan');
        }
    }
}