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

class Discount extends Admin_Controller{
	
	function __construct()
	{
		parent::__construct();
		$this->load->model("mod_promotion");
		$this->load->helper('my_function');
	}


	function index()
	{	
		$tab['activeTab'] = "promotion";
		/*if($this->session->userdata('view.'.MARKUP_CODE.'') != 1)
		{
			$this->session->set_flashdata('message',"You had not permission on this pages");
			redirect("admin/log");
			exit();
		}*/

		$data["result"] = $this->mod_promotion->getPromotion();
		$data["result"] = $data["result"]->result_array(); 
		
		$this->load->view("header",$tab);	
		$this->load->view("promotion/view",$data);
		$this->load->view("footer");	
	}
	
		
	function add()
    {	
		$tab['activeTab'] = "promotion";
		
		$this->form_validation->set_rules('title', 'Title', 'required');
		$this->form_validation->set_rules('services', 'Service Type', 'required');
		$this->form_validation->set_rules('car', 'Car Type', 'required');
        $this->form_validation->set_rules('from_date', 'From Date', 'required');
		$this->form_validation->set_rules('to_date', 'To Date', 'required');
		$this->form_validation->set_rules('st_tra_period', 'Start Travelling Date', 'required');
		$this->form_validation->set_rules('en_tra_period', 'End Travelling Date', 'required');
		$this->form_validation->set_rules('price', 'Price', 'required');
		$this->form_validation->set_rules('pro_type', 'Price Type', 'required');
		$this->form_validation->set_rules('status', 'Status', 'required');
		$this->form_validation->set_rules('from', 'Location From', 'required');
		$this->form_validation->set_rules('to', 'Location To', 'required');
		
		if ($this->form_validation->run() == TRUE)
		{
			if(in_array($this->input->post('services'),array(1,2,3))){
				$origincity = 0;
			}else{
				$origincity = 12;
			}
			//Altering Database
			$data = array(
				'title'				=>	$this->input->post('title'),
				'price'				=>	$this->input->post('price'),
				'pro_type'			=>  $this->input->post('pro_type'),
				'service_type'		=>  $this->input->post('services'),
				'car_id'			=>  $this->input->post('car'),
				'st_tra_period'		=>	date("Y-m-d", strtotime($this->input->post('st_tra_period'))),
				'en_tra_period'		=>	date("Y-m-d", strtotime($this->input->post('en_tra_period'))),
				'description'		=>	$this->input->post('description'),
				'from_date_period'	=>	date("Y-m-d", strtotime($this->input->post('from_date'))),
				'to_date_period'	=>	date("Y-m-d", strtotime($this->input->post('to_date'))),
				'origincity'		=>	$origincity,
				'returncity'		=>	$this->input->post('to'),
				'status'			=>	$this->input->post('status'),
				'createdby'			=>  $this->session->userdata('userId'),
				'createddate'		=>  date('Y-m-d'),	
			);
			$msg = $this->mod_promotion->add($data);
			//End Altering Database
			if($msg == 1 || $msg == 0){
				$this->session->set_flashdata('message',"New Promotion is Successfully Added");
				redirect('admin/discount/add');
				exit();		
			}else{
				$this->session->set_flashdata('message',"New Promotion is Failure Added!");
				redirect('admin/discount/add');	
				exit();	
			}		
		}
				
		$this->load->view('admin/header',$tab);
		$this->load->view('admin/promotion/add');
		$this->load->view('admin/footer');	
	}
	
	
	function edit()
	{
		$tab['activeTab'] = "promotion";
		$id = $this->uri->segment(4);
		$promotion = $this->mod_promotion->getById($id);
		if(!$promotion)
		{
			redirect("admin/promotion");
			exit();
		}
	
		if(@$_POST['update_promotion'])
		{	
			if(in_array($this->input->post('services'),array(1,2,3))){
				$origincity = 0;
			}else{
				$origincity = 12;
			}
			
			$postdata = array(
				'title'				=>	$this->input->post('title'),
				'price'				=>	$this->input->post('price'),
				'pro_type'			=>  $this->input->post('pro_type'),
				'service_type'		=>  $this->input->post('services'),
				'car_id'			=>  $this->input->post('car'),
				'st_tra_period'		=>	date("Y-m-d", strtotime($this->input->post('st_tra_period'))),
				'en_tra_period'		=>	date("Y-m-d", strtotime($this->input->post('en_tra_period'))),
				'description'		=>	$this->input->post('description'),
				'from_date_period'	=>	date("Y-m-d", strtotime($this->input->post('from_date'))),
				'to_date_period'	=>	date("Y-m-d", strtotime($this->input->post('to_date'))),
				'origincity'		=>	$origincity,
				'returncity'		=>	$this->input->post('to'),
				'status'			=>	$this->input->post('status'),
				'modifiedby'		=>  $this->session->userdata('userId'),
				'modifieddate'		=>  date('Y-m-d'),		
			);
				
			$msg = $this->mod_promotion->update($postdata,$id);
			$this->session->set_flashdata('message',"Promotion is Successfully Update");
			redirect("admin/discount");
			exit();
		}
		
		$data['result'] = $promotion;
		$this->load->view('admin/header',$tab);
		$this->load->view('admin/promotion/edit',$data);
		$this->load->view('admin/footer');		
	}
	
	
	function delete()
	{
		$id = $this->uri->segment(4);
		$this->mod_promotion->delete($id);
		$this->session->set_flashdata('message',"Promotion deleted successfully");
		redirect("admin/discount");
		exit();
	}
	
}