<?php
session_start();
date_default_timezone_set('Asia/Kolkata');
include('includes/config.php');

$issue_id = $_POST['issue_id'];
$book_id = $_POST['book_id'];
$borrower_id = $_POST['borrower_id'];
$fine = $_POST['fine'];
$payment_status = $_POST['payment_status'] ?? 'Paid';
$payment_mode = $_POST['payment_mode'] ?? NULL;
$actual_amount = $_POST['actual_amount'] ?? NULL;
$remarks = $_POST['remarks'] ?? NULL;
$returndate = date("Y-m-d H:i:s"); // Output: 2025-09-08 11:25:30
if ($fine > 0) {
    mysqli_query($con, "INSERT INTO tblfine_payments (borrower_id, book_id, fine_amount, payment_status, payment_mode, actual_amount, remarks) 
        VALUES ('$borrower_id','$book_id','$fine','$payment_status','$payment_mode','$actual_amount','$remarks')");

    if ($payment_status === 'Unpaid') {
        echo "Cannot return book until fine is paid!";
        exit;
    }
}

// Mark book as returned
mysqli_query($con, "UPDATE issued_bookdetails SET return_date = $returndate WHERE id='$issue_id'");
echo "Book returned successfully!";
?>
