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

class Userpage extends CI_Controller {
	

	private $cart_qty   = '' ;
	private $logged 	= 0  ;	
	private $user 		= '' ;
	

	public function __construct()
	{
		parent::__construct();
		
		$this->load->model(array('admin_model','Db_model'));
		
		$this->load->library(array('cart','user_loginfunctions'));
		
		$this->load->helper('basic_helper');
		
		$ic=0;
		foreach ($this->cart->contents() as $items):
		$ic++;
		
		endforeach;
		$this->cart_qty = ''.$ic.'';
		
		//if logged out, redirect to login page
		if($this->user_loginfunctions->checkloggedin()===FALSE)
		{
			//redirect('User/login');
		}
		
		if($this->session->userdata('shop_user2'))
		{
			$this->load->library('encrypt') ;
			$this->user = $this->encrypt->decode($this->session->userdata('shop_user2')); 
			$this->logged = 1;
		}
		
		if($_COOKIE['username_m'] !='' && $_COOKIE['password_m']!='' )
		{	
		if($this->user_loginfunctions->mobile_log($_COOKIE['username_m'],$_COOKIE['password_m']))
		{
			// echo $_COOKIE['username_m'];
			$this->load->library('encrypt') ;
			$this->user = $_COOKIE['username_m'];
			$this->logged = 1;
		}	
		}
		
		
		
		
	}
	
	public function index()
	{
		$this->home();
	}
	
	public function home()
	{
		$data['cart_qty'] = $this->cart_qty;
		$data['email'] = $this->user;
		$data['logged'] = $this->logged;
		$data['c_page'] = 'home';
				
		$this->_home_inc($data);
	}
	
	
	private function _home_inc($data=array())
	{
		
		
		$data['result'] = array() ;
		$data['details']=array();
		$data['cart_qty'] = $this->cart_qty;
		$data['email'] = $this->user;
		$data['logged'] = $this->logged;
		$data['page'] = 'about';
	//	$data['id'] = $id ;
		$data['result'] = $this->Db_model->get_all_by_order('shop_category','order','asc') ;
		$data['site'] = $this->Db_model->get_all('site',array()) ;
		$data['allproduct']= $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
		$data['category']=$data['result'] = $this->Db_model->get_where_by_order('shop_category',array('gift'=>1),'order','asc') ;	
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array('gift'=>2),'order','asc') ;
		
		
		$data['facility'] = $this->Db_model->get_where_by_order('shop_pages',array('type'=>'facility','active'=>'Y'),'order','desc') ;
		$data['service'] = $this->Db_model->get_where_by_order('shop_pages',array('type'=>'service','active'=>'Y'),'order','desc') ;
		$data['offer'] = $this->Db_model->get_where_by_order('shop_pages',array('type'=>'brand','active'=>'Y'),'order','desc') ;
		$data['branches'] = $this->Db_model->get_where_by_order('shop_pages',array('type'=>'branch','active'=>'Y'),'order','desc') ;
		//$data['category'] = $this->Db_model->get_all_by_order('shop_category','order','asc') ;
		$details = $this->Db_model->get_where_by_order('shop_pages',array('active'=>'Y','type'=>'about'),'order','asc') ; 
		$data['news'] = $this->Db_model->get_where_by_order('shop_pages',array('active'=>'Y','type'=>'news'),'order','asc') ;
		if($details!==FALSE)
		{
			$data['details'] = $details ;
		}
		
		
		
		$this->load->view('website/header',$data);
		$this->load->view('website/menu',$data);  
		$this->load->view('website/user_menu',$data);
		$this->load->view('website/user_home',$data);
		
