<?php
session_start();
require_once '../library/database.php';

if (!isset($_SESSION["session_user"]) || $_SESSION["session_user"] !== TRUE) {
    header("location: " . $app_url . "/login.php");
}

$data = array(
    'status'  => false,
    'message' => '404 Not Found',
);


if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    $data['message'] = 'METHOD Not Support';
    echo json_encode($data);
    return;
}

$account_id = $con->real_escape_string($_SESSION['account_id']);
$referenceNo = $con->real_escape_string($_POST['referenceNo']);

$result = $con->query("SELECT `status`,`amount` FROM donate WHERE account_id={$account_id} AND referenceNo = '{$referenceNo}'");
if ($result->num_rows > 0) {
    $row = $result->fetch_object();

    if ($row->status == 1) {
			$con->query("DELETE FROM `donate` WHERE `status`=0 AND `account_id`={$account_id}");
            $data['status'] = true;
            $data['message'] = ' '.$row->amount.' บาท';
            echo json_encode($data);
    } else {
        $data['message'] = 'Pending '.$row->amount.' Bath';
        echo json_encode($data);
    }
} else {
    $data['message'] = 'Not Found';
    echo json_encode($data);
}
