<?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>
            <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>

</body>
</html>