<?php
session_start();
include_once 'shikanisha.kts.php';

$response = []; // To store the response

// Check if the request is POST
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $trans_id = $_POST['loanid'] ?? null;
    $payamount = $_POST['payamount'] ?? 0;
    $paymethod = $_POST['paymethod'] ?? null;
    $payamount2 = $_POST['payamount2'] ?? 0;
    $paymethod2 = $_POST['paymethod2'] ?? null;
    $sellername = $_SESSION['user'] ?? 'DefaultUser';
    $date = date('Y-m-d');
    $booking_id = $_POST['booking_id'] ?? 0;

    // Begin transaction
    $conn->begin_transaction();

    // Update transaction_receipts with the first payment
    $stmtUpdate = $conn->prepare("UPDATE `transaction_receipts` SET `amount_received` = `amount_received` + ? WHERE `trans_id` = ?");
    $stmtUpdate->bind_param("di", $payamount, $trans_id);

    if (!$stmtUpdate->execute()) {
        $response['status'] = 'error';
        $response['message'] = 'Error updating transaction receipts: ' . $stmtUpdate->error;
        $conn->rollback();
        echo json_encode($response);
        exit();
    }

    $stmtUpdate->close();

    // Insert first payment into payments table
    $stmtInsert = $conn->prepare("INSERT INTO `payments` (`payment_method`, `trans_id`, `amount`) VALUES (?, ?, ?)");
    $stmtInsert->bind_param("sid", $paymethod, $trans_id, $payamount);

    if (!$stmtInsert->execute()) {
        $response['status'] = 'error';
        $response['message'] = 'Error inserting first payment: ' . $stmtInsert->error;
        $conn->rollback();
        echo json_encode($response);
        exit();
    }

    // If there's a second payment, update and insert it


// Check if there's a second payment and insert if so
if ($payamount2 > 0 && $paymethod2) {
    // Prepare the update statement again for the second payment
    $stmtUpdate = $conn->prepare("UPDATE `transaction_receipts` SET `amount_received` = `amount_received` + ? WHERE `trans_id` = ?");
    $stmtUpdate->bind_param("di", $payamount2, $trans_id);
    if (!$stmtUpdate->execute()) {
        $response['status'] = 'error';
        $response['message'] = 'Error updating transaction receipts with second payment: ' . $stmtUpdate->error;
        $conn->rollback(); // Rollback the transaction
        echo json_encode($response);
        exit();
    }
    $stmtUpdate->close(); // Close the statement

    // Insert second payment into payments table
    $stmtInsert->bind_param("sid", $paymethod2, $trans_id, $payamount2);
    if (!$stmtInsert->execute()) {
        $response['status'] = 'error';
        $response['message'] = 'Error inserting second payment: ' . $stmtInsert->error;
        $conn->rollback(); // Rollback the transaction
        echo json_encode($response);
        exit();
    }
}

  
// ... (rest of the code)
  // If there's a second payment, update and insert it
// Check if there's a second payment and insert if so
if ($booking_id > 0 ) {
    // Prepare the insert statement for the second payment
    $stmtUpdate = $conn->prepare("INSERT INTO `room_bills`(`trans_id`, `booking_id`, `amount`) VALUES (?, ?, ?)");
    $stmtUpdate->bind_param("iid", $trans_id, $booking_id, $payamount);
    
    if (!$stmtUpdate->execute()) {
        $response['status'] = 'error';
        $response['message'] = 'Error updating room bills: ' . $stmtUpdate->error;
        $conn->rollback(); // Rollback the transaction
        echo json_encode($response);
        exit();
    }
    
    $stmtUpdate->close(); // Close the statement
}


    $stmtInsert->close();

    // Commit transaction
    $conn->commit();
    $response['status'] = 'success';
    $response['message'] = 'Payments updated successfully';
} else {
    $response['status'] = 'error';
    $response['message'] = 'Invalid request method.';
}

echo json_encode($response);
?>
