<?php
header('Access-Control-Allow-Origin: *');
header(
    'Access-Control-Allow-Methods: HEAD, GET, POST, PUT, PATCH, DELETE, OPTIONS'
);
header(
    'Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method,Access-Control-Request-Headers, Authorization'
);
header('Content-Type: application/json');
$method = $_SERVER['REQUEST_METHOD'];
if ($method == 'OPTIONS') {
    header('Access-Control-Allow-Origin: *');
    header(
        'Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method,Access-Control-Request-Headers, Authorization'
    );
    header('HTTP/1.1 200 OK');
    die();
}

include '../../../../config/db.php';
$data = [];
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    $query = 'SELECT * FROM Coupon WHERE isDeleted =0';
    if ($result = mysqli_query($con, $query)) {
        $i = 0;
        while ($rows = mysqli_fetch_assoc($result)) {
            $data[$i]['CouponId'] = $rows['CouponId'];
            $data[$i]['CouponCode'] = $rows['CouponCode'];
            $data[$i]['Discount'] = $rows['Discount'];
            $data[$i]['IsPercentage'] = $rows['IsPercentage'];
            $data[$i]['MaxUsage'] = $rows['MaxUsage'];
            $data[$i]['ActiveCouponDate'] = $rows['ActiveCouponDate'];
            $data[$i]['MinAmount'] = $rows['MinAmount'];
            $data[$i]['CouponDescription'] = $rows['CouponDescription'];
            $data[$i]['ExpireDate'] = $rows['ExpireDate'];
            $data[$i]['CreatedDate'] = $rows['CreatedDate'];

            $i++;
        }
        echo json_encode($data);
    }
} else {
    echo json_encode([
        'status' => -1,
    ]);
    die();
}
