<?php

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

class Unifstockmvmnt extends Admin_Controller {

    function __construct() {
        parent::__construct();
        
        // This should be inside the constructor
        $this->session->set_userdata('top_menu', 'Uniform Stock Movement');
        $this->session->set_userdata('sub_menu', 'unifstockmvmnt/index');
        $data['title_list'] = 'Uniform Stock Movement';
    }

    function index() {
        if (!$this->rbac->hasPrivilege('class', 'can_view')) {
            access_denied();
        }
        // Get filter inputs (from POST)
        $filter_from_date = $this->input->post('filter_from_date');
        $filter_to_date = $this->input->post('filter_to_date');
        $filter_start_item = $this->input->post('filter_start_item');
        $filter_end_item = $this->input->post('filter_end_item');
        
          // Reformat dates to 'YYYY-MM-DD' if they are not already in that format
        $filter_from_date = date('Y-m-d', strtotime($filter_from_date));
        $filter_to_date = date('Y-m-d', strtotime($filter_to_date));

        // Fetch item codes for the dropdown
        $data['itemCodes'] = $this->uniformpos_model->getItemCodesstockmvmnt();  

        // Retrieve filtered stock data based on the filters
        if (!empty($filter_from_date) && !empty($filter_to_date) && !empty($filter_start_item) && !empty($filter_end_item)) {
            $data['unif_stock_mvmnt'] = $this->uniformpos_model->getFilteredStockmvmntData($filter_from_date,$filter_to_date,$filter_start_item, $filter_end_item);
        } else {
            $data['unif_stock_mvmnt'] = []; // Empty data for the table if no filters applied
        } 
        // Make sure to pass the data to the view in index()
        $data['title_list'] = 'Uniform Stock Movement';
        $data['qnty_received'] = $this->uniformitem_model->getTotalUniformsReceived();
        $data['qnty_sold'] = $this->uniformitem_model->getTotalUniformsSold();
        $data['qnty_available'] = $this->uniformitem_model->getTotalUniformsAvailable();
        //$data['unif_stock_mvmnt']=$this->uniformpos_model->selectuniformposmvmnt();
        $this->load->view('layout/header', $data);
        $this->load->view('uniformreport/unifstockmvmnt', $data);
        $this->load->view('layout/footer', $data);
    }    
    
    function printUniformReport() {
        if (!$this->rbac->hasPrivilege('class', 'can_view')) {
            access_denied();
        }

        $setting_result = $this->setting_model->get();
        
        $data['settinglist'] = $setting_result;
        $data['qnty_received'] = $this->uniformitem_model->getTotalUniformsReceived();
        $data['qnty_sold'] = $this->uniformitem_model->getTotalUniformsSold();
        $data['qnty_available'] = $this->uniformitem_model->getTotalUniformsAvailable();
           
        $this->load->view('print/printUnifstockmvmnt', $data);
    }
}
?>