<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Routes extends Admin_Controller {
	
	   function __construct()
	   {
			parent::__construct();
			$this->load->model("mod_routes","routes");
			$this->load->helper(array('my_function'));
	   }		

       public function index()
       {	
	   		$tab['activeTab'] = "routes";
		    $this->load->model("mod_routes");
			$data["routes"] = $this->mod_routes->getroutes();
			$data["routes"] = $data["routes"]->result_array(); 
			
			$this->load->view('admin/header',$tab);
			$this->load->view('admin/routes/view',$data);
			$this->load->view('admin/footer');
       }
	
	   public function add()
       {	
	   		$tab['activeTab'] = "routes";
		    $this->load->model("mod_routes");
			$this->form_validation->set_rules('routecode', 'RouteCode', 'required');
		    
			if ($this->form_validation->run() == TRUE)
			{
				$from = $_POST['from'];
        		$to = $_POST['to'];
				$service = $_POST['service'];
				if($this->routes->comproute($from,$to,$service))
				{
				//Altering Database
					$user_id = $this->session->userdata('userId');
					$data['routecode'] = trim($_POST['routecode']);
					$data['service_id'] = $_POST['service'];
					$data['time'] = $_POST['time'];
					$data['locationfrom'] = $_POST['from'];
					if($service==4){
						$data['locationto'] = $_POST['to'];	
					}else{
						$data['locationto'] = 0;	
					}
					$data['status'] = $_POST['status'];
					$data['createdate'] = date('y-m-d');
					$data['createby'] = $user_id;
					$data['routetype'] = $_POST['routetype'];
					$this->routes->add($data);
				//End Altering Database
	
					$this->session->set_flashdata('message',"New Routes is Successfully Added");
					redirect('admin/routes/add');
					exit();
				}else{
					$this->session->set_flashdata('message',"Routes Added failure");
					redirect('admin/routes/add');
        			exit();	
				}
			}
			
			$this->load->view('admin/header',$tab);
			$this->load->view('admin/routes/add');
			$this->load->view('admin/footer');
       }
	
		
	   public function edit()
	   {
			$tab['activeTab'] = "routes";
			$id = $this->uri->segment(4);
			$routes = $this->routes->getById($id);
			if(!$routes)
			{
				redirect("admin/routes");
				exit();
			}
	
			if(@$_POST['update_routes'])
			{	
				$from = $_POST['from'];
        		$to = $_POST['to'];
				$service = $_POST['service'];
				//if($this->routes->editcomproute($id,$from,$to,$service))
				if(count($this->routes->editcomproute($id,$from,$to,$service))<=1)
				{	
					$user_id = $this->session->userdata('userId');
					$postdata['routecode'] = trim($_POST['routecode']);
					$postdata['service_id'] = $_POST['service'];
					$postdata['time'] = $_POST['time'];
					$postdata['locationfrom'] = $_POST['from'];
					if($service==4){
						$postdata['locationto'] = $_POST['to'];
					}else{
						$postdata['locationto'] = 0;
					}
					$postdata['status'] = $_POST['status'];
					$postdata['modifydate'] = date('y-m-d');
					$postdata['modifyby'] = $user_id;
					$postdata['routetype'] = $_POST['routetype'];

					$msg = $this->routes->update($postdata,$id);
					$this->session->set_flashdata('message',"Routes is Successfully Update");
					redirect("admin/routes");
					exit();
				}else{
					$this->session->set_flashdata('message',"Routes had Already");
					redirect('admin/routes/edit/'.$id.'');
        			exit();		
				}
			}
		
		    $data['routes'] = $routes;
		    $this->load->view('admin/header',$tab);
			$this->load->view('admin/routes/edit',$data);
			$this->load->view('admin/footer');	
	   }
	   
	   
	   function active()
	   {
			$id = $this->uri->segment(4);
			$active = $this->uri->segment(5);
			if($active == 1){
				$data['status'] = 0;
			}else{
				$data['status'] = 1;
			}
			
			$this->routes->active($id,$data);
			$this->session->set_flashdata('message',"Status Update successfully");
			redirect("admin/routes");	 
			exit();
	   }
	   
	   
	   function getlocation()
	   {	
			//$ID = $this->input->post('locationId');
			$ID = $this->input->POST('locationId');
			$data['routes'] = $this->routes->getlocation($ID);
			$routes = $data['routes']->result_array();
			foreach($routes as $v)
			{
				echo'<option value="'.$v['id'].'">'.$v['locationname'].'</option>';	 
			}
			
	   }
		
}