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

class Transporter_ctrl extends user_index {
 
	
	public function __construct(){ 
		parent::__construct();
		$data = array();
		$this->load->model('admin_model');	
	}
	
	/*----------------- load transporter list page -----------------*/
	public function transporter_list($tl_type=''){	
		$data['sdata'] 				= array();
		$data['subview'] 			= 'transporter/transporter_list';
		$data['active_cls'] 		= 'trans_cls';
		if($tl_type==''){
			$data['sdata']['tl_list'] 	= $this->admin_model->get_result_array_with_one_where('transporter_list','tl_type','l_u');		
		}else if($tl_type=='l_u'){
			$data['sdata']['tl_list'] 	= $this->admin_model->get_result_array_with_one_where('transporter_list','tl_type','l_u');		
		}else if($tl_type=='icd'){
			$data['sdata']['tl_list'] 	= $this->admin_model->get_result_array_with_one_where('transporter_list','tl_type','icd');		
		}	
		if($tl_type==''){
			$this->load->view('common/sidepanel',$data);			
		}else if($tl_type=='l_u'){
			$this->load->view('common/sidepanel',$data);			
		}else if($tl_type=='icd'){
			$this->load->view('common/icd_sidepanel',$data);			
		}	
	}
	

	/*----------------- load add transporter details page ----------------*/
	public function add_transporter_details($tl_type='',$tl_id=''){	
		$data['sdata'] 						= array();
		$data['subview'] 					= 'transporter/add_transporter_details';
		$data['active_cls'] 				= 'trans_cls';	
		if($tl_type==''){
			$data['sdata']['tl_type'] 	= "l_u";		
		}else if($tl_type=='icd'){
			$data['sdata']['tl_type'] 	= "icd";		
		}	
		if($tl_id!=''){
			$data['sdata']['view_type'] 	= "Edit";
			$data['sdata']['tl_data'] = $this->admin_model->get_result_array('transporter_list','tl_id',$tl_id);	
		}else{
			$data['sdata']['view_type'] 	= "Add";
		}
		if($tl_type==''){
			$this->load->view('common/sidepanel',$data);			
		}else if($tl_type=='icd'){
			$this->load->view('common/icd_sidepanel',$data);			
		}	
	}
	
	
	/*----------------- add transporter  details and edit transporter  details -----------------*/
	public function insert_transporter($tl_type,$tl_id= false){		
		$this->form_validation->set_rules('tl_name', 'Name', 'trim|required');
// 		$this->form_validation->set_rules('tl_mobile', 'Mobile number', 'trim|required');
// 		$this->form_validation->set_rules('tl_email', 'Emailid', 'trim|required');
// 		$this->form_validation->set_rules('tl_state', 'State', 'trim|required');
// 		$this->form_validation->set_rules('tl_district', 'District', 'trim|required');
// 		$this->form_validation->set_rules('tl_place', 'Place', 'trim|required');
// 		$this->form_validation->set_rules('tl_pincode', 'Pincode', 'trim|required');
// 		$this->form_validation->set_rules('tl_address', 'Address', 'trim|required');
// 		$this->form_validation->set_rules('tl_tin_no', 'TIN No', 'trim|required');
// 		$this->form_validation->set_rules('tl_vrn_np', 'VRN Np', 'trim|required');
// 		$this->form_validation->set_rules('tl_co_name', 'Contact Person Name', 'trim|required');
// 		$this->form_validation->set_rules('tl_phone', 'Phone No', 'trim|required');
		
		if ($this->form_validation->run() === FALSE) {
			$e_msg = '';
			$e_msg .= form_error('tl_name');  				$e_msg .= form_error('tl_pincode');  			
// 			$e_msg .= form_error('tl_mobile');   			$e_msg .= form_error('tl_address'); 
// 			$e_msg .= form_error('tl_email');   			$e_msg .= form_error('tl_state');   
// 			$e_msg .= form_error('tl_district');   			$e_msg .= form_error('tl_place');  
// 			$e_msg .= form_error('tl_tin_no');   			$e_msg .= form_error('tl_vrn_np');   
// 			$e_msg .= form_error('tl_co_name');   			$e_msg .= form_error('tl_phone'); 
			$this->session->set_flashdata('tos_error', $e_msg);
			if($tl_id==''){
				redirect('transporter_ctrl/transporter_list/'.$tl_type.'');				
			}else{
				redirect('transporter_ctrl/transporter_list/'.$tl_type.'');				
			}
		}	
		$data['tl_details'] = array();
		foreach($_POST as $post_key=>$post_rows){
			$data['tl_details'][$post_key] 		= $this->input->post($post_key);
		}
		
		$data['tl_details']['tl_type'] = $tl_type;
		
		if($tl_id==false){				 
			$add_tl 						= $this->admin_model->insert_item('transporter_list',$data['tl_details']);		
			$data['tl_details']['tl_code'] = 'TR-'.$add_tl;
			
			$code_tl = $this->admin_model->update_data('transporter_list','tl_id',$add_tl,$data['tl_details']);
			
			if($add_tl!=false){
				$this->session->set_flashdata('tos_success', 'Transporter details added.');
				redirect('transporter_ctrl/transporter_list/'.$tl_type.'');
			}else {
				$this->session->set_flashdata('tos_error', 'Try again !...');
				redirect('transporter_ctrl/transporter_list/'.$tl_type.'');
			}			
		}else{
			$edit_tl = $this->admin_model->update_data('transporter_list','tl_id',$tl_id,$data['tl_details']);
			if($edit_tl=='1'){
				$this->session->set_flashdata('tos_success', 'Transporter details updated');
				redirect('transporter_ctrl/transporter_list/'.$tl_type.'');
			}else {
				$this->session->set_flashdata('tos_warning', 'There is no changes!...');
				redirect('transporter_ctrl/transporter_list/'.$tl_type.'');
			}
		}		
	}

	
	

	
}
