<?php
include 'include/db.php';
date_default_timezone_set('Asia/Kolkata');      
$date=date("Y-m-d H:i");      //  for 24 hour
////    coupon categroy add
if(isset($_REQUEST['add_category']))
{
    $query="insert into coupon_category(coupon_category_name,coupon_category_des) values('".$_REQUEST['category_name']."','".$_REQUEST['category_description']."')";
    $connection=mysqli_query($conn,$query);
    if($connection)
    {
        echo '<script> alert("Coupons category added successfully"); window.location="manage_category.php"; </script>';
    }
    else
    {
        echo '<script> alert("Sorry, something went wrong"); window.location="manage_category.php"; </script>';
    }
}
////   coupon user id check
if(isset($_REQUEST['coupons_user_id']))
{
    $id=$_REQUEST['user_id'];
    $qry="select * from registration where unique_user_id='$id' ";
    $con1=mysqli_query($conn,$qry);
    if(mysqli_num_rows($con1)>0)
    {
        while($rows=mysqli_fetch_array($con1))
        {
            echo $rows['user_name'];
        }
    }
    else
    {
        echo 'err';
    }
}
////    coupon create
if(isset($_REQUEST['generate']))
{
    $qry2="insert into coupon(category_coupon_id,coupon_user_id,coupon_amount,coupon_code,coupon_status,coupon_crt_date,coupon_mdf_date)
            values('".$_REQUEST['coupon_category']."','".$_REQUEST['user_id']."','".$_REQUEST['amount']."','".$_REQUEST['coupon_code']."',0,'$date','')";
    $con2=mysqli_query($conn,$qry2);
    if($con2)
    {
        echo '<script> alert("Coupon generated successfully"); window.location="manage_coupon.php"; </script>';
    }
    else
    {
         echo '<script> alert("sorry soemthing went wrong"); window.location="generate_coupons.php"; </script>';
    }
}
////    coupon delete
if(isset($_REQUEST['key']))
{
    $id=$_REQUEST['id'];
    $qry3="delete from coupon where coupon_incr_id='$id' ";
    $con3=mysqli_query($conn,$qry3);
    if($con3)
    {
        echo '<script> alert("Coupon deleted successfully"); window.location="manage_coupon.php"; </script>';
    }
    else
    {
        echo '<script> alert("sorry, something went wrong"); window.location="manage_coupon.php"; </script>';
    }
    
}
////   coupon category edit
if(isset($_REQUEST['save']))
{
    $qry4="update coupon_category set coupon_category_name='".$_REQUEST['category_name']."',coupon_category_des='".$_REQUEST['category_description']."'
            where coupon_cat_id='".$_REQUEST['append_id']."' ";
    $con4=mysqli_query($conn,$qry4);
    if($con4)
    {
        echo '<script> alert("Coupon category updated successfully"); window.location="manage_category.php"; </script>';
    }
    else
    {
        echo '<script> alert("Sorry, soemthing went wrong"); window.location="manage_category.php"; </script>';
    }
}
///    coupon category delete
if(isset($_REQUEST['dlt_key']))
{
    $qry5="delete from coupon_category where coupon_cat_id='".$_REQUEST['id']."' ";
    $con5=mysqli_query($conn,$qry5);
    if($con5)
    {
         echo '<script> alert("Coupon category deleted successfully"); window.location="manage_category.php"; </script>';
    }
    else
    {
         echo '<script> alert("Sorry, seomething went wrong"); window.location="manage_category.php"; </script>';
    }
}



?>