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

class Authorizing_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 loading_autho_list(){
		if($this->user['panel_type']=='2' && in_array("2", $this->u_roles)){	
			$data['sdata'] 				= array();
			$data['subview'] 			= 'loading/loading_autho_list';
			$data['active_cls'] 		= 'dash';		
			$data['sdata']['lul_list'] 			= $this->loading_model->get_loading_list('Loading','','0');		
			$this->load->view('common/sidepanel',$data);
		}else{
			redirect('login_ctrl/logout');
		}
	}
	
 
	/*----------------- load add loading vehicle  entry details page ----------------*/
	public function approve_loading_d_by_auto($lul_id){	 
		$data['sdata'] 						= array();
		$data['subview'] 					= 'loading/approve_loading_d_by_auto';
		$data['active_cls'] 				= 'dash';	
		$data['sdata']['cl_list'] 			= $this->admin_model->get_result_array('customer_list');				
		$data['sdata']['tl_list'] 			= $this->admin_model->get_result_array('transporter_list');				
		$data['sdata']['com_list'] 			= $this->admin_model->get_result_array('commodity_list');		
		$data['sdata']['unl_list'] 			= $this->admin_model->get_result_array('units_list');		
		$data['sdata']['ml_list'] 			= $this->admin_model->get_meterial_list();		
		if($lul_id!=''){
			$data['sdata']['view_type'] 	= "Edit";
			$data['sdata']['lul_data'] = $this->loading_model->get_loading_list('Loading',$lul_id);	
		}else{
			$data['sdata']['view_type'] 	= "Add";
		}
		$this->load->view('common/sidepanel',$data);
	}
	
	
	/*----------------- add loading vehicle  entry  details and edit loading vehicle  entry  details -----------------*/
	public function insert_appvoved_loading_meterials($lul_id= false){		
		
		
		$data['lul_details'] = array();
		foreach($_POST as $post_key=>$post_rows){
			if($post_key!='luml_ml_id' && $post_key!='luml_unl_id' && $post_key!='luml_total_weight' && $post_key!='bal_bag'&& $post_key!='luml_no_of_bag'){
				$data['lul_details'][$post_key] 		= $this->input->post($post_key);
			}
		}		
		$data['lul_details']['lul_a_o_id'] 			= $this->user['i_user_id'];
		$data['lul_details']['lul_a_o_date_time'] 	= date("Y-m-d h:i a");
		
		
		$edit_lul = $this->admin_model->update_data('loading_unloading_list','lul_id',$lul_id,$data['lul_details']);

		$luml_ml_id 					= $this->input->post('luml_ml_id');
		$luml_unl_id 					= $this->input->post('luml_unl_id');
		$luml_total_weight 				= $this->input->post('luml_total_weight');
		$luml_no_of_bag 				= $this->input->post('luml_no_of_bag');
		$bal_bag 						= $this->input->post('bal_bag');

		foreach($luml_ml_id as $luml_key=>$luml_row){
			$data['luml_details']['luml_ml_id'] 		=  $luml_row;
			$data['luml_details']['luml_unl_id'] 		=  $luml_unl_id[$luml_key];
			$data['luml_details']['luml_total_weight'] 	=  $luml_total_weight[$luml_key];
			$data['luml_details']['luml_no_of_bag'] 	=  $luml_no_of_bag[$luml_key];
			$data['luml_details']['luml_lul_id'] 		=  $lul_id;
						
			$add_lul 									= $this->admin_model->insert_item('loading_unloading_meterial_list',$data['luml_details']);	

			// $data['ml_details']['ml_total_stock'] 		=  $bal_bag[$luml_key];
			

			// $edit_luml = $this->admin_model->update_data('meterial_list','ml_id',$luml_row,$data['ml_details']);
			
		}
			
		if($edit_lul!=false){
			$this->session->set_flashdata('tos_success', 'Loading vehicle authorized details updated.');
			// redirect('print_ctrl/print_autho_view_loading/'.$lul_id);
			redirect('authorizing_o_ctrl/loading_autho_list');
			
		}else {
			$this->session->set_flashdata('tos_error', 'Try again !...');
			redirect('authorizing_o_ctrl/loading_autho_list');
		}			
			
	}

	/*----------------- load edit loading vehicle  entry details page ----------------*/
	public function edit_loading_details($lul_id){	
		if($this->user['panel_type']=='2' && in_array("2", $this->u_roles)){			
			$data['sdata'] 						= array();
			$data['subview'] 					= 'loading/edit_loading_entry_details';
			$data['active_cls'] 				= 'dash';	
			$data['sdata']['cl_list'] 			= $this->admin_model->get_result_array('customer_list');				
			$data['sdata']['tl_list'] 			= $this->admin_model->get_result_array('transporter_list');				
			$data['sdata']['com_list'] 			= $this->admin_model->get_result_array('commodity_list');	
			$data['sdata']['lul_count'] 		= $this->admin_model->get_counts('loading_unloading_list');	
			if($lul_id!=''){
				$data['sdata']['view_type'] 	= "Edit";
				$data['sdata']['lul_id'] 		= $lul_id;
				$data['sdata']['lul_data'] = $this->admin_model->get_result_array('loading_unloading_list','lul_id',$lul_id);	
			}else{
				$data['sdata']['view_type'] 	= "Add";
			}
			$this->load->view('common/sidepanel',$data);
		}else{
			redirect('login_ctrl/logout');
		}
	}
	
	/*----------------- edit loading vehicle  entry  details -----------------*/
	public function update_loading_entry_details($lul_id= false){
		if(($this->user['panel_type']!='2') && (!in_array("2", $this->u_roles))){
			redirect('login_ctrl/logout');
		}		
		$this->form_validation->set_rules('lul_cl_id', 'Customer', 'trim|required');
		$this->form_validation->set_rules('lul_com_id', 'Commodity', 'trim|required');
		$this->form_validation->set_rules('lul_truck_no', 'Truck No', 'trim|required');
		$this->form_validation->set_rules('lul_tl_id', 'Transporter', 'trim|required');
		$this->form_validation->set_rules('lul_driver_name', 'Driver name', 'trim|required');
		$this->form_validation->set_rules('lul_driver_ph_no', 'Driver phone no', 'trim|required');
		$this->form_validation->set_rules('lul_lic', 'Driver Lic', 'trim|required');
		$this->form_validation->set_rules('lul_trailer_no', 'Trailer no', 'trim|required');
		$this->form_validation->set_rules('lul_destination', 'Destination', 'trim|required');

		
		if ($this->form_validation->run() === FALSE) {
			$e_msg = '';			
			$e_msg .= form_error('lul_cl_id');   			$e_msg .= form_error('lul_com_id'); 
			$e_msg .= form_error('lul_truck_no');   		$e_msg .= form_error('lul_tl_id');   
			$e_msg .= form_error('lul_driver_name');   		$e_msg .= form_error('lul_driver_ph_no');   
			$e_msg .= form_error('lul_lic');   			    $e_msg .= form_error('lul_trailer_no');  
			$e_msg .= form_error('lul_destination');
			$this->session->set_flashdata('tos_error', $e_msg);
			redirect('authorizing_o_ctrl/loading_autho_list');				
			
		}	
		$data['lul_details'] = array();
		foreach($_POST as $post_key=>$post_rows){
			$data['lul_details'][$post_key] 		= $this->input->post($post_key);
		}
		
		$data['lul_details']['lul_type'] 			= "Loading";
		
		$edit_cl = $this->admin_model->update_data('loading_unloading_list','lul_id',$lul_id,$data['lul_details']);
		if($edit_cl=='1'){
			$this->session->set_flashdata('tos_success', 'Loading details updated');
			redirect('authorizing_o_ctrl/loading_autho_list');
		}else {
			$this->session->set_flashdata('tos_warning', 'There is no changes!...');
			redirect('authorizing_o_ctrl/loading_autho_list');
		}
				
	}

	/*----------------- load edit loading vehicle  entry details page ----------------*/
	public function reject_loading_entry_details($lul_id){	
		if(($this->user['panel_type']!='2') && (!in_array("2", $this->u_roles))){
			redirect('login_ctrl/logout');
		}	
		
		$this->db->where('lul_id',$lul_id)->set('lul_reject','1')->update("loading_unloading_list");
		// echo $this->db->last_query();return;
		$this->session->set_flashdata('tos_success', 'Entry Rejected');

		redirect('authorizing_o_ctrl/loading_autho_list');
	}
	
}
