<?php
error_reporting(E_ALL);
ini_set('display_errors', 0);

$db_host = 'localhost';
$db_name = 'violexst_bot';
$db_user = 'violexst_botuser';
$db_pass = 'ViolexBot#92!qR7@Lm';

$token = $_GET['token'] ?? '';
$expired = isset($_GET['expired']);

if (!$token) die('نامعتبر.');

$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
$conn->set_charset('utf8mb4');

$stmt = $conn->prepare("SELECT * FROM invoices WHERE token = ?");
$stmt->bind_param('s', $token);
$stmt->execute();
$invoice = $stmt->get_result()->fetch_assoc();
$stmt->close();
$conn->close();

if (!$invoice) die('پیش‌فاکتور یافت نشد.');

$title = $expired ? 'پیش‌فاکتور منقضی شد' : 'پرداخت ناموفق';
$subtitle = $expired
    ? 'زمان ۱ ساعت این پیش‌فاکتور به پایان رسید'
    : 'پرداخت شما انجام نشد';
?>
<!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 ?> - ویولکس</title>
<style>
    * { margin: 0; padding: 0; box-sizing: border-box; }
    body {
        font-family: Tahoma, Arial, sans-serif;
        min-height: 100vh;
        display: flex;
        align-items: center;
        justify-content: center;
        background: linear-gradient(135deg, #d32f2f 0%, #8b1e1e 100%);
        padding: 20px;
    }
    .card {
        background: white;
        border-radius: 24px;
        max-width: 550px;
        width: 100%;
        box-shadow: 0 20px 60px rgba(0,0,0,0.3);
        overflow: hidden;
    }
    .header {
        background: linear-gradient(135deg, #f44336 0%, #c62828 100%);
        color: white;
        padding: 40px 30px;
        text-align: center;
    }
    .failed-icon { font-size: 80px; margin-bottom: 15px; }
    .header h1 { font-size: 26px; margin-bottom: 8px; }
    .header p { font-size: 14px; opacity: 0.95; }
    .body { padding: 30px; }
    .invoice-no {
        text-align: center;
        background: #fff0f0;
        border-radius: 12px;
        padding: 12px;
        margin-bottom: 20px;
        font-size: 13px;
        color: #555;
    }
    .invoice-no strong {
        color: #c62828;
        font-family: monospace;
        direction: ltr;
        display: inline-block;
        word-break: break-all;
    }
    .row {
        display: flex;
        justify-content: space-between;
        padding: 12px 0;
        border-bottom: 1px solid #eee;
        font-size: 15px;
    }
    .row:last-of-type { border-bottom: none; }
    .row .label { color: #888; }
    .row .value {
        color: #333;
        font-weight: bold;
        max-width: 60%;
        text-align: left;
        direction: ltr;
    }
    .status-badge {
        display: inline-block;
        padding: 8px 20px;
        background: #f44336;
        color: white;
        border-radius: 20px;
        font-weight: bold;
        font-size: 14px;
    }
    .total {
        background: #fff0f0;
        border-radius: 12px;
        padding: 18px;
        margin-top: 20px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        border: 2px solid #f44336;
    }
    .total .label { font-size: 16px; color: #333; }
    .total .amount { font-size: 22px; font-weight: bold; color: #c62828; }
    .back-btn {
        display: block;
        width: 100%;
        padding: 18px;
        margin-top: 25px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        text-decoration: none;
        text-align: center;
        border-radius: 12px;
        font-size: 17px;
        font-weight: bold;
    }
    .footer {
        text-align: center;
        padding: 15px;
        background: #fafafa;
        color: #999;
        font-size: 12px;
    }
</style>
</head>
<body>
<div class="card">
    <div class="header">
        <div class="failed-icon">❌</div>
        <h1><?= $title ?></h1>
        <p><?= $subtitle ?></p>
    </div>

    <div class="body">
        <div class="invoice-no">
            شماره فاکتور: <strong><?= htmlspecialchars($invoice['token']) ?></strong>
        </div>

        <div class="row">
            <span class="label">👤 نام واریز کننده:</span>
            <span class="value"><?= htmlspecialchars($invoice['payer_name']) ?></span>
        </div>
        <div class="row">
            <span class="label">📱 شماره تماس:</span>
            <span class="value"><?= htmlspecialchars($invoice['payer_phone']) ?></span>
        </div>
        <div class="row">
            <span class="label">📦 نام محصول:</span>
            <span class="value"><?= htmlspecialchars($invoice['product_name']) ?></span>
        </div>
        <div class="row">
            <span class="label">💰 قیمت واحد:</span>
            <span class="value"><?= number_format($invoice['price']) ?> تومان</span>
        </div>
        <div class="row">
            <span class="label">🔢 تعداد:</span>
            <span class="value"><?= $invoice['quantity'] ?></span>
        </div>
        <div class="row">
            <span class="label">📌 وضعیت:</span>
            <span class="value">
                <span class="status-badge">
                    <?= $expired ? '⏰ منقضی شده' : '❌ پرداخت ناموفق' ?>
                </span>
            </span>
        </div>

        <div class="total">
            <span class="label">💰 مبلغ:</span>
            <span class="amount"><?= number_format($invoice['total_amount']) ?> تومان</span>
        </div>

        <a href="https://t.me/VIOLEXQ_bot" class="back-btn">
            🔙 بازگشت به ربات
        </a>
    </div>

    <div class="footer">
        در صورت کسر وجه، مبلغ تا ۷۲ ساعت بازگردانده می‌شود.
    </div>
</div>
<div style="text-align:center; margin:12px 0;">
    <a referrerpolicy='origin' target='_blank' href='https://trustseal.enamad.ir/?id=716122&Code=z3z1UH6o6PgHsulQATp9NFkdz8lyxGYK'><img referrerpolicy='origin' src='https://trustseal.enamad.ir/logo.aspx?id=716122&Code=z3z1UH6o6PgHsulQATp9NFkdz8lyxGYK' alt='' style='cursor:pointer' code='z3z1UH6o6PgHsulQATp9NFkdz8lyxGYK'></a>
</div>
</body>
</html>