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

       public function index()
       {
		    $tab['activeTab'] = "peaktime";
		    $this->load->model("mod_peaktime");
			$data["peaktime"] = $this->mod_peaktime->getpeaktime();
			$data["peaktime"] = $data["peaktime"]->result_array(); 
			
			$this->load->view('admin/header',$tab);
			$this->load->view('admin/peaktime/view',$data);
			$this->load->view('admin/footer');
       }
	
	   public function add()
       {
		    $tab['activeTab'] = "peaktime";
		    $this->load->model("mod_peaktime");
			$this->form_validation->set_rules('amount', 'Amount', 'required');
		    
			if ($this->form_validation->run() == TRUE)
			{
				$from = $_POST['from'];
				$to = $_POST['to'];
				$service = $_POST['service'];
				$car = $_POST['cartype'];
			if($this->peaktime->compeaktime($from,$to,$service,$car)){
				//Altering Database
				$user_id = $this->session->userdata('userId');
				$data['service_id'] = $_POST['service'];
				$data['amount'] = trim($_POST['amount']);
				$data['from_location'] = $_POST['from'];
				if($service == 1){
				$data['to_location'] = $_POST['to'];
				}
				$data['from_time'] = date("H:i", strtotime("".$_POST['timefrom'].""));
				$data['to_time'] = date("H:i", strtotime("".$_POST['timeto'].""));
				$data['typecar'] = $_POST['cartype'];
				$data['createdate'] = date('y-m-d');
				$data['createby'] = $user_id;
				
				$this->peaktime->add($data);
				//End Altering Database
				$this->session->set_flashdata('message',"New Peak Time is Successfully Added");
					redirect('admin/peaktime/add');
			}else{
				
				$this->session->set_flashdata('message',"New Peak Time is Failure Added!");
				    redirect('admin/peaktime/add');
					exit();	
			}
			    
				
				
			}
				
			$this->load->view('admin/header',$tab);
			$this->load->view('admin/peaktime/add');
			$this->load->view('admin/footer');
       }
	
		
	   public function edit()
	   {
			$tab['activeTab'] = "peaktime";
			$id = $this->uri->segment(4);
			$peaktime = $this->peaktime->getById($id);
			if(!$peaktime)
			{
				redirect("admin/peaktime");
			}
	
			if(@$_POST['update_peaktime'])
			{	
				$from = $_POST['from'];
				$to = $_POST['to'];
				$service = $_POST['service'];
				$car = $_POST['cartype'];
				if($this->peaktime->editcompeaktime($id,$from,$to,$service,$car)){
					$user_id = $this->session->userdata('userId');
					$postdata['service_id'] = $_POST['service'];
					$postdata['amount'] = trim($_POST['amount']);
					$postdata['to_location'] = $_POST['to'];
					$postdata['from_location'] = $_POST['from'];
					$postdata['from_time'] = date("H:i", strtotime("".$_POST['timefrom'].""));
					$postdata['to_time'] = date("H:i", strtotime("".$_POST['timeto'].""));
					$postdata['typecar'] = $_POST['cartype'];
					$postdata['modifydate'] = date('y-m-d');
					$postdata['modifyby'] = $user_id;			
				
					$this->peaktime->update($postdata,$id);
					$this->session->set_flashdata('message',"Peak Ttime is Successfully Update");
					redirect("admin/peaktime");
				}else{
					$this->session->set_flashdata('message',"Peak Ttime had Already!");
					redirect('admin/peaktime/edit/'.$id.'');	
					
				}
				
			}
		
		    $data['peaktime'] = $peaktime;
		    $this->load->view('admin/header',$tab);
			$this->load->view('admin/peaktime/edit',$data);
			$this->load->view('admin/footer');	
	   }
	   
	   
	   function delete()
	   {
			$id = $this->uri->segment(4);
			$this->peaktime->delete($id);
			$this->session->set_flashdata('message',"Peak Ttime deleted successfully");
			redirect("admin/peaktime");
	   }
	   
		
}