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

class Promotioncode extends Admin_Controller{

	function __construct()
	{
		parent::__construct();
		$this->load->model("mod_promotioncode");
		$this->load->helper(array('string','my_function'));
	}


	function index()
	{
		$tab['activeTab'] = "promotioncode";
		$data["result"] = $this->mod_promotioncode->getpromotioncode();
		$data["result"] = $data["result"]->result_array(); 
		
		$this->load->view("header",$tab);	
		$this->load->view("promotioncode/view",$data);
		$this->load->view("footer");	
	}
	
		
	function add()
    {	   
		$tab['activeTab'] = "promotioncode";
		
		$this->form_validation->set_rules('code', 'code', 'required');
		$this->form_validation->set_rules('price', 'price', '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('description', 'description', 'required');
		$this->form_validation->set_rules('status', 'Status', 'required');
		//$this->form_validation->set_rules('payment[]', 'Please Choose any Payment', 'required');
		if ($this->form_validation->run() == TRUE)
		{
			$ID = $this->input->post('payment');
			//$ID = json_encode($ID);
			$ID = '';
			$airline = $this->input->post('airline');
			$adtnum = $this->input->post('passenger');
			//Altering Database
			$data = array(
				'coupon_title'	=>	$this->input->post('title'),
				'coupon_code'	=>	$this->input->post('code'),
				'coupon_value'	=>	$this->input->post('price'),
				'remark'		=>	$this->input->post('description'),
				'from_date'		=>	date("Y-m-d", strtotime($this->input->post('from_date'))),
				'to_date'		=>	date("Y-m-d", strtotime($this->input->post('to_date'))),
				'coupon_limit'	=>	$this->input->post('limited'),
				'payment_method'=>	$ID,
				'status'		=>	$this->input->post('status'),
				'adtnum'		=>	$adtnum,
				'airline'		=>	$airline,
				'createdby'		=>  $this->session->userdata('userId'),
				'createddate'	=>  date('Y-m-d'),
			);
			$msg = $this->mod_promotioncode->add($data);
			//End Altering Database
			if($msg == 1 || $msg == 0)
			{
				$this->session->set_flashdata('message',"New Promotion Code is Successfully Added");
				redirect('admin/promotioncode/add');
				exit();		
			}else{
				$this->session->set_flashdata('message',"New Promotion Code is Failure Added!");
				redirect('admin/promotioncode/add');
				exit();		
			}		
		}
		
		$data['code'] = random_string('alpha', 6);
			
		$this->load->view('admin/header',$tab);
		$this->load->view('admin/promotioncode/add',$data);
		$this->load->view('admin/footer');	
	}
	
	
	function edit()
	{
		$tab['activeTab'] = "promotioncode";
		$id = $this->uri->segment(4);
		$promotion = $this->mod_promotioncode->getById($id);
		if(!$promotion)
		{
			redirect("admin/promotioncode");
			exit();
		}
	
		if(@$_POST['update_promotioncode'])
		{	
			$ID = $this->input->post('payment');
			//$ID = json_encode($ID);
			$ID = '';
			$airline = $this->input->post('airline');
			$adtnum = $this->input->post('passenger');
			
			$postdata = array(
				'coupon_code'	=>	$this->input->post('code'),
				'coupon_title'	=>	$this->input->post('title'),
				'coupon_value'	=>	$this->input->post('price'),
				'remark'		=>	$this->input->post('description'),
				'from_date'		=>	date("Y-m-d", strtotime($this->input->post('from_date'))),
				'to_date'		=>	date("Y-m-d", strtotime($this->input->post('to_date'))),
				'coupon_limit'	=>	$this->input->post('limited'),
				'payment_method'=>	$ID,
				'status'		=>	$this->input->post('status'),
				'adtnum'		=>	$adtnum,
				'airline'		=>	$airline,
				'modifiedby'	=>  $this->session->userdata('userId'),
				'modifieddate'	=>  date('Y-m-d'),
			);
				
			$msg = $this->mod_promotioncode->update($postdata,$id);
			$this->session->set_flashdata('message',"Promotion Code is Successfully Update");
			redirect("admin/promotioncode");
			exit();
		}
		
		$data['result'] = $promotion;
		$this->load->view('admin/header',$tab);
		$this->load->view('admin/promotioncode/edit',$data);
		$this->load->view('admin/footer');		
	}
	
	
	function delete()
	{
		$id = $this->uri->segment(4);
		$this->mod_promotioncode->delete($id);
		$this->session->set_flashdata('message',"Promotion Code deleted successfully");
		redirect("admin/promotioncode");
		exit();
	}
	
}