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

if (isset($_GET['id'])) {
    $withdrawalId = (int) $_GET['id'];

    // Fetch withdrawal details from the database
    $stmt = $conn->prepare("SELECT * FROM bank_debit_amount WHERE id = :id");
    $stmt->execute(['id' => $withdrawalId]);
    $withdrawal = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($withdrawal) {
        // Fetch bank details
        $bankStmt = $conn->prepare("SELECT bank_name FROM banks WHERE id = :bank_id");
        $bankStmt->execute(['bank_id' => $withdrawal['bank_id']]);
        $bank = $bankStmt->fetch(PDO::FETCH_ASSOC);
        $bankName = $bank ? $bank['bank_name'] : 'Unknown Bank';
    } else {
        echo "No withdrawal found with this ID.";
        exit;
    }
} else {
    echo "Invalid voucher ID.";
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Voucher - Withdrawal</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            text-align: center;
        }

        .voucher-container {
            width: 100%;
            max-width: 300px;
            /* Adjusted for thermal printer width */
            margin: 0 auto;
            padding: 10px;
            border: 1px solid #000;
            border-radius: 5px;
            box-sizing: border-box;
            font-size: 14px;
            /* Adjusted for readability on thermal printers */
        }

        .voucher-header {
            font-size: 16px;
            font-weight: bold;
            margin-bottom: 10px;
            text-align: center;
        }

        .voucher-header .business-name {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 5px;
        }

        .voucher-header .address,
        .voucher-header .cell {
            font-size: 12px;
            margin-bottom: 5px;
        }

        .voucher-details {
            margin-top: 15px;
            font-size: 14px;
            line-height: 1.5;
        }

        .voucher-details p {
            margin: 5px 0;
        }

        .btn-print {
            display: block;
            margin-top: 20px;
            width: 100%;
            padding: 8px;
            background-color: #28a745;
            color: #fff;
            border: none;
            cursor: pointer;
        }

        .btn-print:hover {
            background-color: #218838;
        }

        @media print {
            body {
                font-size: 12px;
                padding: 0;
                margin: 0;
            }

            .voucher-container {
                font-size: 14px;
                /* Suitable for thermal printers */
                width: 280px;
                /* Thermal printer width */
            }

            .voucher-header .business-name {
                font-size: 16px;
            }

            .btn-print {
                display: none;
                /* Hide print button during print */
            }
        }
    </style>
</head>

<body>

    <div class="voucher-container">
        <!-- Header Section -->
        <div class="voucher-header">
            <div class="business-name">Fatima Lace Emporium</div>
            <div class="address">Shop #20 Alamghir Market, Shahalam Market, Lahore</div>
            <div class="cell">Cell: 042-37637773</div>
        </div>

        <!-- Withdrawal Details Section -->
        <div class="voucher-details">
            <p><strong>Bank:</strong> <?= htmlspecialchars($bankName) ?></p>
            <p><strong>Amount:</strong> <?= htmlspecialchars($withdrawal['amount']) ?></p>
            <p><strong>Note:</strong> <?= htmlspecialchars($withdrawal['note']) ?></p>
            <p><strong>Date:</strong> <?= htmlspecialchars($withdrawal['date']) ?></p>
            <!-- <p><strong>Transaction ID:</strong> <?= htmlspecialchars($withdrawal['id']) ?></p> -->
        </div>

        <!-- Print Button -->
        <button class="btn-print" onclick="window.print()">Print Voucher</button>
    </div>

</body>

</html>