<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
include '../../../config/index.php';
$data = array();
$sql1 = "SELECT * FROM Coupons ";

$result1 = mysqli_query($conn, $sql1);
if (mysqli_num_rows($result1) > 0) {
    while ($row1 = mysqli_fetch_assoc($result1)) {
        $Name = "";
        $Email = "";
        $Image = "assets/img/fav.png";
        $PhoneNUmber = "";
        $RedemptionDate = "";
        $User = "";
        $sql_spl = "SELECT * FROM RedeemedCoupons WHERE Coupon_id='" . $row1['Coupon_id'] . "'  AND  User='Parent' ";
        $result_spl = mysqli_query($conn, $sql_spl);
        if (mysqli_num_rows($result_spl) > 0) {
            while ($row_spl = mysqli_fetch_assoc($result_spl)) {
                $sql_civ = "SELECT * FROM parents WHERE parent_id='" . $row_spl['User_id'] . "' ";
                $result_civ = mysqli_query($conn, $sql_civ);
                if (mysqli_num_rows($result_civ) > 0) {
                    while ($row_civ = mysqli_fetch_assoc($result_civ)) {
                        $Name = $row_civ["p_name"] . " " . $row_civ["p_last_name"];
                        $Email = $row_civ["p_email"];
                        $Image = $row_civ["p_image"];
                        $PhoneNUmber = $row_civ["p_phone"];
                    }
                }
                $RedemptionDate = $row_spl["Updated"];
                $User = $row_spl["User"];
            }
        }
        $sql_spl = "SELECT * FROM RedeemedCoupons WHERE Coupon_id='" . $row1['Coupon_id'] . "'  AND  User='AdultStudent' ";
        $result_spl = mysqli_query($conn, $sql_spl);
        if (mysqli_num_rows($result_spl) > 0) {
            while ($row_spl = mysqli_fetch_assoc($result_spl)) {
                $sql_civ = "SELECT * FROM AdultStudents WHERE AdultStudent_id='" . $row_spl['User_id'] . "' ";
                $result_civ = mysqli_query($conn, $sql_civ);
                if (mysqli_num_rows($result_civ) > 0) {
                    while ($row_civ = mysqli_fetch_assoc($result_civ)) {
                        $Name = $row_civ["FirstName"] . " " . $row_civ["LastName"];
                        $Email = $row_civ["Email"];
                        $Image = $row_civ["Image"];
                        $PhoneNUmber = $row_civ["PhoneNumber"];
                    }
                }
                $RedemptionDate = $row_spl["Updated"];
                $User = $row_spl["User"];
            }
        }

        $curl_post_data = array(
            'Coupon_id' => $row1["Coupon_id"],
            'CouponCode' => $row1["CouponCode"],
            'DiscountOption' => $row1["DiscountOption"],
            'FixedAmount' => $row1["FixedAmount"],
            'PercentageAmount' => $row1["PercentageAmount"],
            'ExpiryDate' => $row1["ExpiryDate"],
            'Active' => $row1["Active"],
            'Status' => $row1["Status"],
            'Updated' => $row1["Updated"],
            'flag_1' => $row1["flag_1"],
            'flag_2' => $row1["flag_2"],
            'Name' =>  $Name,
            'Email' => $Email,
            'PhoneNUmber' => $PhoneNUmber,
            'Image' => $Image,
            'RedemptionDate' => $RedemptionDate,
            'User' => $User,
        );
        array_push($data, $curl_post_data);
    }
}

echo json_encode($data);
