<?php session_start();
include("fresh_panel/inc/config.php");

if(isset($_POST['coupon'])){
     $coupon  = $_POST['coupon'];
     $final_total  = $_POST['final_total'];
    
    $statement5 = $pdo->prepare("SELECT * FROM tbl_coupon WHERE status = ? AND code=? ORDER BY coupon_id DESC");
    $statement5->execute(array(1,$coupon));
    $result5 = $statement5->fetchAll(PDO::FETCH_ASSOC);
        foreach ($result5 as $row5) {
            if($final_total>=$row5['min_amt']){
            $end_date = date('Ymd', strtotime($row5['end_date']));
            $date=date('Ymd');
                if($end_date>=$date){
                $coupon_id = $row5['coupon_id'];
                
                $statement5 = $pdo->prepare("SELECT * FROM applied_coupon WHERE coupon_id=? AND user_id=?");
                $statement5->execute(array($coupon_id,$_SESSION['customer']['cust_id']));
                    if($statement5->rowCount()>0) {
                    $total='';
                    }
                    else{
                    
                    $total=1;
                    $title = $row5['title'];
                    $coupon_code = $row5['code'];
                    $discount_percentage = $row5['percentage'];
                    }
                }
                else{
                $total='';
                }
            }
            else{
                $total='';
            }
        }

}


if($total){
    
    
      // response for user
    $c = $title.' '.$discount_percentage.'% Discount For Coupon Code -'.$coupon_code;  
    $_SESSION['discount_percentage2']=$discount_percentage;
    $myObj->succ = 1;
    $myObj->error = 0;
    $myObj->title = $title;
    $myObj->discount_percentage = $_SESSION['discount_percentage2'];
    $myObj->coupon_code = $coupon_code;
    $myObj->coupon_id = $coupon_id;
    $myObj->message = $c;
}
else{
    
    
      // response for user
      unset($_SESSION['discount_percentage']);
     $myObj->succ = 0;
     $myObj->error = 1;
    
    
}

$myJSON = json_encode($myObj);
    echo $myJSON;

     