<?php
/**
 * Print Receipt - Printable receipt view
 */

require_once __DIR__ . '/../../includes/auth.php';
require_once __DIR__ . '/../../includes/receipts.php';
requireAuth();
requireFinanceOrAdmin();

$receiptId = (int)($route_id ?? 0);

if (!$receiptId) {
    header('Location: ' . BASE_PATH . '/finance/receipts');
    exit;
}

try {
    $stmt = $pdo->prepare("
        SELECT r.*, 
               f.name AS fund_name, f.description AS fund_description,
               m.first_name, m.last_name, m.email, m.phone, m.address,
               CONCAT(m.first_name, ' ', m.last_name) AS member_name,
               u.username AS issued_by_username,
               t.service AS transaction_service
        FROM receipts r
        JOIN funds f ON r.fund_id = f.id
        LEFT JOIN transactions t ON r.transaction_id = t.id
        LEFT JOIN members m ON r.member_id = m.id
        LEFT JOIN users u ON r.issued_by = u.id
        WHERE r.id = ?
    ");
    $stmt->execute([$receiptId]);
    $receipt = $stmt->fetch();
    
    if (!$receipt) {
        header('Location: ' . BASE_PATH . '/finance/receipts?toast_error=' . urlencode('Receipt not found.'));
        exit;
    }
} catch (PDOException $e) {
    header('Location: ' . BASE_PATH . '/finance/receipts?toast_error=' . urlencode('Error loading receipt.'));
    exit;
}

$churchName = defined('SITE_FULL_NAME') ? SITE_FULL_NAME : 'PCG Nazareth';
$churchAddress = getSetting('church_address', '');
$embed = !empty($_GET['embed']);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Receipt <?= htmlspecialchars($receipt['receipt_number']) ?> - <?= htmlspecialchars(SITE_NAME) ?></title>
    <link rel="icon" href="<?= BASE_PATH ?>/public/img/logo.png" type="image/png">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: 'Times New Roman', serif;
            padding: 40px;
            background: #fff;
            color: #000;
            line-height: 1.6;
        }
        @media print {
            body { padding: 20px; }
            .no-print { display: none; }
        }
        .receipt-container {
            max-width: 800px;
            margin: 0 auto;
            border: 2px solid #000;
            padding: 40px;
        }
        .header {
            text-align: center;
            border-bottom: 2px solid #000;
            padding-bottom: 20px;
            margin-bottom: 30px;
        }
        .header h1 {
            font-size: 24px;
            font-weight: bold;
            margin-bottom: 10px;
        }
        .header p {
            font-size: 14px;
            margin: 5px 0;
        }
        .receipt-title {
            text-align: center;
            font-size: 20px;
            font-weight: bold;
            margin: 30px 0;
            text-decoration: underline;
        }
        .receipt-info {
            margin: 30px 0;
        }
        .receipt-info table {
            width: 100%;
            border-collapse: collapse;
        }
        .receipt-info td {
            padding: 10px;
            border-bottom: 1px solid #ddd;
        }
        .receipt-info td:first-child {
            font-weight: bold;
            width: 40%;
        }
        .amount-section {
            text-align: center;
            margin: 40px 0;
            padding: 20px;
            border: 2px solid #000;
        }
        .amount-section .label {
            font-size: 16px;
            margin-bottom: 10px;
        }
        .amount-section .amount {
            font-size: 32px;
            font-weight: bold;
        }
        .footer {
            margin-top: 50px;
            padding-top: 20px;
            border-top: 1px solid #000;
            text-align: center;
            font-size: 12px;
        }
        .signature-section {
            margin-top: 60px;
            display: flex;
            justify-content: space-between;
        }
        .signature-box {
            width: 45%;
            text-align: center;
        }
        .signature-line {
            border-top: 1px solid #000;
            margin-top: 50px;
            padding-top: 5px;
        }
        .no-print {
            text-align: center;
            margin-bottom: 20px;
        }
        .no-print button {
            padding: 10px 20px;
            background: #1e3a5f;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }
        .no-print button:hover {
            background: #2c5282;
        }
    </style>
</head>
<body>
    <?php if (!$embed): ?>
    <div class="no-print">
        <button onclick="window.print()">Print Receipt</button>
        <a href="<?= BASE_PATH ?>/finance/receipts" style="margin-left: 10px; color: #1e3a5f;">← Back to Receipts</a>
    </div>
    <?php endif; ?>
    
    <div class="receipt-container">
        <div class="header">
            <h1><?= htmlspecialchars($churchName) ?></h1>
            <?php if ($churchAddress): ?>
            <p><?= htmlspecialchars($churchAddress) ?></p>
            <?php endif; ?>
            <p>Official Receipt</p>
        </div>
        
        <div class="receipt-title">RECEIPT</div>
        
        <div class="receipt-info">
            <table>
                <tr>
                    <td>Receipt Number:</td>
                    <td><?= htmlspecialchars($receipt['receipt_number']) ?></td>
                </tr>
                <tr>
                    <td>Date:</td>
                    <td><?= date('F j, Y', strtotime($receipt['transaction_date'])) ?></td>
                </tr>
                <?php if (!empty($receipt['transaction_service'])): ?>
                <tr>
                    <td>Service:</td>
                    <td><?= $receipt['transaction_service'] === '1st' ? '1st service' : ($receipt['transaction_service'] === '2nd' ? '2nd service' : htmlspecialchars($receipt['transaction_service'])) ?></td>
                </tr>
                <?php endif; ?>
                <?php if ($receipt['member_name']): ?>
                <tr>
                    <td>Received from:</td>
                    <td><?= htmlspecialchars($receipt['member_name']) ?></td>
                </tr>
                <?php if ($receipt['address']): ?>
                <tr>
                    <td>Address:</td>
                    <td><?= htmlspecialchars($receipt['address']) ?></td>
                </tr>
                <?php endif; ?>
                <?php endif; ?>
                <tr>
                    <td>Fund:</td>
                    <td><?= htmlspecialchars($receipt['fund_name']) ?></td>
                </tr>
                <?php if ($receipt['notes']): ?>
                <tr>
                    <td>Notes:</td>
                    <td><?= htmlspecialchars($receipt['notes']) ?></td>
                </tr>
                <?php endif; ?>
            </table>
        </div>
        
        <div class="amount-section">
            <div class="label">Amount Received</div>
            <div class="amount">GH&#8373; <?= number_format($receipt['amount'], 2) ?></div>
        </div>
        
        <div class="footer">
            <p>This receipt serves as confirmation of your contribution.</p>
            <p style="margin-top: 10px;">Issued on: <?= date('F j, Y \a\t g:i A', strtotime($receipt['issued_at'])) ?></p>
            <?php if ($receipt['issued_by_username']): ?>
            <p>Issued by: <?= htmlspecialchars($receipt['issued_by_username']) ?></p>
            <?php endif; ?>
        </div>
        
        <div class="signature-section">
            <div class="signature-box">
                <div class="signature-line">Member Signature</div>
            </div>
            <div class="signature-box">
                <div class="signature-line">Authorized Signature</div>
            </div>
        </div>
    </div>
</body>
</html>
