<?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));

$shop_id = mysqli_real_escape_string($con, trim($request->shop_id));
$shop_code = mysqli_real_escape_string($con, trim($request->shop_code));

include 'token_check.php';

if($token_check)
{

    $s_sql = "SELECT shop_logo FROM shop_details WHERE shop_id='$shop_id' and shop_code='$shop_code'";
    $s_result = mysqli_query($con,$s_sql);
    if($row = $s_result->fetch_assoc())
    {
        $image = $row['shop_logo'];
    }
    $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(shop_id,shop_code,name,image,discount,code,description,max_order_amount,max_discount_amount,max_use_limit,status,timestamp) 
            VALUES ('$shop_id','$shop_code','$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' and shop_id='$shop_id' and shop_code='$shop_code'";
            $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' and shop_id='$shop_id' and shop_code='$shop_code'";
            $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' => 'Data Error');
    echo json_encode($rows);
}
}

?>
