<?php
require_once __DIR__ . '/../../app/core/bootstrap.php';
auth_require_login();
$pdo = crm_pdo();
$_lang = gp_lang($pdo);

$id = (int)($_GET['id'] ?? 0);
if ($id <= 0) die('Missing payment ID.');

$stmt = $pdo->prepare("
    SELECT pp.*, p.title AS project_title, p.code AS project_code, c.name AS customer_name, c.address AS customer_address, c.city AS customer_city
    FROM project_payments pp
    JOIN projects p ON p.id = pp.project_id
    LEFT JOIN customers c ON c.id = p.customer_id
    WHERE pp.id = ?
");
$stmt->execute([$id]);
$payment = $stmt->fetch();

if (!$payment) die('Payment not found.');

?>
<!DOCTYPE html>
<html lang="<?php echo $_lang; ?>">
<head>
    <meta charset="UTF-8">
    <title>Receipt - <?php echo h($payment['project_code']); ?></title>
    <style>
        body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; line-height: 1.6; color: #333; margin: 0; padding: 40px; background: #f9f9f9; }
        .receipt-container { max-width: 800px; margin: 0 auto; background: #fff; padding: 40px; border-radius: 8px; box-shadow: 0 4px 20px rgba(0,0,0,0.08); }
        .header { display: flex; justify-content: space-between; border-bottom: 2px solid #3b82f6; padding-bottom: 20px; margin-bottom: 30px; }
        .header h1 { margin: 0; color: #3b82f6; font-size: 1.8rem; }
        .company-info { text-align: right; font-size: 0.9rem; color: #666; }
        .section { margin-bottom: 25px; }
        .section-title { font-weight: bold; text-transform: uppercase; font-size: 0.8rem; color: #888; margin-bottom: 8px; border-bottom: 1px solid #eee; }
        .info-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
        .info-item { margin-bottom: 10px; }
        .info-label { font-size: 0.75rem; color: #888; display: block; }
        .info-value { font-weight: 600; font-size: 1rem; }
        .amount-box { background: #f0f7ff; border: 1px solid #c2e0ff; padding: 20px; border-radius: 8px; text-align: center; margin-top: 30px; }
        .amount-value { font-size: 2.2rem; font-weight: 800; color: #1e40af; }
        .footer { margin-top: 50px; text-align: center; font-size: 0.8rem; color: #aaa; border-top: 1px solid #eee; padding-top: 20px; }
        @media print {
            body { background: #fff; padding: 0; }
            .receipt-container { box-shadow: none; width: 100%; max-width: none; }
            .no-print { display: none; }
        }
        .btn-print { background: #3b82f6; color: #fff; border: none; padding: 10px 20px; border-radius: 6px; cursor: pointer; font-weight: 600; margin-bottom: 20px; }
    </style>
</head>
<body>
    <div style="text-align: center;" class="no-print">
        <button class="btn-print" onclick="window.print()">Print Receipt / Save as PDF</button>
    </div>
    
    <div class="receipt-container">
        <div class="header">
            <div>
                <h1><?php echo $_lang==='el'?'ΑΠΟΔΕΙΞΗ ΕΙΣΠΡΑΞΗΣ':'COLLECTION RECEIPT'; ?></h1>
                <div style="font-weight: 600;">#PAY-<?php echo h($payment['id']); ?></div>
            </div>
            <div class="company-info">
                <strong>GoPools Project Management</strong><br>
                Grevenon 56, Ano Liosia 13341<br>
                Athens, Greece<br>
                info@gopools.gr
            </div>
        </div>

        <div class="info-grid">
            <div class="section">
                <div class="section-title"><?php echo $_lang==='el'?'ΠΕΛΑΤΗΣ':'BILL TO'; ?></div>
                <div class="info-item">
                    <span class="info-value"><?php echo h($payment['customer_name']); ?></span>
                    <?php if ($payment['customer_address']): ?>
                        <div class="info-value" style="font-weight: 400; font-size: 0.9rem;">
                            <?php echo h($payment['customer_address']); ?><br>
                            <?php echo h($payment['customer_city']); ?>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
            <div class="section">
                <div class="section-title"><?php echo $_lang==='el'?'ΠΛΗΡΟΦΟΡΙΕΣ':'DETAILS'; ?></div>
                <div class="info-item">
                    <span class="info-label"><?php echo $_lang==='el'?'ΕΡΓΟ':'PROJECT'; ?></span>
                    <span class="info-value"><?php echo h($payment['project_code'] . ' - ' . $payment['project_title']); ?></span>
                </div>
                <div class="info-item">
                    <span class="info-label"><?php echo $_lang==='el'?'ΗΜΕΡΟΜΗΝΙΑ':'DATE'; ?></span>
                    <span class="info-value"><?php echo h(gp_date($payment['payment_date'])); ?></span>
                </div>
                <div class="info-item">
                    <span class="info-label"><?php echo $_lang==='el'?'ΤΥΠΟΣ':'TYPE'; ?></span>
                    <span class="info-value"><?php 
                        if ($payment['payment_type'] === 'deposit') echo $_lang==='el'?'Προκαταβολή':'Deposit';
                        elseif ($payment['payment_type'] === 'refund') echo $_lang==='el'?'Επιστροφή':'Refund';
                        else echo $_lang==='el'?'Πληρωμή':'Standard Payment';
                    ?></span>
                </div>
            </div>
        </div>

        <?php if ($payment['notes']): ?>
        <div class="section">
            <div class="section-title"><?php echo $_lang==='el'?'ΣΗΜΕΙΩΣΕΙΣ':'NOTES'; ?></div>
            <p><?php echo nl2br(h($payment['notes'])); ?></p>
        </div>
        <?php endif; ?>

        <div class="amount-box">
            <div class="section-title"><?php echo $_lang==='el'?'ΣΥΝΟΛΙΚΟ ΠΟΣΟ':'TOTAL AMOUNT'; ?></div>
            <div class="amount-value">€ <?php echo h(number_format((float)$payment['amount'], 2)); ?></div>
        </div>

        <div class="footer">
            <?php echo $_lang==='el'?'Σας ευχαριστούμε για την προτίμησή σας.':'Thank you for your business.'; ?><br>
            generated by GoPools CRM &middot; <?php echo date('d/m/Y H:i'); ?>
        </div>
    </div>
</body>
</html>
