<?php

require 'db.php';

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
if(isset($postdata) && !empty($postdata))
{
$cid = mysqli_real_escape_string($con, trim($request->c_id));
$mode = mysqli_real_escape_string($con, trim($request->mode));

$cname = mysqli_real_escape_string($con, trim($request->form_value->c_name));
$code = mysqli_real_escape_string($con, trim($request->form_value->code));
$discount = mysqli_real_escape_string($con, trim($request->form_value->discount));
$maxdisamount = mysqli_real_escape_string($con, trim($request->form_value->max_discount_amount));
$maxorderamount = mysqli_real_escape_string($con, trim($request->form_value->max_order_amount));
$maxuselimit = mysqli_real_escape_string($con, trim($request->form_value->max_use_limit));
$image = 'banner/ap_logo.png';
$desc = 'test';

if($mode == "add")
{
    if($cname == "" || $code == "" || $maxdisamount == "" || $maxorderamount == "" || $maxuselimit == "")
    {
        $rows = array('status'=>false,'message' => 'Fields Missing');
        echo json_encode($rows);
    }
    else
    {
        $date = date("Y-m-d H:i:s");
        if($discount == "")
        {
            $discount = 0;
        }
        
        $sql = "INSERT INTO coupon_code(name,image,discount,code,description,max_order_amount,max_discount_amount,max_use_limit,status,timestamp) 
        VALUES ('$cname','$image','$discount','$code','$desc','$maxorderamount','$maxdisamount','$maxuselimit','1','$date')";
        $result = mysqli_query($con,$sql);
        if($result)
        {
            $rows = array('status'=>true,'message' => 'New Coupon Added');
            echo json_encode($rows);
        }
        else
        {
            $rows = array('status'=>false,'message' => 'Coupon Error');
            echo json_encode($rows);
            
        }
        
    }
}

else if($mode == "edit")
{
    if($cname == "" || $code == "" || $maxdisamount == "" || $maxorderamount == "" || $maxuselimit == "" || $cid == "")
    {
        $rows = array('status'=>false,'message' => 'Fields Missing');
        echo json_encode($rows);
    }
    else
    {
        if($discount == "")
        {
            $discount = 0;
        }
        $sql = "UPDATE coupon_code set name = '$cname',code='$code',discount='$discount',max_discount_amount='$maxdisamount',max_order_amount='$maxorderamount',max_use_limit='$maxuselimit' where id='$cid'";
        $result = mysqli_query($con,$sql);
        if($result)
        {
            $rows = array('status'=>true,'message' => 'Coupon Updated');
            echo json_encode($rows);
        }
        else
        {
            $rows = array('status'=>false,'message' => 'No Coupon Available');
            echo json_encode($rows);
            
        }

    }
    
}

else if($mode == "delete")
{
    if($cid == "")
    {
        $rows = array('status'=>false,'message' => 'Coupon Error');
        echo json_encode($rows);
    }
    else
    {
        $sql = "DELETE FROM coupon_code where id ='$cid'";
        $result = mysqli_query($con,$sql);
        if($result)
        {
            $rows = array('status'=>true,'message' => 'Coupon Deleted');
            echo json_encode($rows);
        }
        else
        {
            $rows = array('status'=>false,'message' => 'No Coupon Available');
            echo json_encode($rows);
            
        }
    }
}
}
else
{
    $rows = array('status'=>false,'message' => 'Something Error');
    echo json_encode($rows);
}

?>
