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

if (!isset($_GET["code"])) {
    die("Shop code missing.");
}

$shop_code = trim($_GET["code"]);

$stmt = $pdo->prepare("
    SELECT
        shop_code,
        shop_name,
        owner_name,
        phone,
        address,
        status,
        latitude,
        longitude
    FROM partner_shops
    WHERE shop_code = ?
    AND status = 'active'
    LIMIT 1
");
$stmt->execute([$shop_code]);

$shop = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$shop) {
    die("Shop not found or inactive.");
}
?>

<!DOCTYPE html>
<html>
<head>
    <title><?php echo htmlspecialchars($shop["shop_name"]); ?></title>

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <style>

        body{
            font-family:Arial,sans-serif;
            background:#f4f6f8;
            margin:0;
            padding:20px;
        }

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

        .card{
            background:white;
            border-radius:18px;
            padding:24px;
            box-shadow:0 6px 20px rgba(0,0,0,0.08);
        }

        .badge{
            display:inline-block;
            background:#dcfce7;
            color:#166534;
            padding:8px 14px;
            border-radius:999px;
            font-size:13px;
            font-weight:bold;
            margin-bottom:18px;
        }

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

        .shop-code{
            color:#2563eb;
            font-size:20px;
            font-weight:bold;
            margin-top:10px;
        }

        .section{
            margin-top:24px;
        }

        .label{
            font-weight:bold;
            color:#374151;
            margin-bottom:6px;
        }

        .value{
            color:#111827;
        }

        .address-box{
            background:#f9fafb;
            border-radius:10px;
            padding:14px;
            margin-top:8px;
        }

        .btn{
            display:inline-block;
            padding:12px 18px;
            border-radius:10px;
            text-decoration:none;
            margin-top:18px;
            margin-right:10px;
            font-weight:bold;
        }

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

        .btn-call{
            background:#16a34a;
            color:white;
        }

        .footer{
            text-align:center;
            margin-top:20px;
            color:#6b7280;
            font-size:14px;
        }

        .logo{
            text-align:center;
            font-size:28px;
            font-weight:bold;
            color:#111827;
            margin-bottom:20px;
        }

    </style>

</head>
<body>

<div class="container">

    <div class="logo">
        Seven Digital
    </div>

    <div class="card">

        <span class="badge">
            ✓ Verified Partner Shop
        </span>

        <h1>
            <?php echo htmlspecialchars($shop["shop_name"]); ?>
        </h1>

        <div class="shop-code">
            <?php echo htmlspecialchars($shop["shop_code"]); ?>
        </div>

        <div class="section">
            <div class="label">
                Owner
            </div>

            <div class="value">
                <?php echo htmlspecialchars($shop["owner_name"]); ?>
            </div>
        </div>

        <div class="section">
            <div class="label">
                Phone Number
            </div>

            <div class="value">
                <?php echo htmlspecialchars($shop["phone"]); ?>
            </div>

            <?php if(!empty($shop["phone"])): ?>

                <a
                    class="btn btn-call"
                    href="tel:<?php echo htmlspecialchars($shop["phone"]); ?>">
                    Call Shop
                </a>

            <?php endif; ?>
        </div>

        <div class="section">
            <div class="label">
                Address
            </div>

            <div class="address-box">
                <?php echo nl2br(htmlspecialchars($shop["address"])); ?>
            </div>
        </div>

        <?php if (!empty($shop['latitude']) && !empty($shop['longitude'])): ?>

            <div class="section">

                <a
                    class="btn btn-map"
                    target="_blank"
                    href="https://maps.google.com/?q=<?php echo $shop['latitude']; ?>,<?php echo $shop['longitude']; ?>">

                    Open in Google Maps

                </a>

            </div>

        <?php endif; ?>

    </div>

    <div class="footer">
        Active Seven Digital Partner Shop
    </div>

</div>

</body>
</html>