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


class Order_admin extends CI_Controller {
	
	private $id			=	''	; 
	private $uname		=	''	; 
	private $pword		=	''	; 
	private $error		=	'' 	;
	private $user		=	''	;

	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->library('loginfunctions');
		
		$this->load->helper('basic_helper');
		
		//logged out
		if($this->loginfunctions->checkloggedin()===FALSE)
		{
			redirect('Admin/') ;
		}
		else
		{
			$this->load->model('Db_model') ;
		}
		

	}
	
	public function index()
	{
		$this->view();
	}



	public function view()
	{
	
		$statuso = '' ;
		$this->load->helper('basic_helper') ;
		
		//$result_array = $this->Db_model->get_by_order_group('shop_order','order_date','desc','order_id') ;
		if($this->input->post('submit'))
		{
		$data = array(
		$statuso=$this->input->post('statuso'));
		//$result_array = $this->Db_model->get_all_by_order('shop_shipdetails','order_date','desc') ;
		$result_array = $this->Db_model->get_where_by_order('shop_shipdetails',array('status'=>$statuso,'remove'=>0),'order_date','desc') ;
		}
		else
		{
		$result_array = $this->Db_model->get_where_by_order('shop_shipdetails',array('remove'=>0),'order_date','desc') ;
		}
		
	
		if($result_array===FALSE)
		{
			$result_array = array() ;
		}
		
		$data=  array(
					  'page_name' => 'Order' ,
					  'heading' => 'Order' ,
					  'top_link_name' => '' ,
					  'top_link' => '' ,
					  'result' => $result_array
					 );
	
		
		$this->load->view('admin/home_header',$data);
		$this->load->view('admin/menu');
		/*$this->load->view('admin/top_links',$data);*/
		$this->load->view('admin/view_orders',$data);
		$this->load->view('admin/footer');
	
	}
	
	
	
	
	public function order_details($order_id='')
	{

	$data = array(
				'cart_qty'=>$this->cart_qty,	  
				'user' => $this->user,
				'email' => $this->user,
				'logged' => $this->logged,
				'capimage' => $capimage,
				'c_val' => $this->encrypt->encode($cap_result),
				'suc_msg' => '',
				'result' => '',
				'offer' => '',
				'error' => ''
				);

		$data['category']=$data['result'] = $this->Db_model->get_where_by_order('shop_category',array(),'order','asc') ;	
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array('gift'=>2),'order','asc') ;

		
	$data['result2'] = $this->Db_model->get_where('shop_order',array('order_id'=>$order_id));
		
		$data['contactus'] = $this->Db_model->get_where_by_order('shop_contact',array('status'=>'1'),'id','desc') ;
		
		$data['product'] = $this->Db_model->get_where('shop_product',array('active'=>'Y'));
		$data['shipdetails'] = $ship_det = $this->Db_model->get_one('shop_shipdetails',array('order_id'=>$order_id));
		//$data['category'] = $this->Db_model->get_all_by_order('shop_category','order','asc') ;
     $data['area'] = $this->_getarea($ship_det->area) ;	



		$this->load->view('website/billing',$data);

		//$this->load->view('website/order_footer');

	}
	
	
	public function change_del_status()
	{
		
		if(isset($_POST['status']) && $_POST['status']!='' && isset($_POST['oid']) && $_POST['oid']!='')
		{
			$status = $_POST['status'];
			
			$order_id = $_POST['oid'];
			
			//change status			
			$this->Db_model->update('shop_shipdetails',array('status'=>$status),array('order_id'=>$order_id));
			
			
			
			/**
			*
			*	if status is confirmed mail to user
			*/
			
			if($status=='1')
			{
				$det = $this->Db_model->get_one('shop_shipdetails',array('order_id'=>$order_id));
				
				if($det!==FALSE)
				{
					$user_det = $this->Db_model->get_one('shop_checkoutusers',array('id'=>$det->user_id) );
					
					if($user_det!==FALSE)
					{
						$mailto = $user_det->email ;	
										
						$this->_send_confirm_mail($mailto,$order_id);
					}
				}
			}
			
			
			//redirect to order_admin page
			redirect('Order_admin');
		}
		
	}
	
		
	public function edit($order_id='')
	{
	
		//retrieve details from database matching to the id
		$details = $this->Db_model->get_one('shop_shipdetails',array('order_id'=>$order_id));
		
		if($details!==FALSE)
		{
			$order_det = $details ;
		}
		else
		{
			$order_det = array();
		}
		
		$data = array(
				'page_name' => 'Order' ,
				'heading' => 'Order' ,
				'top_link_name' => 'View' ,
				'top_link' => site_url('Order_admin/view') ,
				'order_det' => $order_det,
				'str' => ''
				);
		
		
		//check if submit button clicked
		if($this->input->post('submit'))
		{
				
				$p_data = array(
							   'track_det'=>$this->input->post('track_det',TRUE),
							   'status'=>$this->input->post('status',TRUE)
							   ) ;
				
				$order_id = $this->input->post('oid');
				
				$this->Db_model->update('shop_shipdetails',$p_data,array('order_id'=>$order_id));
				
				redirect('Order_admin') ;
		
		}//if($this->input->post('submit'))
		
		else
		{
			/**
			*
			*	show form at first time page loads,
			*	ie, before clicking submit button
			*
			*/
			
			$this->_page($data);
		}
		

	}
	
	private function _page($data=array())
	{
		$this->load->view('admin/home_header',$data);
		$this->load->view('admin/menu');
		/*$this->load->view('admin/top_links',$data);*/
		$this->load->view('admin/order_form',$data);
		$this->load->view('admin/footer');
	}
	
	
	public function delete($order_id='',$del=0)
	{
		if ($del==1)
		{	
		
			//delete all records in shop_order corresponding to this user
			$this->Db_model->delete('shop_order',array('order_id'=>$order_id));
			
			//delete all records in shop_shipdetails corresponding to this user
			$this->Db_model->delete('shop_shipdetails',array('order_id'=>$order_id));
			
			//redirect page to hide the id
			redirect('Order_admin/view') ;
		}
	}
	
	
	
	
		public function remove($order_id='',$del=0)

	{

		if ($del==1)

		{	

		


			$this->Db_model->update('shop_shipdetails',array('remove'=>$del,'status'=>"A"),array('order_id'=>$order_id));
			

			//redirect page to hide the id

			redirect('Order_admin/view') ;

		}

	}

	
	
	
	
	private function _getarea($area_id = '')
	{
		if($this->Db_model->get_one('shop_shipping',array('id'=>$area_id))!==FALSE)
		{
			$det = $this->Db_model->get_one('shop_shipping',array('id'=>$area_id));
			
			return $det->area;
		}
		else
		{
			return '';
		}
	}
	
	
	private function _send_confirm_mail($mailto='',$order_id='')
	{
		$this->new_pass = uniqid();
				
		$this->load->library('email');
		
		$config['charset'] = 'iso-8859-1';
		$config['mailtype'] = 'html';
		$config['wordwrap'] = TRUE;
		$username='';
		$address='';
		$placedon='';
		$this->email->initialize($config);

		$this->email->from('sale@edansemart.com', 'Edans e-mart');
		
		$this->email->to($mailto);
		
		$ship_det =$this->Db_model->get_one('shop_shipdetails',array('order_id'=>$order_id));
		
		$user_det = $this->Db_model->get_one('shop_checkoutusers',array('email'=>$mailto)) ;
		if( $user_det!==FALSE)
		{
			$user_mail = $user_det->email ; 
			$username=$user_det->first_name;
			$address=$user_det->address;
		}
		
		if( $ship_det!==FALSE)
		{
			$placedon = $ship_det->order_date ; 
			
		}
		
		$this->email->subject('Order Confirmed');
	
		$msg=' <div style="width:95%; padding:10px ; background-color:#E0E0E0;">
				
				<table width="100%"  border="0" cellpadding="0" cellspacing="0" align="center" bgcolor="#FBFBFB" 
				style="padding:20px;font-family:Arial, Helvetica, sans-serif; color:#2C2C2C; font-size:13px;">
				
				<tr><td colspan="2"><font style="font-size:18px;">Order Confirmed</font></td></tr>
				<tr height="20"><td colspan="2"></td></tr>
				<tr><td width="30%" height="30">
				
				<p>Hello '.$username.',</p>
				<p>Thank you for your order. This e-mail confirmed that we have received your order.</p>
				<p>If you would like to view the status of your order or make any changes to it, </p>
				<p>please visit Your Orders on Pookkani</p>
				<p>-------------------------------------------------------------------------------------------------</p>
				<p><b>Your order will be sent to: </b></p>
				<p>'.$username.'</p>
				<p>'.$address.'</p>
				<p><b>Order Details</b></p>
				<p>order ID: <b>'.$order_id.'</b></p>
				<p>Placed on '.$placedon.'</p>

				
				<p>
				Thank You,<br />
				Edans e-mart
				</p>
				
				</td></tr>
				
				<tr height="40"></tr>
				
				<tr><td colspan="2">
			
				<div style="font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#575757;">
				<p>This is a system generated message, Please do not reply to this email.</p>
				<p></p>
				<p></p>
				</div>
				
				</td></tr>
				</table>
				
				</div>    
				';		

		
		$this->email->message($msg);
		
		//email not sent
		if(! $this->email->send())
		{
			return false;			
		}
		else
		{
			return true;
		}
		
	}

	

	
}//end class