		$this->load->view('website/footer');
		
		
		
	}
	
	
	public function shipaddress()
	{

		$data['cart_qty'] = $this->cart_qty;
		$data['email'] = $this->user;
		$data['logged'] = $this->logged;
		$data['error'] = '';
		$data['suc_msg'] = '';
		$data['c_page'] = 'shipaddress';
		$data['allproduct']= $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
		$data['category']=$data['result'] = $this->Db_model->get_where_by_order('shop_category',array('gift'=>1),'order','asc') ;	
		//$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array('gift'=>2),'order','asc') ;
		$data['site'] = $this->Db_model->get_all('site',array()) ;
		
		
	$data['post_list'] = $this->Db_model->get_where_by_order('delivery',array('type'=>'delivery','active'=>'Y'),'title','asc') ;
		
		
		//$data['category']=$data['result'] = $this->Db_model->get_all_by_order('shop_category','order','asc') ;	
		$data['subcate'] = $this->Db_model->get_all_by_order('shop_subcategory','id','desc') ;
		
 		$data['contactus'] = $this->Db_model->get_where_by_order('shop_contact',array('status'=>'1'),'id','desc') ;
		
	
		//$data['offer'] = $this->Db_model->get_all_by_order('shop_category','order','asc') ;
		if($this->Db_model->get_all_by_order('shop_shipping','area','asc')!==FALSE)
		{
			$area_det = $this->Db_model->get_all_by_order('shop_shipping','area','asc');
		}
		else
		{
			$area_det = array() ;
		}
		
		$data['area_det'] = $area_det;
		
		
		$this->load->library('form_validation');
		
		/*$data = array(
					  
				
				'location_id' => $this->input->post('location_id'),
				'location_result' => $location_result,
				'area' => $this->input->post('area'),
				'area_det' => $area_det,
				'logged' => $this->logged,
				'suc_msg' => '',
				'c_page' => 'profile',
				);*/
		
		
		
		$ship_det = $this->Db_model->get_one('shop_checkoutusers',array('id'=>$this->_userid(),'email'=>$this->user)) ;
		
		
		
		
	//print_r($ship_det);
		//$ship_det = $this->Db_model->get_all_by_order('shop_shipdetails','name','asc');
		
		
		//get details from databse for updation
		if($ship_det!==FALSE)
		{
			
		$data['first_name']=$ship_det->first_name;
		$data['address']=$ship_det->shaddress;
		$data['pin']=$ship_det->shpin;
		$data['area']=$ship_det->area;
		$data['landmark']=$ship_det->landmark;
		$data['city']=$ship_det->city;
		$data['state']=$ship_det->state;
		$data['tel']=$ship_det->tel;
		$data['mobile']=$ship_det->mobile;
		$data['name']=$ship_det->name;
		$data['shaddress']=$ship_det->shaddress;
		$data['shpin']=$ship_det->shpin;
		$data['shtel']=$ship_det->shtel;
		$data['shmobile']=$ship_det->shmobile;
		$data['area']=$ship_det->area;
		$data['landmark']=$ship_det->landmark;
		$data['gifts']=$ship_det->gifts;
		$data['post']=$ship_det->post;
		//$data['dtype']=$ship_det->dtype;
		//$data['giftmsg']=$ship_det->giftmsg;
		
		}
		
		//$this->_ship_inc($data);
		//check if submit button clicked
		if($this->input->post('submit'))
		{


$ddate=$_POST['ddate']; $dtime=$_POST['dtime']; $gifts=$_POST['gifts'];



if(function_exists('date_default_timezone_set')) {
date_default_timezone_set("Asia/Kolkata");
}



if($ddate==''){  $data['error'] = 'Pick a date' ;$this->_ship_inc($data);}
else{


			//make sure session is not empty
			if($this->cart->total_items())
			{ 
								
				//if there are form validation errors
				//if ($this->form_validation->run() === FALSE)
			//	{
					//$this->_ship_inc($data);
			//	}
				//else
			//	{ 
					/**
					*	save detials to `shop_shipdetails` and to `shop_order`	
					*/
					
					date_default_timezone_set('Asia/Kolkata');



					$order_id = $this->_maxorderid_plus_one(); 

					$user_id = $this->_userid() ; 
					
					$batch_det = array() ;
					
					//retrieve session details and save to `shop_order`
					foreach($this->cart->contents() as $item)
					{
						
						$prod_det = $this->Db_model->get_one('shop_product',array('aid'=>$item['id']));
						
						if($prod_det!==FALSE)
						{
							//$prod_cat = $prod_det->category_id;
							$prod_subcat = $prod_det->subcategory_id;
						}	
						else
						{
							//$prod_cat = 0;
							$prod_subcat = 0;
						}
							
							//echo $item['subtotal'];
							
							//if($item['subtotal']<1000.00){$finaltotal=$item['subtotal']+100;} else {$finaltotal=$item['subtotal'];} echo $finaltotal;
							
							
						$batch_det[] = array(
										  'order_id' => $order_id,
										  'user_id' => $user_id,
										  'prod_id' => $item['id'],
										  'prod_name' => $item['name'],
										 
										 // 'prod_cat' => $prod_cat,
										  'prod_subcat' => $prod_subcat,
										  'prod_price' => $item['price'],
										  'prod_qty' => $item['qty'],
										  'subtotal' => $item['subtotal']
										   ) ;
						
					}
					
					if($this->Db_model->insert_batch('shop_order',$batch_det)===TRUE)
						$order_inserted = 1;						
					else
						$order_inserted = 0;
						
						
						
					$stat = $this->Db_model->get_one('shop_shipping',array('id'=>$this->input->post('area'))) ;
					
					
					//if($item['subtotal']<1000.00){$final=100;} else {$final=0;} 
					
					if($stat!==FALSE)
					{
						$ship_charge = $stat->cost ;
					}
					else
					{
						$ship_charge = 0 ;
					}
					
					
					//save shipping details to `shop_shipdetails`
					$ship_det = array(
									  'order_id'=>$order_id,
									  'user_id'=>$user_id,
									  'name'=>$this->input->post('name',TRUE),
									  'address'=>$this->input->post('address',TRUE),
									  'pin'=>$this->input->post('pin',TRUE),
									  'address'=>$this->input->post('address',TRUE),
									  'landmark'=>$this->input->post('landmark',TRUE),
									  'city'=>$this->input->post('city',TRUE),
									  'state'=>$this->input->post('state',TRUE),
									 // 'area'=>$this->input->post('area',TRUE),
									  'tel'=>$this->input->post('tel',TRUE),
									  'mobile'=>$this->input->post('mobile',TRUE),
									  'ddate'=>$this->input->post('ddate',TRUE),
									  'dtime'=>$this->input->post('dtime',TRUE),
									  'order_date' => date('Y-m-d H:i:s'),
									  'gifts'=>$this->input->post('gifts',TRUE),
									  'post'=>$this->input->post('post',TRUE),
									  'dtype'=>$this->input->post('dtype',TRUE),
									 // 'giftmsg'=>$this->input->post('giftmsg',TRUE),
									'ship_charge' => $this->input->post('ship_charge',TRUE),
									  'status' => 'P' // P means pending
									  );
									  
									  
					$checkoutusers = array(
									  
									  'name'=>$this->input->post('name',TRUE),
									  'shaddress'=>$this->input->post('address',TRUE),
									  'shpin'=>$this->input->post('pin',TRUE),
									  'landmark'=>$this->input->post('landmark',TRUE),
									  'city'=>$this->input->post('city',TRUE),
									  'state'=>$this->input->post('state',TRUE),
									  'ddate'=>$this->input->post('ddate',TRUE),
									  'dtime'=>$this->input->post('dtime',TRUE),
									 // 'area'=>$this->input->post('area',TRUE),
									  'shtel'=>$this->input->post('tel',TRUE),
									  'shmobile'=>$this->input->post('mobile',TRUE),
									  'gifts'=>$this->input->post('gifts',TRUE),
									  'post'=>$this->input->post('post',TRUE),
									  // 'dtype'=>$this->input->post('dtype',TRUE),
									 // 'giftmsg'=>$this->input->post('giftmsg',TRUE),
									  
									  );				  
					              					
				
					if($this->Db_model->insert('shop_shipdetails',$ship_det)!==FALSE)
						$ship_inserted = 1;		
										
					else
						$ship_inserted = 0;	
						
						
					
					/**
					*	make sure details inserted to `shop_order` & `shop_shipdetails`
					*	else delete the inserted rows corresponding to order_id
					*/
					//echo $order_inserted; echo $ship_inserted;
					if($order_inserted==1 && $ship_inserted==1)	
					{
											
						$user_det = $this->Db_model->get_one('shop_checkoutusers',array('email'=>$this->user));
						
						if($user_det!==FALSE)
						{
							$customer_mail = $user_det->email ;
							
		
							
							
							//send mail to customer
							$cmail_status = $this->_send_ordermail($customer_mail,$order_id,'U');
						}

						//send mail to admin
						//$amail_status = $this->_send_ordermail('rahul@annvision.com',$order_id,'A'); 
						
						$amail_status = $this->_send_ordermail('hello@novelkart.com',$order_id,'A');
						
						
						//mail successfully sent to admin & customer
						if($cmail_status && $amail_status)
						{
							//empty cart
							
							
							
							
							
							
						$this->cart->destroy();
							
				            $this->Db_model->update('shop_checkoutusers',$checkoutusers,array('id'=>$this->_userid(),'email'=>$this->user));
							$data['suc_msg'] = 'Order details have been sent successfully.' ;
							
							$this->_ship_inc($data);
						
						//	$this->output->set_header('refresh:2;url=Payment'); 
						if($gifts==2) { redirect('Userpage/order_detailspay/'.$order_id.''); }
						else { redirect('Userpage/order_detailspay/'.$order_id.'');}
						}
						
						//problem on sending mail
						else
						{
							//delete data from shop_order corresponding to order_id
							//$this->Db_model->delete('shop_order',array('order_id'=>$order_id));
							
							//delete data from shop_order corresponding to order_id
							//$this->Db_model->delete('shop_shipdetails',array('order_id'=>$order_id));
						
							$data['error'] = 'Problem on sending mail, Try again.' ;
							
							$this->_ship_inc($data);
							
							
							
							
							
							
							
							
							
							
							
						$this->cart->destroy();
							
							
							
							
							
							
							
							
							
							
							
							
							$data['suc_msg'] = 'Novel: Order details has been sent successfully.' ;
							
							$this->_ship_inc($data);
						
							//$this->output->set_header('refresh:5;url=userpage/vieworders'); 
							//$this->output->set_header('refresh:2;url=Payment');
						
						if($gifts==2) { redirect('Userpage/order_detailspay/'.$order_id.''); }
						else { redirect('Userpage/order_detailspay/'.$order_id.'');}
						
						}
						
						 
					}
					
					
					/**
					*	if order details OR shipping details not inserted
					*/
					
					else
					{
						//delete data from shop_order corresponding to order_id
						$this->Db_model->delete('shop_order',array('order_id'=>$order_id));
						
						//delete data from shop_order corresponding to order_id
						$this->Db_model->delete('shop_shipdetails',array('order_id'=>$order_id));
						
						$data['error'] = 'Error occured try again.' ;
						
						$this->_ship_inc($data);
					}
					
					
				//}
			
			}
			else
			{
				$data['error'] = 'Your cart is empty .' ;
				$this->_ship_inc($data);
			}
			
		}}
		else
		{
			/**
			*
			*	show login form at first time page loads,
			*	ie, before clicking submit button
			*
			*/
			
			$this->_ship_inc($data);			
		}
		
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	private function _ship_inc($data=array())
	{
		$this->load->view('website/header',$data);
	
		$this->load->view('website/menu',$data);

		$this->load->view('website/shipping_form',$data);
		$this->load->view('website/footer');
	}
	
	private function _userid()
	{
		$user_det = $this->Db_model->get_one('shop_checkoutusers',array('email'=>$this->user));//print_r($user_det);
		if($user_det!==FALSE)
			return $user_det->id ;
		else
			return '';
	}
	
	private function _maxorderid_plus_one()
	{
		if($this->Db_model->max_orderid()!==FALSE)
		{
			$det = $this->Db_model->max_orderid();
			$order_id = $det->order_id + 1 ;
		}
		else
		{
			$order_id = 1 ;
		}
		
		return $order_id ;
	}
	
	
	private function _send_ordermail($mailto='',$order_id,$user='A')
	{
		
		$msg ='';
		
				$this->load->config('email');
        $this->load->library('email');
        
        $from = $this->config->item('smtp_user');
        $to = $this->input->post('to');
        $subject = $this->input->post('subject');
        $message = $this->input->post('message');

        $this->email->set_newline("\r\n");
        $this->email->from('info@asianstore.eu', 'Asian Spice Store : '.$order_id.'');
		//$this->email->fromname('Asian');
        $this->email->to($mailto);
        $this->email->subject('Order Details : '.$order_id.'');
		
		
		
		
		if($user=='A')
		{
			 $msg .= "<p>Dear Administrator,</p>
			 <p>An order has been placed. The details are given below:</p>
			 
			 
			 <p>&nbsp;</p>";
		}
		else if($user=='U')
		{
			 $msg .= "<p>Dear Customer,</p>
			 <p>An order has been placed by you. The details are given below:</p>
			
			 <p>&nbsp;</p>";
		}
		
		$total_amt = 0;
		
		$result = $this->Db_model->get_where('shop_order',array('order_id'=>$order_id,'user_id'=>$this->_userid()));
		
		$shipdetails = $this->Db_model->get_one('shop_shipdetails',array('order_id'=>$order_id));
		
		
		
		if($result!==FALSE)
		{
			
		$msg .= '

		<table width="95%" border="0" cellpadding="7" cellspacing="2" align="left" style="text-align:left; 
		font-family: Myriad Pro,Arial, Helvetica, sans-serif; font-size:13px;">
		
		<tr>
		<td colspan="2">Order ID : <b>'.$shipdetails->order_id.'</b></td>
		<td colspan="3" style="text-align:right;">Date : <b>'.get_datetime($shipdetails->order_date).'</b></td>
		</tr>  
		
		<tr bgcolor="#c89951" style="color:#FFF;">
			<td width="3%">No.</td>
			<td width="40%">Product Name</td>
			<td width="7%">Qty.</td>
			<td width="20%">Unit Price(Rs.)</td>
			<td width="30%">Sub Total(Rs.)</td>
		</tr>';
		  
		 $i=1;
		 
		 foreach($result as $row)
		 {
			$msg .=' 
			<tr bgcolor="#FAFAFA">
			<td>'.$i.'</td>
			<td>'.$row['prod_name'].'</td>
			<td>'.$row['prod_qty'].'</td>
			<td>'.$row['prod_price'].'</td>
			<td style="text-align:right;">'.$row['subtotal'].'</td>
			</tr>';
		
			$total_amt+=$row['subtotal'] ;
			$i++;
		 }
		 
		 $msg.='<tr bgcolor="#F5F5F5">
				<td colspan="4" align="center">Shipping Charge ('.$this->_getarea($shipdetails->area).')</td>
				<td style="text-align:right;">'.$shipdetails->ship_charge.'</td>
		 		</tr>';
		  
		 $total_amt+= $shipdetails->ship_charge ; 
		
		
		$msg.='<tr bgcolor="#F2F2F2" style="color:#0E0E0E;text-align:center;"><td colspan="4">Total Amount Payable</td>
		<td  style="text-align:right;">Rs.&nbsp;&nbsp;<b>'.$total_amt.'</b></td></tr> ';
		
		$msg.='<tr bgcolor="#F2F2F2" style="color:#0E0E0E;text-align:center;"><td colspan="4">Delivery Date & Time</td>
		<td  style="text-align:right;"><b>'.$shipdetails->ddate.','.$shipdetails->dtime.'</b></td></tr> ';

		
			$msg.'<tr bgcolor="#F5F5F5"> <td>The above will be delivered shortly.</td> </tr>';	
		$msg.='</table>' ;



		//show shipping details
		
		if($shipdetails!==FALSE)
		{
		
		/*$msg.='
		
		<table width="100%" border="0" cellpadding="7" cellspacing="2" align="center" 
		style="text-align:left; font-family:Myriad Pro,Arial, Helvetica, sans-serif; font-size:13px;">
		
		<tr height="30"><td colspan="4"></td></tr>
		
		<tr><td colspan="2" style="text-align:left; font-size:16px; font-weight:bold;"><u>Shipping Details</u></td></tr>
		
		<tr class="tr_2">
			<td width="30%">Deliver To</td>
			<td width="70%">: &nbsp;&nbsp;'.$shipdetails->name.'</td>
		</tr>
		
		<tr class="tr_2">
			<td>Address</td>
			<td>: &nbsp;&nbsp;'.$shipdetails->address.'</td>
		</tr>
		
		<tr class="tr_2">
			<td>Pincode</td>
			<td>: &nbsp;&nbsp;'.$shipdetails->pin.'</td>
		</tr>
		
		<tr class="tr_2">
			<td>Landmark</td>
			<td>: &nbsp;&nbsp;'.$shipdetails->landmark.'</td>
		</tr>
		
		<tr class="tr_2">
			<td>City</td>
			<td>: &nbsp;&nbsp;'.$shipdetails->city.'</td>
		</tr>
		
		<tr class="tr_2">
			<td>State</td>
			<td>: &nbsp;&nbsp;'.$shipdetails->state.'</td>
		</tr>
		
		<tr class="tr_2">
			<td>Area</td>
			<td>: &nbsp;&nbsp;'.$this->_getarea($shipdetails->area).'</td>
		</tr>
		
		<tr class="tr_2">
			<td>Telephone</td>
			<td>: &nbsp;&nbsp;'.$shipdetails->tel.'</td>
		</tr>
		
		<tr class="tr_2">
			<td>Mobile</td>
			<td>: &nbsp;&nbsp;'.$shipdetails->mobile.'</td>
		</tr>  
		
		<tr>
			<td colspan="2" height="30"></td>
		</tr>
		
		</table>
		
		';*/
		
		if($user=='A')
		{
		
			$prof_det = $this->Db_model->get_one('shop_checkoutusers',array('id'=>$shipdetails->user_id));
			
			if($prof_det!==FALSE)
			{
			
			$msg.='
			
			<table width="100%" border="0" cellpadding="7" cellspacing="2" align="center" 
			style="text-align:left; font-family:Myriad Pro,Arial, Helvetica, sans-serif; font-size:13px;">
			
			<tr><td colspan="2" style="text-align:left; font-size:16px; font-weight:bold;"><u>Customer Details</u></td></tr>
			
			<tr class="tr_2">
				<td width="30%">Name</td>
				<td width="70%">: &nbsp;&nbsp;'.$prof_det->name.' '.$prof_det->name.'</td>
			</tr>
			
			<tr class="tr_2">
				<td>Username</td>
				<td>: &nbsp;&nbsp;'.$prof_det->first_name.'</td>
			</tr>
			
			<tr class="tr_2">
				<td>Email</td>
				<td>: &nbsp;&nbsp;'.$prof_det->email.'</td>
			</tr>
			
			<tr class="tr_2">
				<td>Address</td>
				<td>: &nbsp;&nbsp;'.$prof_det->shaddress.'</td>
			</tr>
			
			<tr class="tr_2">
				<td>Pin</td>
				<td>: &nbsp;&nbsp;'.$prof_det->shpin.'</td>
			</tr>
			
			<tr class="tr_2">
				<td>Telephone</td>
				<td>: &nbsp;&nbsp;'.$prof_det->shtel.'</td>
			</tr>
			
			<tr class="tr_2">
				<td>Mobile</td>
				<td>: &nbsp;&nbsp;'.$prof_det->shmobile.'</td>
			</tr>
			
			<tr>
				<td colspan="2" height="30"></td>
			</tr>
	
			</table>
			';
			
			}// end if($prof_det!==FALSE)
		
		
		}// end if($user=='A')
		
	
		}//end if($shipdetails!==FALSE)
		
		
		$msg .= '<table width="100%" border="0" style="font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#666666;">
						
				<tr height="20"><td></td></tr>
				
				<tr>
					<td>
						<p>This is a system generated message.Please do not reply to this email.</p>
					</td>
				</tr>
				
				</table>
				
				';
		
		
		}//end if($result!==FALSE)
		

		
		
		$this->email->message($msg);
		
		//email not sent
		if(! $this->email->send())
		{
			return false;			
		}
		else
		{
			return true;
		}
		
	}
	
	
	
	
	
	public function vieworders()
	{
		$data['cart_qty'] = $this->cart_qty;
		$data['email'] = $this->user;
		$data['logged'] = $this->logged;
		$data['c_page'] = 'vieworders';
		$offer_products='';
		//$data['result'] = $this->Db_model->get_where_by_order_group('shop_order',array('user_id'=>$this->_userid()),'order_date','desc','order_id') ;
		//$data['category']=$data['result'] = $this->Db_model->get_all_by_order('shop_category','order','asc') ;
		
				$data['category']=$data['result'] = $this->Db_model->get_where_by_order('shop_category',array('gift'=>1),'order','asc') ;	
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array('gift'=>2),'order','asc') ;
			$data['allproduct']= $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
		$data['subcate'] = $this->Db_model->get_all_by_order('shop_subcategory','id','desc') ;
		$data['site'] = $this->Db_model->get_all('site',array()) ;
		$data['contactus'] = $this->Db_model->get_where_by_order('shop_contact',array('status'=>'1'),'id','desc') ;
		
		
		if($offer_products!==FALSE)
		{
			$data['offer'] = $offer_products;
		}
		else
		{
			$data['offer'] = array() ;
		}
		
		$data['result1'] = $this->Db_model->get_where_by_order('shop_shipdetails',array('user_id'=>$this->_userid()),'order_date','desc') ;
		//print_r($data['result1']);
		$this->_vieworders_inc($data);
	}
	
	private function _vieworders_inc($data=array())
	{
		$this->load->view('website/header',$data);
		
		$this->load->view('website/menu',$data);
		//$this->load->view('website/user_menu',$data);
		$this->load->view('website/view_orders',$data);
		$this->load->view('website/footer');
		
		
	}
	
	
	public function userdata()
	{
		$data['cart_qty'] = $this->cart_qty;
		$data['email'] = $this->user;
		$data['logged'] = $this->logged;
		$data['c_page'] = 'vieworders';
		$emaills=$this->user;
		
		//$data['result'] = $this->Db_model->get_where_by_order_group('shop_order',array('user_id'=>$this->_userid()),'order_date','desc','order_id') ;
		
		$data['result'] = $this->Db_model->get_where_by_order('shop_pages',array('type'=>'about','active'=>'Y'),'order','desc') ;
		$data['site'] = $this->Db_model->get_all('site',array()) ;
		$data['category']=$offer_products = $this->Db_model->get_all_by_order('shop_category','order','asc') ;
		
		//$data['contactus'] = $this->Db_model->get_where_by_order('shop_contact',array('status'=>'1'),'id','desc') ;
		
		
		
		if($offer_products!==FALSE)
		{
			$data['offer'] = $offer_products;
		}
		else
		{
			$data['offer'] = array() ;
		}
		
		$user_det = $this->Db_model->get_one('shop_checkoutusers',array('email'=>$emaills));
					/*if($this->Db_model->get_where_by_order('shop_shipdetails',array('user_id'=>$user_det->id),'order_date','desc'))
					{*/
			$dataret= $this->Db_model->get_where_by_order('shop_shipdetails',array('user_id'=>$user_det->id),'order_date','desc') ;
					//}
					
					
					//$user_id = $this->_userid() ;echo $user_id;
				//	print_r($dataret);
					// redirect to home page
					if($this->cart_qty!=0)
					{
					redirect('Userpage/shipaddress');	
					}
					if($dataret!='')
					{
					redirect('Userpage/vieworders');		
					}
					else
					{
						redirect('Product/categorys');
					}
		
		
		
		
	}
	
	/*private function _userdata_inc($data=array())
	{
		$this->load->view('website/header',$data);
		$this->load->view('website/menu',$data);
		$this->load->view('website/view_orders',$data);
		$this->load->view('website/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('gift'=>1),'order','asc') ;	
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array('gift'=>2),'order','asc') ;
$data['allproduct']= $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
		$data['site'] = $this->Db_model->get_all('site',array()) ;
		$data['result2'] = $this->Db_model->get_where('shop_order',array('order_id'=>$order_id,'user_id'=>$this->_userid()));
		
		$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) ;		
	 
	 
		$final_report="erghaeth";
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 	if($this->input->post('submit'))
		{
		
	/*	echo $key=$_POST['key'];
		echo $hash=$_POST['hash'];
		echo $txnid=$_POST['txnid'];
		echo $amount=$_POST['amount'];
		echo $firstname=$_POST['firstname'];*/
		
