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

class Unit_ctrl extends user_index {
 
	
	public function __construct(){ 
		parent::__construct();
		$data = array();
		$this->load->model('admin_model');	
	}
	
	
	

	/*----------------- load add unit details & list unit page ----------------*/
	public function units_list($unl_id=''){	
		$data['sdata'] 						= array();
		$data['subview'] 					= 'unit/units_list';
		$data['active_cls'] 				= 'com_cls';	
		$data['sdata']['unl_list'] 	= $this->admin_model->get_result_array('units_list');				
		
		if($unl_id!=''){
			$data['sdata']['view_type'] 	= "Edit";
			$data['sdata']['unl_data'] = $this->admin_model->get_result_array('units_list','unl_id',$unl_id);	
		}else{
			$data['sdata']['view_type'] 	= "Add";
		}
		$this->load->view('common/sidepanel',$data);
	}
	
	
	/*----------------- add unit  details and edit unit  details -----------------*/
	public function insert_units($unl_id= false){		
		$this->form_validation->set_rules('unl_unit', 'Unit', 'trim|required');
		
		if ($this->form_validation->run() === FALSE) {
			$e_msg = '';
			$e_msg .= form_error('unl_unit');  				   
			$this->session->set_flashdata('tos_error', $e_msg);
			redirect('unit_ctrl/units_list');				
			
		}	
		$data['unl_details'] = array();
		foreach($_POST as $post_key=>$post_rows){
			$data['unl_details'][$post_key] 		= $this->input->post($post_key);
		}
		

		if($unl_id==false){				
			$add_unl 						= $this->admin_model->insert_item('units_list',$data['unl_details']);		
			
			if($add_unl!=false){
				$this->session->set_flashdata('tos_success', 'unit details added.');
				redirect('unit_ctrl/units_list');
			}else {
				$this->session->set_flashdata('tos_error', 'Try again !...');
				redirect('unit_ctrl/units_list');
			}			
		}else{
			$edit_unl = $this->admin_model->update_data('units_list','unl_id',$unl_id,$data['unl_details']);
			if($edit_unl=='1'){
				$this->session->set_flashdata('tos_success', 'unit details updated');
				redirect('unit_ctrl/units_list');
			}else {
				$this->session->set_flashdata('tos_warning', 'There is no changes!...');
				redirect('unit_ctrl/units_list/'.$unl_id);
			}
		}		
	}

	
	

	
}
