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

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

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

if (!$shop) {
    die("No partner shop connected.");
}

$qr_data = "https://7d-lao.com/seven-credit-system/public/shop.php?code=" . urlencode($shop["shop_code"]);
$qr_url = "https://api.qrserver.com/v1/create-qr-code/?size=260x260&data=" . urlencode($qr_data);
?>

<!DOCTYPE html>
<html>
<head>
    <title>My QR Code</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background: #f4f6f8;
            margin: 0;
            padding: 30px;
        }

        .container {
            max-width: 720px;
            margin: auto;
        }

        .card {
            background: white;
            border-radius: 16px;
            padding: 28px;
            box-shadow: 0 4px 14px rgba(0,0,0,0.08);
            text-align: center;
        }

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

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

        .qr-box {
            background: #f9fafb;
            border: 1px solid #e5e7eb;
            border-radius: 16px;
            padding: 24px;
            margin: 20px 0;
        }

        .qr-box img {
            max-width: 260px;
            width: 100%;
        }

        .info {
            background: #eff6ff;
            color: #1e3a8a;
            padding: 14px;
            border-radius: 10px;
            text-align: left;
            margin-bottom: 20px;
        }

        .shop-code {
            font-size: 22px;
            font-weight: bold;
            color: #2563eb;
        }

        .btn {
            padding: 11px 16px;
            border-radius: 8px;
            text-decoration: none;
            display: inline-block;
            margin: 6px;
        }

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

        .btn-gray {
            background: #6b7280;
            color: white;
        }
    </style>
</head>
<body>

<div class="container">
    <div class="card">

        <h1>My Partner QR Code</h1>
        <p class="subtitle">Customers can scan this QR code to view your shop.</p>

        <div class="info">
            <p><strong>Shop Name:</strong> <?php echo htmlspecialchars($shop["shop_name"]); ?></p>
            <p><strong>Shop Code:</strong> <span class="shop-code"><?php echo htmlspecialchars($shop["shop_code"]); ?></span></p>
        </div>

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

        <a class="btn btn-primary" target="_blank" href="<?php echo htmlspecialchars($qr_data); ?>">
            Open Shop Page
        </a>

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

    </div>
</div>

</body>
</html>