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

class Discipline extends Admin_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('admin/discipline_model');
    }

    // Incident Types Management
    public function incident_types() {
        if (!$this->rbac->hasPrivilege('exam', 'can_view')) {
            access_denied();
        }
        $this->session->set_userdata('top_menu', 'Discipline');
        $this->session->set_userdata('sub_menu', 'discipline/incident_types');
        $data['title'] = 'Incident Types';
        $data['title_list'] = 'Incident Types List';
        
        $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
        if ($this->form_validation->run() == FALSE) {
            // Validation failed or first load
        } else {
            $data = array(
                'name' => $this->input->post('name'),
                'note' => $this->input->post('note'),
            );
            $this->discipline_model->add_incident_type($data);
            $this->session->set_flashdata('msg', '<div class="alert alert-success text-left">Incident type added successfully</div>');
            redirect('admin/discipline/incident_types');
        }
        
        $incident_types_result = $this->discipline_model->get_incident_types();
        $data['incident_types_list'] = $incident_types_result;
        $this->load->view('layout/header', $data);
        $this->load->view('admin/discipline/incident_types', $data);
        $this->load->view('layout/footer', $data);
    }

    // Incident Categories Management
    public function incident_categories() {
        if (!$this->rbac->hasPrivilege('exam_schedule', 'can_view')) {
            access_denied();
        }
        $this->session->set_userdata('top_menu', 'Discipline');
        $this->session->set_userdata('sub_menu', 'discipline/incident_categories');
        $data['title'] = 'Incident Categories';
        $data['title_list'] = 'Incident Categories List';
        
        $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
        if ($this->form_validation->run() == FALSE) {
            // Validation failed or first load
        } else {
            $data = array(
                'name' => $this->input->post('name'),
                'note' => $this->input->post('note'),
            );
            $this->discipline_model->add_incident_category($data);
            $this->session->set_flashdata('msg', '<div class="alert alert-success text-left">Incident category added successfully</div>');
            redirect('admin/discipline/incident_categories');
        }
        
        $incident_categories_result = $this->discipline_model->get_incident_categories();
        $data['incident_categories_list'] = $incident_categories_result;
        $this->load->view('layout/header', $data);
        $this->load->view('admin/discipline/incident_categories', $data);
        $this->load->view('layout/footer', $data);
    }

    // Edit Incident Type
    function edit_incident_type($id) {
        if (!$this->rbac->hasPrivilege('exam', 'can_edit')) {
            access_denied();
        }
        $data['title'] = 'Edit Incident Type';
        $data['id'] = $id;
        $incident_type = $this->discipline_model->get_incident_type($id);
        $data['incident_type'] = $incident_type;
        $data['title_list'] = 'Incident Types List';
        $incident_types_result = $this->discipline_model->get_incident_types();
        $data['incident_types_list'] = $incident_types_result;
        
        $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('layout/header', $data);
            $this->load->view('admin/discipline/incident_types_edit', $data);
            $this->load->view('layout/footer', $data);
        } else {
            $data = array(
                'id' => $id,
                'name' => $this->input->post('name'),
                'note' => $this->input->post('note'),
            );
            $this->discipline_model->add_incident_type($data);
            $this->session->set_flashdata('msg', '<div class="alert alert-success text-left">Incident type updated successfully</div>');
            redirect('admin/discipline/incident_types');
        }
    }

    // Edit Incident Category
    function edit_incident_category($id) {
        if (!$this->rbac->hasPrivilege('exam_schedule', 'can_edit')) {
            access_denied();
        }
        $data['title'] = 'Edit Incident Category';
        $data['id'] = $id;
        $incident_category = $this->discipline_model->get_incident_category($id);
        $data['incident_category'] = $incident_category;
        $data['title_list'] = 'Incident Categories List';
        $incident_categories_result = $this->discipline_model->get_incident_categories();
        $data['incident_categories_list'] = $incident_categories_result;
        
        $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('layout/header', $data);
            $this->load->view('admin/discipline/incident_categories_edit', $data);
            $this->load->view('layout/footer', $data);
        } else {
            $data = array(
                'id' => $id,
                'name' => $this->input->post('name'),
                'note' => $this->input->post('note'),
            );
            $this->discipline_model->add_incident_category($data);
            $this->session->set_flashdata('msg', '<div class="alert alert-success text-left">Incident category updated successfully</div>');
            redirect('admin/discipline/incident_categories');
        }
    }

    // Incident Reports Management
    public function incident_reports() {
        if (!$this->rbac->hasPrivilege('marks_register', 'can_view')) {
            access_denied();
        }
        $this->session->set_userdata('top_menu', 'Discipline');
        $this->session->set_userdata('sub_menu', 'discipline/incident_reports');
        $data['title'] = 'Incident Reports';
        $data['title_list'] = 'Incident Reports List';
        $data['settinglist'] = $this->setting_model->get();

        
        $class = $this->class_model->get('', $classteacher = 'yes');
        $data['classlist'] = $class;
        
        $this->form_validation->set_rules('class_id', 'Class', 'trim|required|xss_clean');
        $this->form_validation->set_rules('section_id', 'Section', 'trim|required|xss_clean');
        
        if ($this->form_validation->run() == FALSE) {
            // Show initial form
        } else {
            $class_id = $this->input->post('class_id');
            $section_id = $this->input->post('section_id');
            $data['class_post'] = $class_id;
            $data['section_post'] = $section_id;
            $resultlist = $this->student_model->searchByClassSection($class_id, $section_id);
            $data['resultlist'] = $resultlist;
        }
        
        // Get incident types and categories for dropdowns
        $data['incident_types'] = $this->discipline_model->get_incident_types();
        $data['incident_categories'] = $this->discipline_model->get_incident_categories();
        
        $this->load->view('layout/header', $data);
        $this->load->view('admin/discipline/incident_reports', $data);
        $this->load->view('layout/footer', $data);
    }

    public function save_incident_report() {
        if (!$this->rbac->hasPrivilege('marks_register', 'can_add')) {
            access_denied();
        }
        
        $this->form_validation->set_rules('student_id', 'Student', 'trim|required|xss_clean');
        $this->form_validation->set_rules('incident_type_id', 'Incident Type', 'trim|required|xss_clean');
        $this->form_validation->set_rules('incident_category_id', 'Incident Category', 'trim|required|xss_clean');
        $this->form_validation->set_rules('reported_to', 'Reported To', 'trim|required|xss_clean');
        $this->form_validation->set_rules('reported_by', 'Reported By', 'trim|required|xss_clean');
        $this->form_validation->set_rules('description', 'Description', 'trim|required|xss_clean');
        $this->form_validation->set_rules('incident_date', 'Incident Date', 'trim|required|xss_clean');
        
        if ($this->form_validation->run() == FALSE) {
            $errors = array(
                'student_id' => form_error('student_id'),
                'incident_type_id' => form_error('incident_type_id'),
                'incident_category_id' => form_error('incident_category_id'),
                'reported_to' => form_error('reported_to'),
                'reported_by' => form_error('reported_by'),
                'description' => form_error('description'),
                'incident_date' => form_error('incident_date')
            );
            echo json_encode(array('status' => 'fail', 'msg' => $errors));
        } else {
            $data = array(
                'student_id' => $this->input->post('student_id'),
                'class_id' => $this->input->post('class_id'),
                'section_id' => $this->input->post('section_id'),
                'incident_type_id' => $this->input->post('incident_type_id'),
                'incident_category_id' => $this->input->post('incident_category_id'),
                'reported_to' => $this->input->post('reported_to'),
                'reported_by' => $this->input->post('reported_by'),
                'description' => $this->input->post('description'),
                'action_taken' => $this->input->post('action_taken'),
                'outcome' => $this->input->post('outcome'),
                'incident_date' => date('Y-m-d', strtotime($this->input->post('incident_date')))
            );
            
            $this->discipline_model->add_incident_report($data);
            echo json_encode(array('status' => 'success', 'msg' => 'Incident report saved successfully'));
        }
    }

    // Updated incident_reports_list method
    public function incident_reports_list() {
        if (!$this->rbac->hasPrivilege('marks_register', 'can_view')) {
            access_denied();
        }
        $this->session->set_userdata('top_menu', 'Discipline');
        $this->session->set_userdata('sub_menu', 'discipline/incident_reports_list');
        $data['title'] = 'All Incident Reports';
        
        // Get filter parameters
        $category_filter = $this->input->get('category_filter');
        $type_filter = $this->input->get('type_filter');
        $learner_filter = $this->input->get('learner_filter');
        
        $data['category_filter'] = $category_filter;
        $data['type_filter'] = $type_filter;
        $data['learner_filter'] = $learner_filter;
        $data['settinglist'] = $this->setting_model->get();

        
        // Get all students for learner filter dropdown
        $data['all_students'] = $this->discipline_model->get_all_students();
        
        // Get incident types and categories for dropdowns
        $data['incident_types'] = $this->discipline_model->get_incident_types();
        $data['incident_categories'] = $this->discipline_model->get_incident_categories();
        
        // Only get reports if filters are applied
        if ($category_filter || $type_filter || $learner_filter) {
            $reports_result = $this->discipline_model->get_filtered_incident_reports($category_filter, $type_filter, $learner_filter);
            $data['reports_list'] = $reports_result;
        } else {
            $data['reports_list'] = array();
        }
        
        $this->load->view('layout/header', $data);
        $this->load->view('admin/discipline/incident_reports_list', $data);
        $this->load->view('layout/footer', $data);
    }

    public function create_incident() {
        if (!$this->rbac->hasPrivilege('marks_register', 'can_view')) {
            access_denied();
        }
        $this->session->set_userdata('top_menu', 'Discipline');
        $this->session->set_userdata('sub_menu', 'discipline/create_incident');
        $data['title'] = 'Create Incident Report';
        
        $class = $this->class_model->get('', $classteacher = 'yes');
        $data['classlist'] = $class;
        
        // Get incident types and categories for dropdowns
        $data['incident_types'] = $this->discipline_model->get_incident_types();
        $data['incident_categories'] = $this->discipline_model->get_incident_categories();
        
        // Get students with existing incident reports
        $data['students_with_incidents'] = $this->discipline_model->get_students_with_incidents();
        
        $this->form_validation->set_rules('class_id', 'Class', 'trim|required|xss_clean');
        $this->form_validation->set_rules('section_id', 'Section', 'trim|required|xss_clean');
        $this->form_validation->set_rules('student_id', 'Student', 'trim|required|xss_clean');
        $this->form_validation->set_rules('incident_type_id', 'Incident Type', 'trim|required|xss_clean');
        $this->form_validation->set_rules('incident_category_id', 'Incident Category', 'trim|required|xss_clean');
        $this->form_validation->set_rules('reported_to', 'Reported To', 'trim|required|xss_clean');
        $this->form_validation->set_rules('reported_by', 'Reported By', 'trim|required|xss_clean');
        $this->form_validation->set_rules('description', 'Description', 'trim|required|xss_clean');
        $this->form_validation->set_rules('incident_date', 'Incident Date', 'trim|required|xss_clean');
        
        if ($this->form_validation->run() == FALSE) {
            // Show form with validation errors
        } else {
            $data = array(
                'student_id' => $this->input->post('student_id'),
                'class_id' => $this->input->post('class_id'),
                'section_id' => $this->input->post('section_id'),
                'incident_type_id' => $this->input->post('incident_type_id'),
                'incident_category_id' => $this->input->post('incident_category_id'),
                'reported_to' => $this->input->post('reported_to'),
                'reported_by' => $this->input->post('reported_by'),
                'description' => $this->input->post('description'),
                'action_taken' => $this->input->post('action_taken'),
                'outcome' => $this->input->post('outcome'),
                'incident_date' => date('Y-m-d', strtotime($this->input->post('incident_date')))
            );
            
            $this->discipline_model->add_incident_report($data);
            $this->session->set_flashdata('msg', '<div class="alert alert-success text-left">Incident report created successfully</div>');
            redirect('admin/discipline/create_incident');
        }
        
        $this->load->view('layout/header', $data);
        $this->load->view('admin/discipline/create_incident', $data);
        $this->load->view('layout/footer', $data);
    }

    public function get_students_by_class_section() {
        $class_id = $this->input->post('class_id');
        $section_id = $this->input->post('section_id');
        
        if ($class_id && $section_id) {
            $students = $this->student_model->searchByClassSection($class_id, $section_id);
            echo json_encode(array('status' => 'success', 'students' => $students));
        } else {
            echo json_encode(array('status' => 'error', 'message' => 'Class and section required'));
        }
    }

    public function delete_incident_report($id) {
        if (!$this->rbac->hasPrivilege('marks_register', 'can_delete')) {
            access_denied();
        }
        $this->discipline_model->delete_incident_report($id);
        $this->session->set_flashdata('msg', '<div class="alert alert-success text-left">Incident report deleted successfully</div>');
        redirect('admin/discipline/incident_reports_list');
    }

    // Print incident reports
    public function print_incident_reports() {
        if (!$this->rbac->hasPrivilege('marks_register', 'can_view')) {
            access_denied();
        }
        
        $category_filter = $this->input->post('category_filter');
        $type_filter = $this->input->post('type_filter');
        $learner_filter = $this->input->post('learner_filter');
        $data['settinglist'] = $this->setting_model->get();
        
        $data['reports_list'] = $this->discipline_model->get_filtered_incident_reports($category_filter, $type_filter, $learner_filter);
        $data['category_filter'] = $category_filter;
        $data['type_filter'] = $type_filter;
        $data['learner_filter'] = $learner_filter;
        
        $this->load->view('admin/discipline/print_incident_reports', $data);
    }

    // Delete Incident Type
    function delete_incident_type($id) {
        if (!$this->rbac->hasPrivilege('exam', 'can_delete')) {
            access_denied();
        }
        $this->discipline_model->delete_incident_type($id);
        $this->session->set_flashdata('msg', '<div class="alert alert-success text-left">Incident type deleted successfully</div>');
        redirect('admin/discipline/incident_types');
    }

    // Delete Incident Category
    function delete_incident_category($id) {
        if (!$this->rbac->hasPrivilege('exam_schedule', 'can_delete')) {
            access_denied();
        }
        $this->discipline_model->delete_incident_category($id);
        $this->session->set_flashdata('msg', '<div class="alert alert-success text-left">Incident category deleted successfully</div>');
        redirect('admin/discipline/incident_categories');
    }
}
?>