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

       public function index()
       {
		    $tab['activeTab'] = "price";
		    $this->load->model("mod_price");
			$data["price"] = $this->mod_price->getprice();
			$data["price"] = $data["price"]->result_array(); 
			
			$this->load->view('admin/header',$tab);
			$this->load->view('admin/price/view',$data);
			$this->load->view('admin/footer');
       }
	
	   public function add()
       {
		    $tab['activeTab'] = "price";
		    $this->load->model("mod_price");
			$this->form_validation->set_rules('price', 'Price', 'required|numeric');
		    
			if ($this->form_validation->run() == TRUE)
			{
				$car_id = $_POST['car'];
				$service_id = $_POST['services'];
				$RID = $_POST['routes'];
				if($this->price->compprice($car_id,$service_id,$RID)){
				//Altering Database
					$user_id = $this->session->userdata('userId');
					$data['price'] = trim($_POST['price']);
					$data['vat']   = trim($_POST['vat']);
					$data['over_stay'] = trim($_POST['over_stay']);
					$data['wholeday'] = trim($_POST['wholeday']);
					$data['RID'] = $_POST['routes'];
					$data['service_id'] = $_POST['services'];
					$data['brand_id'] = $_POST['car'];
					$data['car_id'] = $_POST['car'];
					$data['createdate'] = date('y-m-d');
					$data['createby'] = $user_id;
				//End Altering Database
					$msg = $this->price->add($data);
					$this->session->set_flashdata('message',"New Price is Successfully Added");
					redirect('admin/price/add');
					exit();
				}else{
					$this->session->set_flashdata('message',"New Price is Failure Added!");
					redirect('admin/price/add');	
					exit();	
				}	
			}
				
			$this->load->view('admin/header',$tab);
			$this->load->view('admin/price/add');
			$this->load->view('admin/footer');
       }
	
		
	   public function edit()
	   {
		    $tab['activeTab'] = "price";
			$id = $this->uri->segment(4);
			if($this->uri->segment(5)){
				$my_car_id = $this->uri->segment(5);
			}else{
				$my_car_id = 0;		
			}
			$price = $this->price->getById($id);
			if(!$price)
			{
				redirect("admin/price");
				exit();
			}
	
			if(@$_POST['update_price'])
			{	
				$car_id = $_POST['car'];
				$service_id = $_POST['services'];
				$RID = $_POST['routes'];
				if($this->price->editcompprice($id,$brand_id,$service_id,$RID))
				{
					$user_id = $this->session->userdata('userId');								
					$postdata['price'] = trim($_POST['price']);
					$postdata['vat']   = trim($_POST['vat']);
					$postdata['over_stay'] = trim($_POST['over_stay']);
					$postdata['wholeday'] = trim($_POST['wholeday']);
					$postdata['RID'] = $_POST['routes'];
					$postdata['service_id'] = $_POST['services'];
					$postdata['car_id'] = $_POST['car'];
					$postdata['brand_id'] = $_POST['car'];
					$postdata['modifydate'] = date('y-m-d');
					$postdata['modifyby'] = $user_id;
					$this->price->update($postdata,$id);
					$this->session->set_flashdata('message',"Price is Successfully Update");
					if($my_car_id == 0){
						redirect("admin/price");
						exit();	
					}else{
						redirect('admin/car/detail/'.$my_car_id.'');
						exit();
					}
				}else{
					$this->session->set_flashdata('message',"Price had Already");
					redirect('admin/price/edit/'.$id.'');
        			exit();	
				}   
			}
			
			$data['my_car_id'] = $my_car_id;
		    $data['price'] = $price;
		    $this->load->view('admin/header',$tab);
			$this->load->view('admin/price/edit',$data);
			$this->load->view('admin/footer');	
	   }
	   
	   function delete()
	   {
			$id = $this->uri->segment(4);
			$this->price->delete($id);
			$this->session->set_flashdata('message',"Price deleted successfully");
			redirect("admin/price");
			exit();
	   }
	   
	   function showroute()
	   {	
			$ID = $this->input->POST('Id');
			$data['price'] = $this->price->showprice($ID);
			$price = $data['price']->result_array();
			foreach($price as $v)
			{
				echo'<option value="'.$v['id'].'">'.$v['routecode'].'</option>';	 
			}
	   }
	   
	   
	   
}