<div class="container py-4">
    <!-- Shop Header Banner & Info Bar (Lazada Style) -->
    <div class="card border-0 shadow-sm rounded-3 overflow-hidden mb-4">
        <!-- Banner Image -->
        <div class="position-relative bg-dark" style="height: 180px; background: url('<?= !empty($seller['banner']) ? APP_URL . '/public/' . $seller['banner'] : 'https://images.unsplash.com/photo-1441986300917-64674bd600d8?auto=format&fit=crop&w=1200&q=80' ?>') center/cover no-repeat;">
            <div class="position-absolute top-0 start-0 w-100 h-100" style="background: rgba(0,0,0,0.35);"></div>
            
            <?php if ($isOwner): ?>
                <!-- Settings Button for Shop Owner -->
                <div class="position-absolute top-0 end-0 p-3">
                    <a href="<?= APP_URL ?>/seller/settings" class="btn btn-light btn-sm fw-bold shadow-sm rounded-pill px-3"><i class="bi bi-gear-fill me-1 text-primary"></i> ตั้งค่าหน้าร้านค้า</a>
                    <a href="<?= APP_URL ?>/seller/dashboard" class="btn btn-warning btn-sm fw-bold shadow-sm rounded-pill px-3 ms-1"><i class="bi bi-speedometer2 me-1"></i> Seller Dashboard</a>
                </div>
            <?php endif; ?>
        </div>

        <!-- Store Profile Details Bar -->
        <div class="card-body p-4 bg-white position-relative">
            <div class="row align-items-center g-3">
                <div class="col-md-auto text-center text-md-start">
                    <div style="margin-top: -65px;">
                        <?php if (!empty($seller['logo']) && file_exists(SITE_ROOT . '/public/' . $seller['logo'])): ?>
                            <img src="<?= APP_URL . '/public/' . $seller['logo'] ?>" alt="<?= Security::e($seller['shop_name']) ?>" class="rounded-3 shadow border border-3 border-white bg-white" style="width: 100px; height: 100px; object-fit: cover;">
                        <?php else: ?>
                            <div class="rounded-3 bg-primary text-white d-inline-flex align-items-center justify-content-center shadow border border-3 border-white fs-1" style="width: 100px; height: 100px;">
                                <i class="bi bi-shop"></i>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>

                <div class="col-md">
                    <div class="d-flex align-items-center gap-2 mb-1">
                        <h3 class="fw-bold m-0"><?= Security::e($seller['shop_name']) ?></h3>
                        <span class="badge bg-danger rounded-pill px-2 py-1"><i class="bi bi-patch-check-fill me-1"></i>Official Store</span>
                    </div>
                    <p class="text-muted small mb-2"><?= Security::e($seller['description'] ?? 'ร้านค้าออนไลน์คุณภาพ จำหน่ายสินค้าราคาพิเศษบริการจัดส่งรวดเร็ว') ?></p>
                    <div class="d-flex flex-wrap gap-4 text-secondary small">
                        <span><i class="bi bi-box-seam me-1 text-primary"></i>สินค้าทั้งหมด: <strong class="text-dark"><?= count($products) ?></strong> รายการ</span>
                        <span><i class="bi bi-telephone me-1 text-success"></i><?= Security::e($seller['phone']) ?></span>
                        <div>
                            <i class="bi bi-geo-alt me-1 text-danger"></i>
                            <?= Security::e($seller['address']) ?>
                            <button id="storeMapBtn" class="btn btn-sm btn-outline-primary ms-2" type="button" data-address="<?= Security::e($seller['address']) ?>" data-name="<?= Security::e($seller['shop_name']) ?>" data-lat="<?= Security::e($seller['latitude'] ?? '') ?>" data-lng="<?= Security::e($seller['longitude'] ?? '') ?>">
                                ดูแผนที่
                            </button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- Inline Map Container (Moved outside flex layout) -->
            <div id="inlineStoreMap" class="map-collapsed mt-3" style="height:400px; width:100%;"></div>
        </div>

        <!-- Navigation Tabs Bar -->
        <div class="card-footer bg-light border-top px-4 py-2 d-flex justify-content-between align-items-center flex-wrap gap-3">
            <ul class="nav nav-pills gap-2">
                <li class="nav-item">
                    <a class="nav-link active fw-bold rounded-pill px-4" href="#"><i class="bi bi-grid-fill me-2"></i>สินค้าทั้งหมด (<?= count($products) ?>)</a>
                </li>
            </ul>

            <!-- Search In Store Form -->
            <form action="<?= APP_URL ?>/store/<?= $seller['slug'] ?>" method="GET" class="d-flex align-items-center gap-2">
                <div class="input-group input-group-sm" style="max-width: 280px;">
                    <input type="text" name="q" class="form-control border-secondary-subtle" placeholder="ค้นหาในร้านนี้..." value="<?= Security::e($query) ?>">
                    <button class="btn btn-outline-primary" type="submit"><i class="bi bi-search"></i></button>
                </div>
            </form>
        </div>
    </div>

    <!-- Store Products Catalog Grid -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h5 class="fw-bold m-0"><i class="bi bi-bag-check-fill text-primary me-2"></i>รายการสินค้าในร้านค้า</h5>
        
        <form action="<?= APP_URL ?>/store/<?= $seller['slug'] ?>" method="GET" class="d-flex align-items-center gap-2">
            <?php if (!empty($query)): ?>
                <input type="hidden" name="q" value="<?= Security::e($query) ?>">
            <?php endif; ?>
            <label class="small text-muted text-nowrap">เรียงตาม:</label>
            <select name="sort" class="form-select form-select-sm" onchange="this.form.submit()">
                <option value="latest" <?= $sort === 'latest' ? 'selected' : '' ?>>มาใหม่ล่าสุด</option>
                <option value="price_asc" <?= $sort === 'price_asc' ? 'selected' : '' ?>>ราคาต่ำไปสูง</option>
                <option value="price_desc" <?= $sort === 'price_desc' ? 'selected' : '' ?>>ราคาสูงไปต่ำ</option>
            </select>
        </form>
    </div>

    <?php if (empty($products)): ?>
        <div class="card border-0 shadow-sm p-5 text-center my-4">
            <i class="bi bi-box-seam text-muted display-1 mb-3"></i>
            <h5>ยังไม่มีรายการสินค้าในร้านค้านี้</h5>
            <p class="text-muted">ร้านค้านี้ยังไม่ได้ลงขายสินค้า หรือไม่พบสินค้าที่คุณค้นหา</p>
        </div>
    <?php else: ?>
        <div class="row g-4 mb-5">
            <?php foreach ($products as $prod): ?>
                <div class="col-6 col-md-4 col-lg-3">
                    <div class="card border-0 shadow-sm h-100 product-card">
                        <div class="product-img-wrapper">
                            <?php if ($prod['sale_price']): ?>
                                <span class="badge bg-danger badge-sale">ลดราคา</span>
                            <?php endif; ?>
                            <?php 
                                $imgSrc = !empty($prod['primary_image']) ? APP_URL . '/public/' . ltrim($prod['primary_image'], '/') : APP_URL . '/public/assets/images/sample-headphone.jpg';
                            ?>
                            <a href="<?= APP_URL ?>/product/<?= $prod['slug'] ?>">
                                <img src="<?= $imgSrc ?>" alt="<?= Security::e($prod['title']) ?>" class="card-img-top" onerror="this.src='<?= APP_URL ?>/public/assets/images/sample-headphone.jpg'">
                            </a>
                        </div>
                        <div class="card-body d-flex flex-column">
                            <span class="text-secondary small mb-1"><?= Security::e($prod['category_name']) ?></span>
                            <h6 class="card-title fw-bold text-truncate mb-2">
                                <a href="<?= APP_URL ?>/product/<?= $prod['slug'] ?>" class="text-decoration-none text-dark"><?= Security::e($prod['title']) ?></a>
                            </h6>
                            
                            <div class="mt-auto d-flex align-items-center justify-content-between">
                                <div>
                                    <?php if ($prod['sale_price']): ?>
                                        <span class="fw-bold text-danger fs-5">฿<?= number_format($prod['sale_price'], 2) ?></span>
                                        <span class="text-decoration-line-through text-muted small ms-1">฿<?= number_format($prod['price'], 2) ?></span>
                                    <?php else: ?>
                                        <span class="fw-bold text-primary fs-5">฿<?= number_format($prod['price'], 2) ?></span>
                                    <?php endif; ?>
                                </div>
                                <div class="d-flex gap-1">
                                    <form action="<?= APP_URL ?>/cart/add" method="POST" class="d-inline ajax-cart-form">
                                        <input type="hidden" name="csrf_token" value="<?= $csrf_token ?>">
                                        <input type="hidden" name="product_id" value="<?= $prod['id'] ?>">
                                        <input type="hidden" name="quantity" value="1">
                                        <button type="submit" class="btn btn-outline-warning btn-sm rounded-circle" title="ใส่ตะกร้า"><i class="bi bi-cart-plus"></i></button>
                                    </form>
                                    <a href="<?= APP_URL ?>/product/<?= $prod['slug'] ?>" class="btn btn-primary btn-sm rounded-circle" title="ดูรายละเอียด / สั่งซื้อ"><i class="bi bi-bag-check"></i></a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
</div>
