<?php
session_start();
@$userKey = $_SESSION['loginKey'];

if($userKey == "")
{
    echo "<script>window.open('login.php','_self')</script>";
}


include "control/activity.php";
$dd = new activity();


$table = "userdetails";
$condition = " where userKey='$userKey'";
$getuserdetails = $dd->single($table,$condition);



$table = "admin_calculation";
$condition = null;
$getAdminDetails = $dd->single($table,$condition);
$fevicon = $getAdminDetails['favicon'];


$table = "gift_card";
$condition = " WHERE userKey_id='$userKey_id'";
$getgiftcard = $dd->single($table, $condition);
$amount = $getgiftcard['amount'];
$giftNumber = $getgiftcard['giftNumber']; 
$status = $getgiftcard['status'];
?>



<!DOCTYPE html>
<html lang="en" translate="no">
<head>
<meta charset="UTF-8">
<link rel="icon" type="image/svg+xml" href="temp_img/<?= $fevicon; ?>">
<meta name="google" content="notranslate">
<meta name="robots" content="noindex,nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title><?= $getAdminDetails['title']; ?></title>
<link rel="stylesheet" href="css/slick.css" />
<link rel="stylesheet" href="css/modules.css" />
<link rel="stylesheet" href="css/style.css" />
<script src="js/jquery.min.js" type="text/javascript"></script>
<script src="js/slick.min.js" type="text/javascript"></script>
<script src="js/main.js" type="text/javascript"></script>
</head>
<body>
    <div id="app">
        <div class="redeem-container">
            <div class="redeem-container-header">
                <div class="navbar">
                    <div class="navbar-fixed wc" style="background: linear-gradient(90deg, rgb(249, 90, 90) 0%, rgb(255, 153, 142) 100%);">
                        <div data-v-de39c3b8="" class="navbar__content">
                            <div data-v-905dfe8c="" class="navbar__content-left" onclick="clicktoBack()"><div data-v-905dfe8c="" class="backIcon"></div></div>
                            <div data-v-de39c3b8="" class="navbar__content-center"><div data-v-de39c3b8="" class="navbar__content-title">Gift</div></div><div data-v-de39c3b8="" class="navbar__content-right"></div></div>
                    </div>
                </div>
                <!-- redeem-container-header-belly -->
                <div class="redeem-container-header-belly">
                <img data-v-909e1547="" alt="" data-src="./images/gift-0e49be1a.png" src="./images/gift-0e49be1a.png" lazy="loaded">
                </div>


                <!-- redeem-container-header-belly -->
            </div>
<?php
  if($getuserdetails['gift_status'] == "success"){
    $btn = "disabled";
  }else{
    $btn = null;
  }

?>




            <div data-v-909e1547="" class="redeem-container-content">
                <div data-v-909e1547="" class="redeem-container-receive">
                    <p data-v-909e1547="">Hi</p>
                    <p data-v-909e1547="">We have a gift for you</p>
                    <h4 data-v-909e1547="">Please enter the gift code below</h4><form method="post">
                    <input required data-v-909e1547="" name="giftNumber" type="text" auto-complete="new-password" autocomplete="off" placeholder="Please enter gift code" />
                    <!---->
                    <button data-v-909e1547="" name="recvd">Receive</button></form>
                </div>

<?php
 if(isset($_REQUEST['recvd']))
 {
     $giftNumber = $_REQUEST['giftNumber'];
     $table = "funds_request";
     $condition = " where userKey_id='$userKey' AND remark like '%$giftNumber%'";
     $checkUsed = $dd->check($table,$condition);
     if($checkUsed['status'] != "success"){
     
     $table = "gift_card";
     $condition = " where giftNumber='$giftNumber'";
     $result = $dd->check($table,$condition);
     if($result['status'] == "success")
     {
        $numberOfCode = $result['data']['number_of_people'];
        $amount = $result['data']['amount'];
        if($numberOfCode > 0){
            $table = "userdetails";
            $value = "wallet=wallet+$amount,gift_status='success'";
            $condition = " userKey='$userKey'";
            $result = $dd->updateData($table,$value,$condition);
            if($result){
                $date = date("h:i d/m/Y");
                $ref = mt_rand(1111111111,9999999999);
                $table = "funds_request";
                $value = "(`userKey_id`, `request_date`, `request_type`, `amount`, `refrence_number`, `funds_status`, `approved_date`, `avl_balance`, `remark`) 
                SELECT '$userKey','$date','Gift','$amount','$ref','Approved','$date',wallet,'Gift Code-$giftNumber' FROM userdetails WHERE userKey='$userKey'";
                $result = $dd->insertData($table,$value);
                if($result){
                    $table = "gift_card";
                    $value = "number_of_people=number_of_people-1,total_used=total_used+1";
                    $condition = " giftNumber='$giftNumber'";
                    $result = $dd->updateData($table,$value,$condition);
                    if($result){
                        echo "<script>alert('Amount-$amount Receivd Successfully..!!'),window.open('index.php','_self')</script>";
                    }
                }
            }
        }
     }
     else{
        echo "<script>alert('Sorry Invalid code..!!')</script>";
     }
     
   }else{
        echo "<script>alert('Sorry Gift code Already used..!!')</script>";
     }
 }


?>

                <div data-v-909e1547="" class="redeem-container-record">
                    <div data-v-909e1547="" class="redeem-container-record-title"><img data-v-909e1547="" src="images/gRecord-5912132e.png" /><span data-v-909e1547="">History</span></div>
                    <div class="redeem_result">
                        <div class="gift_icon">
                            <img src="images/Gifts.png" alt="Gift">
                        </div>
                        <div class="redeem_info">
                            <span><?= $giftNumber;?></span>
                              <span><?= $getgiftcard['amount']; ?></span> 
                            <span><?= $getgiftcard['status'];?></span>
                        </div>
                    </div>
                                    <div data-v-909e1547="" class="redeem-container-record-itemsBox">
                        <div data-v-004096dd="" data-v-909e1547="" class="infiniteScroll" id="refresh99c06d5150834a7daebf0fa2d89d196a">
                            <div data-v-004096dd="" class="infiniteScroll__loading">
                                <!---->
                                <div data-v-91f2fefc="" data-v-004096dd="" class="empty__container empty">
                                    <img data-v-91f2fefc="" alt="" data-src="images/empty-4ac9a431.png" src="images/empty-4ac9a431.png" lazy="loaded" />
                                    <p data-v-91f2fefc="">No data</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</body>
<script>
  function clicktoBack()
  {
    window.open('index.php','_self');
  }
</script>
</html>