<?php
include 'includes/config.php'; 

$invoice_no = isset($_GET['invoice_no']) ? htmlentities($_GET['invoice_no']) : '';

if (empty($invoice_no)) {
    echo "<p>No invoice number provided.</p>";
    exit;
}

try {
    $query = "
        SELECT 
            cd.invoice,
            cd.amount,
            cd.date_deposit,
            cd.payment_status,
            b.bank_name
        FROM 
            cash_deposit cd
        LEFT JOIN 
            banks b ON cd.bank_id = b.id
        WHERE 
            cd.invoice = :invoice_no";
    $stmt = $conn->prepare($query);
    $stmt->execute(['invoice_no' => $invoice_no]);
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // If no records are found
    if (empty($results)) {
        echo "<p>No payment details found for Invoice #{$invoice_no}.</p>";
        exit;
    }
} catch (PDOException $e) {
    // Handle database errors
    echo "<p>Database error: " . htmlspecialchars($e->getMessage()) . "</p>";
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Payment Details for Invoice #<?php echo $invoice_no; ?></title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f9f9f9;
        }
        h1 {
            font-size: 20px;
            margin-bottom: 20px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        table, th, td {
            border: 1px solid #ddd;
        }
        th, td {
            padding: 10px;
            text-align: left;
        }
        th {
            background-color: #f4f4f4;
        }
        .close-button {
            display: inline-block;
            padding: 10px 15px;
            background-color: #007bff;
            color: white;
            text-decoration: none;
            border-radius: 5px;
        }
        .close-button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <h1>Payment Details for Invoice #<?php echo $invoice_no; ?></h1>
    <table>
        <thead>
            <tr>
                <th>Invoice No</th>
                <th>Amount</th>
                <th>Deposit Date</th>
                <th>Payment Method</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($results as $row) { ?>
                <tr>
                    <td><?php echo htmlspecialchars($row['invoice']); ?></td>
                    <td><?php echo number_format($row['amount'], 2); ?></td>
                    <td><?php echo date("d M Y", strtotime($row['date_deposit'])); ?></td>
                    <td>
                        <?php 
                        if (!empty($row['bank_name'])) {
                            echo htmlspecialchars($row['bank_name']); // Show bank name if available
                        } else {
                            echo "----"; // Show "----" if no bank name is found
                        }
                        ?>
                    </td>
                </tr>
            <?php } ?>
        </tbody>
    </table>
    <a href="javascript:window.close();" class="close-button">Close Window</a>
</body>
</html>
