<?php
session_start();
require_once '../config/database.php';

if (!isset($_SESSION["user_id"]) || !in_array($_SESSION["role"], ["customer","company_admin"])) {
    header("Location: ../auth/login.php");
    exit;
}

$stmt = $pdo->prepare("SELECT * FROM customers WHERE user_id = ? LIMIT 1");
$stmt->execute([$_SESSION["user_id"]]);
$customer = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$customer) {
    die("No customer wallet connected.");
}

$qr_data = "https://7d-lao.com/seven-credit-system/customer/transfer.php?to=" . urlencode($customer["customer_code"]);

$qr_url = "https://api.qrserver.com/v1/create-qr-code/?size=800x800&data=" . urlencode($qr_data);
?>

<!DOCTYPE html>
<html>
<head>
    <title>My Customer QR Code</title>
    <style>
        body{
            font-family:Arial,sans-serif;
            background:#f4f6f8;
            margin:0;
            min-height:100vh;
        }

        .container{
            max-width:100%;
            padding:16px;
        }

        .card{
            background:white;
            border-radius:20px;
            padding:20px;
            text-align:center;
            min-height:calc(100vh - 32px);
            box-sizing:border-box;
        }

        h1{
            margin-top:0;
            color:#111827;
        }

        .subtitle{
            color:#6b7280;
            margin-bottom:18px;
        }

        .info{
            background:#eff6ff;
            color:#1e3a8a;
            padding:10px;
            border-radius:12px;
            text-align:left;
            width:85%;
            max-width:380px;
            margin:0 auto 20px;
        }

        .customer-code{
            font-size:24px;
            font-weight:800;
            color:#2563eb;
        }

        .qr-box{
            background:white;
            padding:10px;
        }

        .qr-box img{
            width:90vw;
            max-width:700px;
            height:auto;
        }

        .btn{
            padding:16px 24px;
            border-radius:12px;
            text-decoration:none;
            display:inline-block;
            margin:8px;
            font-size:18px;
            font-weight:bold;
        }

        .btn-primary{
            background:#2563eb;
            color:white;
        }

        .btn-gray{
            background:#6b7280;
            color:white;
        }

        .btn-large{
            padding:48px 58px;
            font-size:40px;
            font-weight:bold;
        }
    </style>
</head>
<body>

<div class="container">
    <div class="card">
        <h1>My Customer QR Code</h1>
        <p class="subtitle">Other customers can scan this QR code to send credit to you.</p>

        <div class="info">
            <p><strong>Name:</strong> <?php echo htmlspecialchars($customer["full_name"]); ?></p>
            <?php if (!empty($customer["pay_id"])): ?>
            <p><strong>Pay ID:</strong>
            <span class="customer-code" style="font-size:22px;letter-spacing:2px;">
            <?php echo htmlspecialchars($customer["pay_id"]); ?>
            </span>
            </p>
            <p style="color:#6b7280;font-size:13px;margin-top:2px;">Tell a shop your Pay ID or phone number to receive money.</p>
            <?php endif; ?>
            <p><strong>Customer Code:</strong>
            <span class="customer-code">
            <?php echo htmlspecialchars($customer["customer_code"]); ?>
            </span>
            </p>
        </div>

        <div class="qr-box">
            <img src="<?php echo $qr_url; ?>" alt="Customer QR Code">
        </div>

        <p>
        <a class="btn btn-gray btn-large" href="dashboard.php">
            Back to Dashboard
        </a>
    </div>
</div>

<?php require __DIR__ . '/../config/auto_update.php'; ?>
</body>
</html>