// echo $final_report="The System is redirecting... Please wait a few seconds. You will be redirected automatically.";
		
		
		}
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	
		$this->load->view('website/header',$data);
		//$this->load->view('website/menu',$data); 
		$this->load->view('website/order_details',$data);
		// $this->load->view('website/footer');
	}
	
	
	
	
	public function order_detailspay($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('gift'=>1),'order','asc') ;	
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array('gift'=>2),'order','asc') ;
$data['allproduct']= $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
		$data['site'] = $this->Db_model->get_all('site',array()) ;
		$data['result2'] = $this->Db_model->get_where('shop_order',array('order_id'=>$order_id,'user_id'=>$this->_userid()));
		
		$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) ;		
	 
	 
		$final_report="erghaeth";
	 

	 
	 	if($this->input->post('submit'))
		{
			
		}
	 

	 
	 
	
		$this->load->view('website/header',$data);
		$this->load->view('website/menu',$data); 
		$this->load->view('website/order_detailspay',$data);
		 $this->load->view('website/footer');
	}
	

	public function profile()
	{
		
		
	
		$first_name='';
		$last_name='';
		$address='';
		$pin='';
		$tel='';
		$mobile='';
		$username='';
		$email='';
	    $location_id='';
		
		
		$this->load->library('form_validation') ;
		if($this->Db_model->get_all_by_order('shop_shipping','area','asc')!==FALSE)
		{
			$area_det = $this->Db_model->get_all_by_order('shop_shipping','area','asc');
		}
		else
		{
			$area_det = array() ;
		}
		
		//$data['area_det'] = $area_det;
		
	    if($this->Db_model->get_all_by_order('shop_location','location','asc') === FALSE )
		
		$location_result = array() ;
		
		else
		$location_result = $this->Db_model->get_all_by_order('shop_location','location','asc') ;
		
		$data = array(
				'cart_qty'=>$this->cart_qty,	  
				'email' => $this->user,
				'location_id' => $this->input->post('location_id'),
				'location_result' => $location_result,
				'area' => $this->input->post('area'),
				'area_det' => $area_det,
				'logged' => $this->logged,
				'suc_msg' => '',
				'c_page' => 'profile',
				);
		
		$user_det = $this->Db_model->get_one('shop_checkoutusers',array('id'=>$this->_userid(),'email'=>$this->user)) ;
		$data['result1'] = $this->Db_model->get_where_by_order('shop_shipdetails',array('user_id'=>$this->_userid()),'order_date','desc') ;
		$data['site'] = $this->Db_model->get_all('site',array()) ;
		$data['contactus'] = $this->Db_model->get_where_by_order('shop_contact',array('status'=>'1'),'id','desc') ;
		$data['category']=$this->Db_model->get_all_by_order('shop_category','order','asc') ;
		$data['post_list'] = $this->Db_model->get_where_by_order('shop_pages',array('type'=>'about','active'=>'Y'),'title','asc') ;
		
		//get details from databse for updation
		if($user_det!==FALSE)
		{
			$data['first_name']=$user_det->first_name;
			$data['last_name']=$user_det->last_name;
			$data['address']=$user_det->address;
			$data['pin']=$user_det->pin;
			$data['tel']=$user_det->tel;
			$data['mobile']=$user_det->mobile;
			$data['username']=$user_det->username;
			$data['email']=$user_det->email;
			$data['location']=$user_det->location;
			$data['name']=$user_det->name;
			$data['shaddress']=$user_det->shaddress;
			$data['shpin']=$user_det->shpin;
			$data['landmark']=$user_det->landmark;
			$data['city']=$user_det->city;
			$data['state']=$user_det->state;
			$data['area']=$user_det->area;
			$data['shtel']=$user_det->shtel;
			$data['shmobile']=$user_det->shmobile;
			$data['post']=$user_det->post;
			//$data['pcustomer']=$user_det->pcustomer;
		}
		
		//check if submit button clicked
		if($this->input->post('update'))
		{

		
		   
		    $this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
			$this->form_validation->set_rules('address', 'Address', 'trim|required');
			$this->form_validation->set_rules('mobile', 'Mobile Number', 'trim|required|min_length[10]|max_length[12]|numeric');
			
			
			/**
			*	$p_data for updating details in database	
			*	$data for displaying details in webpage
			*/

			$p_data['first_name'] = $data['first_name']=$this->input->post('first_name');
			$p_data['address'] = $data['address']=$this->input->post('address');
            $p_data['mobile'] = $data['mobile']=$this->input->post('mobile');
			$p_data['tel'] = $data['tel']=$this->input->post('tel');
			

			if ($this->form_validation->run() === FALSE)
			{
				$this->_editprofile_inc($data); 
			}
			else
			{
				//do updation

				$insert_id = $this->Db_model->update('shop_checkoutusers',$p_data,array('id'=>$this->_userid(),'email'=>$this->user));
				
				if($insert_id!==FALSE)
				{
					//update details 
					
					$data['suc_msg'] = 'Details have been updated successfully.' ;
					
					$this->_editprofile_inc($data);	
					
					$this->output->set_header('refresh:3;');  
					redirect('Customer/dashboard');
				}
				else
				{
					//not inserted
					$this->_editprofile_inc($data);	
				}
			}
			
			
		}
		else
		{
			/**
			*
			*	show login form at first time page loads,
			*	ie, before clicking submit button
			*
			*/
			
			$this->_editprofile_inc($data);			
			
		}
	}
	
	
	private function _editprofile_inc($data=array())
	{   
	
	$data['result'] = array() ;
		$data['details']=array();
		$data['cart_qty'] = $this->cart_qty;
		$data['email'] = $this->user;
		$data['logged'] = $this->logged;
		$data['page'] = 'about' ;
	//	$data['id'] = $id ;
		//$data['category']=$data['result'] = $this->Db_model->get_all_by_order('shop_category','order','asc') ;
		$data['category']=$data['result'] = $this->Db_model->get_where_by_order('shop_category',array('gift'=>1),'order','asc') ;	
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array('gift'=>2),'order','asc') ;
		$data['site'] = $this->Db_model->get_all('site',array()) ;
		
		$data['allproduct']= $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
			
		$data['subcate'] = $this->Db_model->get_all_by_order('shop_subcategory','id','desc') ;
		
		
		
		
		
		$this->load->view('website/header',$data);
		$this->load->view('website/menu',$data);
		$this->load->view('website/edit_profile',$data);
		$this->load->view('website/footer');
	}
	
	
	
	public function changepassword()
	{
		$password='';
		$vpassword='';
		
	
		$this->load->library('form_validation') ;
		
		$user_det = $this->Db_model->get_one('shop_checkoutusers',array('id'=>$this->_userid(),'username'=>$this->user)) ;
	
		$data = array(
				'cart_qty'=>$this->cart_qty,	  
				'email' => $this->user,
				'logged' => $this->logged,
				'suc_msg' => '',
				'username' => $user_det,
				'c_page' => 'changepassword'
				);
		
	$data['result'] = $this->Db_model->get_all_by_order('shop_category','order','asc') ;	
		$data['subcate'] = $this->Db_model->get_all_by_order('shop_subcategory','id','desc') ;
		
		$data['offer'] = $this->Db_model->get_all_by_order('shop_category','order','asc') ;
		
		//check if submit button clicked
		if($this->input->post('change_pass'))
		{
		
			$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|max_length[15]');
			$this->form_validation->set_rules('vpassword', 'Confirm Password', 'trim|required|matches[password]|min_length[6]|max_length[15]');
			//$this->form_validation->set_rules('captext', 'Security Text', 'trim|required|xss_clean|callback_captcha_check');
			
			
			/**
			*	$p_data for updating details in database	
			*	$data for displaying details in webpage
			*/
			$p_data['password'] = $data['password']=$this->input->post('password');
		
			//if there are form validation errors
			if ($this->form_validation->run() === FALSE)
			{
				$this->_pass_inc($data);
			}
			else
			{
				//do updation

				$insert_id = $this->Db_model->update('shop_checkoutusers',$p_data,array('id'=>$this->_userid(),'email'=>$this->user));
				
				
				
				
				
				
				
				
				
				
									
$isWebView = false;
if((strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile/') !== false) && (strpos($_SERVER['HTTP_USER_AGENT'], 'Safari/') == false)) :
    $isWebView = true;
elseif(isset($_SERVER['HTTP_X_REQUESTED_WITH'])) :
    $isWebView = true;
endif;

if(!$isWebView) : 
    // Browserview
	
			
	
	
else :
    // Webview
		setcookie("password_m", '', 1, '/');
		setcookie('password_m',$this->input->post('password'),time() + (60 * 60* 24 * 365 * 10),'/'); // 86400 = 1 day
endif;	
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				if($insert_id!==FALSE)
				{
					//update details 
					
					$data['suc_msg'] = 'Password has been changed successfully.' ;
					
					$this->_pass_inc($data);	
					
					$this->output->set_header('refresh:3;url='. site_url().'');  
					//redirect('Product/categorys');
				}
				else
				{
					//not inserted
					$this->_pass_inc($data);	
				}
			}
			
			
		}
		else
		{
			/**
			*
			*	show login form at first time page loads,
			*	ie, before clicking submit button
			*
			*/
			
			$this->_pass_inc($data);			
			
		}
	}
	
	
	private function _pass_inc($data=array())
	{$data['result'] = array() ;
		$data['details']=array();
		$data['cart_qty'] = $this->cart_qty;
		$data['email'] = $this->user;
		$data['logged'] = $this->logged;
		//$data['result'] = $this->Db_model->get_all_by_order('shop_category','order','asc') ;
		
		
		$data['category']=$data['result'] = $this->Db_model->get_where_by_order('shop_category',array('gift'=>1),'order','asc') ;	
		$data['gift'] = $this->Db_model->get_where_by_order('shop_category',array('gift'=>2),'order','asc') ;
		$data['site'] = $this->Db_model->get_all('site',array()) ;
		$data['allproduct']= $this->Db_model->get_where_by_order('shop_product',array('active'=>'Y'),'order','asc') ;
		$data['contactus'] = $this->Db_model->get_where_by_order('shop_contact',array('status'=>'1'),'id','desc') ;
		
		
		
		$this->load->view('website/header',$data);
		
		$this->load->view('website/menu',$data); 
		//$this->load->view('website/user_menu',$data); 
		$this->load->view('website/change_password',$data);
		$this->load->view('website/footer');
		
	}
	
	
	
	
	
	
	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 '';
		}
	}
	
	
	
	public function payment_response_update($order_id='',$stata='')
	{
	
	
	if($order_id!='' && $stata!=''){
    $data = array('payment' => $stata);
//update data
$this->Db_model->update('shop_shipdetails',$data,array('order_id'=>$order_id)) ;

redirect('Userpage/vieworders');
	
	
	}
	
	
	
	
	}
	
	
	public function payment_response()
	{
	
	$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['site'] = $this->Db_model->get_all('site',array()) ;

//echo $email='majo@annvision.com'; $status='success';  $productinfo='0000000022'; //
           /*    $email=$_POST["email"];  $status=$_POST["status"]; $productinfo=$_POST["productinfo"];
			   if($email!='' && $status=='success' )
			   {send mail to customer
			   $cmail_status = $this->_send_ordermail($email,$productinfo,'U');
			   }*/
			  


	 
				//send mail to customer
				//$cmail_status = $this->_send_ordermail($customer_mail,$order_id,'U');


		$this->load->view('website/header',$data);
		$this->load->view('website/menu',$data); 
		$this->load->view('website/payment',$data);
		$this->load->view('website/footer');
	}
	

	
	
}
