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

class Route_Assign extends CI_Controller {

    var $main_menu_name = "settings";
	var $sub_menu_name = "route_assign";
	
	public function __construct()
	{
		parent::__construct();

		$this->load->model('Route_Assign_Model');
		$this->load->model('Warehouse_Model');
		$this->load->model('Common_Model');
		$this->load->model('Sequerty_Model');
		
	}

	public function index()
	{
		$data['main_menu_name'] = $this->main_menu_name;
		$data['sub_menu_name'] = $this->sub_menu_name;
		$data['master_area'] = $this->Route_Assign_Model->get_all_area_names();
        $this->load->view('route_assign',$data);
	}
	
	public function get_all_route_by_warehouse_id()
	{
	
		$warehouse_id=$this->input->get('WH');
		$route = $this->Route_Assign_Model->get_all_route_by_warehouse_id($warehouse_id);
		echo json_encode($route);
	
	}
	
	public function save_route()
	{
		$route_id=$this->input->post('route_id');
		$type=$this->input->post('type');
		$route_name=$this->input->post('route_name');
		$route_code=$this->input->post('route_code');
		$warehouse_id=$this->input->post('warehouse_id');
		$data=array(
			'route_name'=>$route_name,
			'route_code'=>$route_code,
			'warehouse_id'=>$warehouse_id
		);
		
		$this->load->library('form_validation'); //form validation lib
		if($type=='A')
		{
			$this->form_validation->set_rules('route_code', 'Code', 'required|is_unique[routes.route_code]');
		}
		else if($type=='E')
		{
			$this->form_validation->set_rules('route_code', 'Code', 'required');
		}
		
		if ($this->form_validation->run() == FALSE)
        {
           $st = array('status' =>0,'validation' => validation_errors());
           echo json_encode($st);
        }
        else
        {
		
		$_insert=$this->Route_Assign_Model->save_route($data,$route_id);
		$lastid=$this->db->insert_id();

		if($type=='A'){
			if ($lastid) {
				echo json_encode(array('id'=>$lastid,'type'=>$type));
			} else {
				echo json_encode(array('status'=>'error'));
			}
		}
		if($type=='E'){
			echo json_encode(array('type'=>$type));
		}
		}
	}

	public function add_route()
	{
        if (isset($_GET['route_id'])) {
			$route_id=$_GET['route_id'];
		}
		else {
			$route_id='';
		}
		if($route_id){
			$data['route_id']=$route_id;
			$data['type']='E';
			$data['pageName']='UPDATE  ROUTE';
			$data['btnText']='Update Route';
			$data['routes_details']= $this->Route_Assign_Model->get_route_info($route_id);	
			$data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
		}
		else {
			$data['route_id']='';
			$data['type']='A';
			$data['pageName']='ADD ROUTE';
			$data['btnText']='Add Route';
			$data['routes_details']=array();
			$data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
		}
        $this->load->view('models/create_route',$data);
	}

	public function list_selected_route()
	{
		$area_id=$this->input->get('area_id');
	$requestData= $_REQUEST;
	
	$columns = array( 
		0 =>'route_code', 
		1 => 'route_name'
	);
	
	$data = array();
	$route = $this->Route_Assign_Model->get_selected_route($area_id);
	$totalData = count($route);
	$totalFiltered = $totalData;  
	
	foreach ($route as $row){
		$nestedData=array(); 
		$nestedData[] = $row['route_name'];
		
		$actionTxtDisble='';
		$actionTxtEnable='';
		$actionTxtUpdate='';
		$actionTxtDelete='';
																
 $actionTxtDisble = '<a class="btn btn-xs btn-green tooltips" data-placement="top" data-original-title="Disable supplier" onClick="disableUnitData('.$row['route_id'].')"><i class="glyphicon fa fa-check"></i></a> &nbsp;';
	
	$nestedData[]=$actionTxtUpdate.$actionTxtDisble.$actionTxtEnable.$actionTxtDelete;
	$data[] = $nestedData;
}

	$json_data = array(
			//"draw"          => intval( $requestData['draw'] ),  
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ),
			"data"            => $data 
			);

	echo json_encode($json_data); 
	}

// second table 

public function list_pending_route()
	{
	$requestData= $_REQUEST;
	
	$columns = array( 
		0 =>'route_code', 
		1 => 'route_name'
	);
	
	$data = array();
	$route = $this->Route_Assign_Model->get_pending_route();
	$totalData = count($route);
	$totalFiltered = $totalData;  
	
	foreach ($route as $row){
		$nestedData=array(); 
		$nestedData[] = $row['route_name'];
		//$nestedData[] =$row['route_code'];
		$actionTxtDisble='';
		$actionTxtEnable='';
		$actionTxtUpdate='';
		$actionTxtDelete='';
		
$actionTxtEnable = '<a class="btn btn-xs btn-warning tooltips" data-placement="top" data-original-title="Disable supplier" onClick="enableUnitData('.$row['route_id'].')"><i class="glyphicon fa fa-minus-circle"></i></a> &nbsp;';
			
	$nestedData[]=$actionTxtUpdate.$actionTxtDisble.$actionTxtEnable.$actionTxtDelete;
	$data[] = $nestedData;
}

	$json_data = array(
			//"draw"            => intval( $requestData['draw'] ),  
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ),
			"data"            => $data 
			);

	echo json_encode($json_data); 
	}


	/*function delete_route() {
		$route_id	= $this->input->post('route_id');
		$this->Route_Assign_Model->delete_route($route_id);
        if ($route_id) {
        	echo json_encode(array('id'=>$route_id));
        } else {
        	echo json_encode(array('status'=>'error'));
        }
	}*/
	
	function disable_route() {
		$route_id	= $this->input->post('route_id');
		$this->Route_Assign_Model->disable_route($route_id);
        if ($route_id) {
        	echo json_encode(array('id'=>$route_id));
        } else {
        	echo json_encode(array('status'=>'error'));
        }
	}
	
	function enable_route() {
		$route_id	= $this->input->post('route_id');
		$area_id	= $this->input->post('area_id');
		$this->Route_Assign_Model->enable_route($route_id,$area_id);
        if ($route_id) {
        	echo json_encode(array('id'=>$route_id));
        } else {
        	echo json_encode(array('status'=>'error'));
        }
	}
	
	
	public function add_area()
	{
		//$area_id = $this->input->get('area_id');
		
        if (isset($_GET['area_id'])) {
			$area_id=$_GET['area_id'];
		}
		else {
			$area_id='';
		}
		if($area_id){//echo $area_id;
			$data['area_id']=$area_id;
			$data['type']='E';
			$data['pageName']='UPDATE  AREA';
			$data['btnText']='Update Area';
			$data['area_details']= $this->Area_Model->get_area_info($area_id);
			$data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
			
		}
		else {
			$data['area_id']='';
			$data['type']='A';
			$data['pageName']='ADD Area';
			$data['btnText']='Add Area';
			$data['area_details']=array();
			$data['warehouse_list'] = $this->Warehouse_Model->get_all_warehouse();
		}
        $this->load->view('models/create_area',$data);
	}
	
	
}