<?php
require_once('../connect.php');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $referenceCode = $_POST['reference_code'];
    $amountPaid = $_POST['payAmount'];
    $riceType = $_POST['rice_type'];
    $uniqueId = $_POST['unique_id'];
    $quantity = $_POST['quantity_kilo'];
    $datePurchase = $_POST['date_purchase'];
    $paymentDue = $_POST['payment_due'];

    // Insert new Paid transaction
    $stmt = $conn->prepare("INSERT INTO tbl_bigasrecord (unique_id, rice_type, transaction_type, quantity_kilo, date_purchase, payment_due, bigas_credit_amount, payment_option, date_encoded, reference_code) VALUES (?, ?, 'Paid', ?, ?, ?, ?, 'Cash', NOW(), ?)");
    $stmt->bind_param("ssissds", $uniqueId, $riceType, $quantity, $datePurchase, $paymentDue, $amountPaid, $referenceCode);

    // Update the "Order" record's payment_option
    $updateStmt = $conn->prepare("UPDATE tbl_bigasrecord SET payment_option = 'Cash' WHERE reference_code = ? AND transaction_type = 'Order'");
    $updateStmt->bind_param("s", $referenceCode);

    if ($stmt->execute() && $updateStmt->execute()) {
        echo "success";
    } else {
        http_response_code(500);
        echo "error";
    }
}
?>
