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

class Despatch_o_ctrl extends user_index {
 
	
	public function __construct(){ 
		parent::__construct();
		$data = array(); 
		$this->load->model('admin_model');	
		$this->load->model('loading_model');	
	}
	
	/*----------------- load loading vehicle  entry list page -----------------*/
	public function despatch_selection_window_list(){	
		if(($this->user['panel_type']=='2' && in_array("2", $this->u_roles)) || ($this->user['panel_type']=='3' && in_array("3", $this->u_roles))){		
			$data['sdata'] 				= array();
			$data['subview'] 			= 'despatch/despatch_selection_window_list';
			$data['active_cls'] 		= 'loadaa_cls_d';		
			$data['sdata']['lul_list'] 	= $this->loading_model->get_loading_list('Loading','','','1','','0','','2');
			// echo $this->db->last_query()	;return;	
			$this->load->view('common/sidepanel',$data);
		}else{
			redirect('login_ctrl/logout');
		}
	}
	

	/*----------------- load delevery not / gate pass page ----------------*/
	public function delevery_not_gate_pass($lul_id){	
		if(($this->user['panel_type']=='2' && in_array("2", $this->u_roles)) || ($this->user['panel_type']=='3' && in_array("3", $this->u_roles))){				
			$data['sdata'] 						= array();
			$data['subview'] 					= 'despatch/delevery_not_gate_pass';
			$data['active_cls'] 				= 'loadaa_cls_d';				
			$data['sdata']['lul_data'] 			= $this->loading_model->get_loading_list('Loading',$lul_id);
			$data['sdata']['luml_data'] 		= $this->loading_model->get_loading_or_unloading_meterial_list($lul_id);	
			$data['sdata']['ttl_data'] 			= $this->admin_model->get_result_array('token_tb','tt_id','1');
			
			// echo $this->db->last_query();return;
			$this->load->view('common/sidepanel',$data);
		}else{
			redirect('login_ctrl/logout');
		}
	}
	
	
	/*----------------- submit delevery not / gate pass -----------------*/
	public function submit_delevery_not_gate_pass($lul_id= false){		
		if(($this->user['panel_type']=='2' && in_array("2", $this->u_roles)) || ($this->user['panel_type']=='3' && in_array("3", $this->u_roles))){	
			
			$this->form_validation->set_rules('lul_time_out', 'Time OUT', 'trim|required');
			
			if ($this->form_validation->run() === FALSE) {
				$e_msg = ''; 				   
				$e_msg .= form_error('lul_time_out');  				  
				$this->session->set_flashdata('tos_error', $e_msg);
				redirect('despatch_o_ctrl/delevery_not_gate_pass/'.$lul_id);				
				
			}	
					
			$data['lul_details']['lul_d_no'] 				= $this->input->post('lul_d_no');
			$data['lul_details']['lul_time_out'] 			= $this->input->post('lul_time_out');
			$data['lul_details']['lul_d_o_id'] 				= $this->user['i_user_id'];
			$data['lul_details']['lul_d_o_date_time'] 		= date("Y-m-d h:i a");
			$data['lul_details']['lul_exit_date'] 			= date('Y-m-d');
			
			
			
			$edit_lul = $this->admin_model->update_data('loading_unloading_list','lul_id',$lul_id,$data['lul_details']);
			$tt_data['tt_count'] = $data['lul_details']['lul_d_no']+1;
			$edit_tt = $this->admin_model->update_data('token_tb','tt_id','1',$tt_data);

				
			if($edit_lul!=false){
				$this->session->set_flashdata('tos_success', 'despatched.');
				redirect('print_ctrl/print_despatch_view_loading/'.$lul_id);
			}else {
				$this->session->set_flashdata('tos_error', 'Try again !...');
				redirect('despatch_o_ctrl/delevery_not_gate_pass');
			}	
		}else{
			redirect('login_ctrl/logout');
		}			
			
	}

	
	

	
}
