<?php session_start();
include("../include/config.php");
include("../include/functions.php"); 
validate_admin();
$arr =$_POST['ids'];
//print_r($_REQUEST);

$Submit =$_POST['what'];

if(count($arr)>0){
	$str_rest_refs=implode(",",$arr);
	if($Submit=='Delete')
	{  
	    $sql="delete from $tbl_buygiftcard where id in ($str_rest_refs)"; 
		$obj->query($sql);
		$sess_msg='Selected record(s) deleted successfully';
		$_SESSION['sess_msg']=$sess_msg;
    }
	elseif($Submit=='Approve')
	{	
	    $recArr=$obj->query("select * from $tbl_buygiftcard where id in ($str_rest_refs) and status!=1 ",$debug=-1);
		while($rsRec=$obj->fetchNextObject($recArr)){
		  $uArr=$obj->query("select id from $tbl_user where email='".$rsRec->receiver_email."' and user_type=0 ");	
		  if($obj->numRows($uArr)>0){
		    $rsUser=$obj->fetchNextObject($uArr);
			
			$setting_reward=getField('rewardamount',$tbl_setting,1);
			$reward_point=floor($rsRec->gift_amount/$setting_reward);
			$my_reward_point = $reward_point+$rsRec->gift_amount;
			
			$obj->query("insert into  $tbl_reward_history set user_id='".$rsUser->id."',reward_point='".$my_reward_point."',type='Cr',added_date=now(),status=1  ");
			
			$sql="update $tbl_buygiftcard set alloted=1,status=1 where id ='".$rsRec->id."'";
			$obj->query($sql);
		  
		  }
		
		$FromTo=getAdminEmail();
		$subject="Gift Cart Approval ".($rsUser->fname." ".$rsUser->lname)."";
		$headers = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
		// More headers
		$headers .= "From: <".$FromTo.">\r\n";
				
		$message="<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
		<html xmlns='http://www.w3.org/1999/xhtml'>
		<head>
		<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' />
		</head>
		<body>
		<table width='500' border='0' cellpadding='3' cellspacing='0'>
			<tbody>
				<tr>
					<td  valign='top' colspan='3'>Dear User,</td>
				</tr>
				<tr>
					<td  valign='top' colspan='3'>Your Gift Card has been successfully activated from ".SITE_TITLE."</td>
				</tr>
				<tr>
					<td  valign='top' colspan='3'>Please find the details below:</td>
				</tr>
				<tr>
					<td  valign='top' colspan='3'>&nbsp;</td>
				</tr>
								
		
				<tr>
					<td valign='top'><strong>Gift Amount</strong></td>
					<td valign='top'>:</td>
					<td valign='top'> Rs. ".$rsRec->gift_amount."</td>
				</tr>
				
				
				<tr>
					<td  valign='top' colspan='3'>&nbsp;</td>
				</tr>
				
				<tr>
					<td  valign='top' colspan='3'>Thanks</td>
				</tr>
				<tr>
					<td  valign='top' colspan='3'>".SITE_TITLE."</td>
				</tr>
			</tbody>
		</table>
		</body>
		</html>";
	
		@mail($rsRec->receiver_email,$subject,$message,$headers);
		  
		  
		}
		$sql="update $tbl_buygiftcard set status=1,payment_status=1 where id in ($str_rest_refs)";
		$obj->query($sql);
		
		
		
		 
		
		
		$sess_msg='Selected record(s) activated successfully';
		$_SESSION['sess_msg']=$sess_msg;
	}
	elseif($Submit=='Disapprove')
	{		
	    $recArr=$obj->query("select * from $tbl_buygiftcard where id in ($str_rest_refs) and status!=0 ");
		while($rsRec=$obj->fetchNextObject($recArr)){
		  $uArr=$obj->query("select id from $tbl_user where email='".$rsRec->receiver_email."' and user_type=0 ");	
		  if($obj->numRows($uArr)>0){
		    $rsUser=$obj->fetchNextObject($uArr);
			if($rsRec->alloted==1){
				
			$setting_reward=getField('rewardamount',$tbl_setting,1);
			$reward_point=floor($rsRec->gift_amount/$setting_reward);
			$my_reward_point = $reward_point+$rsRec->gift_amount;
			$obj->query("insert into  $tbl_reward_history set user_id='".$rsUser->id."',reward_point='$my_reward_point',type='Dr',added_date=now(),status=1  ");
			$sql="update $tbl_buygiftcard set alloted=0,status=0 where id ='".$rsRec->id."'";
			$obj->query($sql);
			}
		  }
		}
		$sql="update $tbl_buygiftcard set status=0 where id in ($str_rest_refs)";
		$obj->query($sql);
		$sess_msg='Selected record(s) deactivated successfully';
		$_SESSION['sess_msg']=$sess_msg;
	}
		
	}	
else{
	$sess_msg="Please select check box";
	$_SESSION['sess_msg']=$sess_msg;
	header("location: ".$_SERVER['HTTP_REFERER']);
	exit();
	}
	header("location: buygiftcard-list.php");
	exit();
	
?>
