<?php

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

class Unifstocklevel extends Admin_Controller {

    function __construct() {
        parent::__construct();
        
        // Load required models
        $this->load->model('uniformpos_model');
        $this->load->model('uniformitem_model');
        $this->load->model('setting_model'); // Assuming this model is needed for the print report
        
        // Set session data for menu navigation
        $this->session->set_userdata('top_menu', 'Uniform Stock Level');
        $this->session->set_userdata('sub_menu', 'unifstocklevel/index');
    }

    public function index() {
        // Check user privilege
        if (!$this->rbac->hasPrivilege('class', 'can_view')) {
            access_denied();
        }

        // Get filter inputs (from POST)
        $filter_start_item = $this->input->post('filter_start_item');
        $filter_end_item = $this->input->post('filter_end_item');
        $filter_from_date = $this->input->post('filter_from_date');
        $filter_to_date = $this->input->post('filter_from_date');
        // Fetch item codes for the dropdown
        $data['itemCodes'] = $this->uniformpos_model->getItemCodesstocklevel();  

        // Retrieve filtered stock data based on the filters
        if (!empty($filter_start_item) && !empty($filter_end_item)) {
            $data['unif_stock_level'] = $this->uniformpos_model->getFilteredStocklevelData($filter_start_item, $filter_end_item, $filter_from_date, $filter_to_date);
        } else {
            $data['unif_stock_level'] = []; // Empty data for the table if no filters applied
        } 

        // Other data to be passed to the view
        $data['title_list'] = 'Uniform Stock level';
        $data['qnty_available'] = $this->uniformitem_model->getTotalUniformsAvailable();

        // Load the views
        $this->load->view('layout/header', $data);
        $this->load->view('uniformreport/unifstocklevel', $data);
        $this->load->view('layout/footer', $data);
    }

    public function printUniformReport() {
        // Check user privilege
        if (!$this->rbac->hasPrivilege('class', 'can_view')) {
            access_denied();
        }

        // Retrieve settings for printing
        $setting_result = $this->setting_model->get();
        $data['settinglist'] = $setting_result;
        $data['qnty_available'] = $this->uniformitem_model->getTotalUniformsAvailable();
           
        // Load the print view
        $this->load->view('print/printUnifstocklevel', $data);
    }
}

?>
