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

class Sickbay extends MY_Controller
{

    // public $data = array();


    function __construct()
    {
        parent::__construct();
        $this->load->model('Sickbay_Model', 'sickbay', true);
    }


    // public function index()
    // {
    //     $data['records'] = $this->sickbay->get_all_data();
    //     $this->layout->title($this->lang->line('manage_sickbay') . ' | ' . SMS);
    //     $this->load->layout->view('academic/sickbay/index', $data);
    // }main code is this

    public function index($school_id = null)
    {
        // check_permission(VIEW);
        $this->data['records'] = $this->sickbay->get_sickbay_list($school_id);
        if ($this->session->userdata('role_id') != SUPER_ADMIN) {
            $condition = array();
            $condition['status'] = 1;
            $condition['school_id'] = $this->session->userdata('school_id');
            $this->data['departments'] = $this->sickbay->get_list('departments', $condition, '', '', '', 'id', 'ASC');
        }
        $this->data['filter_school_id'] = $school_id;
        $this->data['schools'] = $this->schools;
        $this->data['list'] = TRUE;
        // $data['records'] = $this->sickbay->get_all_data();
        // $this->layout->title($this->lang->line('manage_sickbay') . ' | ' . SMS);
        
        $this->load->layout->title('Manage Sickbay' . ' | ' . SMS);
        $this->load->layout->view('academic/sickbay/index', $this->data);
    }
    // public function index($school_id = null)
    // {

    //     check_permission(VIEW);

    //     $this->data['departments'] = $this->department->get_sickbay_list($school_id);

    //     $this->data['filter_school_id'] = $school_id;
    //     $this->data['schools'] = $this->schools;

    //     $this->data['list'] = TRUE;
    //     $this->layout->title($this->lang->line('manage_department') . ' | ' . SMS);
    //     $this->layout->view('department/index', $this->data);
    // }

    public function add()
    {

        $data = array(
            'school_id' => $this->input->post('school_id'),
            'name' => $this->input->post('name'),

            'date' => $this->input->post('date'),
            'class' => $this->input->post('class_id'),
            'section' => $this->input->post('section_id'),
            'roll_no' => $this->input->post('roll_no'),
            'attendant' => $this->input->post('attendant'),
            'sickness' => $this->input->post('sickness'),
            'treatment' => $this->input->post('treatment'),
            'status' => $this->input->post('status')
        );

        $this->sickbay->insert('sickbay', $data);

        redirect('academic/sickbay/index');
    }
    
    
     public function edit($id = null) {

        // check_permission(EDIT);

        if(!is_numeric($id)){
            error($this->lang->line('unexpected_error'));
            redirect('teacher/department/index');
        }
        
        if ($_POST) {
            $this->_prepare_department_validation();
            if ($this->form_validation->run() === TRUE) {
                $data = $this->_get_posted_department_data();
                $updated = $this->sickbay->update('sickbay', $data, array('id' => $this->input->post('id')));

                if ($updated) {
                     create_log('Has been updated a sickbay : '.$data['title']);   
                    
                    success($this->lang->line('update_success'));
                    redirect('academic/sickbay/index');
                } else {
                    error($this->lang->line('update_failed'));
                    redirect('academic/sickbay/edit/' . $this->input->post('id'));
                }
            } else {
                $this->data['sickbay'] = $this->sickbay->get_single('sickbay', array('id' => $this->input->post('id')));
            }
        }

        if ($id) {
            $this->data['sickbay'] = $this->sickbay->get_single('sickbay', array('id' => $id));

            if (!$this->data['sickbay']) {
                redirect('academic/sickbay/index');
            }
        }

        $this->data['sickbay'] = $this->sickbay->get_sickbay_list(); 
        // $this->data['school_id'] = $this->data['sickbay']->school_id;
        $this->data['schools'] = $this->schools;
        
        $this->data['edit'] = TRUE;
        $this->layout->title($this->lang->line('edit') . ' | ' . SMS);
        $this->load->layout->view('academic/sickbay/index', $this->data);
    }
    
    /*****************Function get_single_sickbay**********************************
     * @type            : Function
     * @function name   : get_single_sickbay
     * @description     : "Load single sickbay information" from database                  
     *                    to the user interface   
     * @param           : null
     * @return          : null 
     * ********************************************************** */
    public function get_single_sickbay(){
        
       $sickbay_id = $this->input->post('id');   
       $this->data['sickbay'] = $this->department->get_single('sickbay', array('id' => $sickbay_id));
       echo $this->load->view('sickbay/get-single-sickbay', $this->data);
    }
    
    
            public function delete($id)
            {
                // Load the model
                // $this->load->model('academic/canteen_model');
        
                // Check if the user has permission to delete (you can modify this condition)
        
                // Call the delete method in the model
                if($this->sickbay->delete_sickbay_record($id)){
                    create_log('Has been deleted a sickbay : '.$sickbay->title);   
                      success($this->lang->line('delete_success'));
                }
                // $this->sickbay->delete_sickbay_record($id);
        
                // Redirect to a page after deletion (adjust the URL)
                redirect('academic/sickbay/index');
            }
    
    
    
    // public function delete($id = null) {

    //     // check_permission(VIEW);

    //     // if(!is_numeric($id)){
    //     //     error($this->lang->line('unexpected_error'));
    //     //     redirect('academic/sickbay/index');
    //     // }
        
    //     $sickbay = $this->sickbay->get_single('sickbay', array('id' => $id));
        
    //     if ($this->sickbay->delete('sickbay', array('id' => $id))) {
            
    //         create_log('Has been deleted a sickbay : '.$sickbay->title);   
    //         success($this->lang->line('delete_success'));
    //     } else {
    //         error($this->lang->line('delete_failed'));
    //     }
    //   redirect('academic/sickbay/index');
    // }
}
