<?php
require_once 'api/config.php';

$invoice_id = intval($_GET['id'] ?? 0);

try {
    $pdo = getDB();
    $stmt = $pdo->prepare("SELECT * FROM invoices WHERE id = ?");
    $stmt->execute([$invoice_id]);
    $invoice = $stmt->fetch();
} catch (Exception $e) {
    die('خطا: ' . $e->getMessage());
}

if (!$invoice) { die('فاکتور یافت نشد'); }
if ($invoice['status'] === 'paid') { header('Location: /invoice-success.php?invoice=' . urlencode($invoice['invoice_number'])); exit; }
if ($invoice['status'] === 'failed') { header('Location: /invoice-failed.php'); exit; }

$payment_url = ZIBAL_STARTPAY . $invoice['track_id'];
?>
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>پیش‌فاکتور - ویولکس</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: Tahoma, 'Segoe UI', sans-serif; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
        .card { background: white; border-radius: 24px; padding: 40px 35px; max-width: 520px; width: 100%; box-shadow: 0 25px 70px rgba(0,0,0,0.3); }
        .header { text-align: center; margin-bottom: 30px; }
        .header-icon { font-size: 60px; margin-bottom: 10px; }
        h1 { color: #333; font-size: 22px; margin-bottom: 8px; }
        .subtitle { color: #888; font-size: 13px; }
        .info-row { display: flex; justify-content: space-between; padding: 14px 0; border-bottom: 1px solid #f0f0f0; font-size: 14px; }
        .info-row:last-of-type { border-bottom: none; }
        .info-label { color: #666; }
        .info-value { color: #333; font-weight: bold; direction: ltr; }
        .amount-box { background: linear-gradient(135deg, #f0f4ff 0%, #e8ecff 100%); padding: 25px; border-radius: 16px; margin: 25px 0; text-align: center; border: 2px dashed #667eea; }
        .amount-label { color: #666; font-size: 13px; margin-bottom: 10px; }
        .amount-value { font-size: 32px; font-weight: bold; color: #667eea; }
        .amount-unit { font-size: 16px; color: #667eea; margin-right: 5px; }
        .btn { display: block; width: 100%; padding: 18px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; text-align: center; text-decoration: none; border-radius: 14px; font-weight: bold; font-size: 17px; margin-top: 25px; box-shadow: 0 8px 20px rgba(102,126,234,0.4); }
        .note { text-align: center; color: #999; font-size: 12px; margin-top: 20px; line-height: 1.8; }
        .note strong { color: #667eea; }
        .footer { text-align: center; margin-top: 25px; padding-top: 20px; border-top: 1px solid #f0f0f0; color: #bbb; font-size: 11px; }
    </style>
</head>
<body>
    <div class="card">
        <div class="header">
            <div class="header-icon">🧾</div>
            <h1>پیش‌فاکتور پرداخت</h1>
            <div class="subtitle">VIOLEX | کلینیک رفع اشکال کنکور</div>
        </div>
        <div class="info-row"><span class="info-label">شماره فاکتور</span><span class="info-value"><?= htmlspecialchars($invoice['invoice_number']) ?></span></div>
        <div class="info-row"><span class="info-label">شناسه کاربر</span><span class="info-value"><?= htmlspecialchars($invoice['user_id']) ?></span></div>
        <div class="info-row"><span class="info-label">محصول</span><span class="info-value"><?= htmlspecialchars($invoice['package_name']) ?></span></div>
        <div class="info-row"><span class="info-label">تاریخ صدور</span><span class="info-value"><?= htmlspecialchars($invoice['created_at']) ?></span></div>
        <div class="amount-box">
            <div class="amount-label">مبلغ قابل پرداخت</div>
            <div class="amount-value"><?= number_format($invoice['amount']) ?><span class="amount-unit">تومان</span></div>
        </div>
        <a href="<?= htmlspecialchars($payment_url) ?>" class="btn">💳 ورود به درگاه پرداخت</a>
        <div class="note">پس از پرداخت، به ربات تلگرام برگردید<br>و روی دکمه <strong>«✅ پرداخت کردم»</strong> بزنید.</div>
        <div class="footer">© VIOLEX - تمامی حقوق محفوظ است</div>
    </div>
</body>
</html>
