<?php
session_start();
include '../db_connection.php';

$con = new mysqli("localhost", "root", "", "shop");
$con->set_charset("utf8mb4");

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $customer_id = intval($_POST['customer_id']);
    $collected_amount = floatval($_POST['collected_amount']);
    $collected_by = intval($_POST['collected_by']);
    $collected_at = date("Y-m-d H:i:s");

    // Insert into due_collections
    $stmt = $con->prepare("INSERT INTO due_collections (customer_id, collected_amount, collected_by, collected_at) VALUES (?, ?, ?, ?)");
    $stmt->bind_param("idis", $customer_id, $collected_amount, $collected_by, $collected_at);

    if ($stmt->execute()) {
        echo "<script>alert('Collection saved successfully!'); window.location.href='due_list.php?customer_id=$customer_id';</script>";
    } else {
        echo "Error: " . $stmt->error;
    }
    $stmt->close();
}
?>
