<?php
require_once("../php/db_connect.php");

$shopId = (int)($_GET['shop_id'] ?? 0);

$shop = null;
$services = [];

if ($shopId > 0) {

    // GET SHOP INFO
    $stmt = $conn->prepare("
        SELECT 
            id, 
            shop_name, 
            shop_type, 
            main_services,
            shop_image,
            email,
            mobile_number,
            address
        FROM users
        WHERE id = ?
        AND role = 'admin'
        LIMIT 1
    ");
    $stmt->bind_param("i", $shopId);
    $stmt->execute();
    $shop = $stmt->get_result()->fetch_assoc();

    // GET SERVICES
    $stmt2 = $conn->prepare("
        SELECT 
            service_name, 
            service_type, 
            description,
            price, 
            estimated_days, 
            service_image
        FROM services
        WHERE owner_shop_id = ?
        AND status = 'active'
    ");
    $stmt2->bind_param("i", $shopId);
    $stmt2->execute();

    $result = $stmt2->get_result();

    while ($row = $result->fetch_assoc()) {
        $services[] = $row;
    }
}

if (!$shop) {
    echo "<div style='padding:30px;'>Shop not found.</div>";
    exit;
}

function h($value) {
    return htmlspecialchars((string)($value ?? ''), ENT_QUOTES, 'UTF-8');
}
?>

<div class="landing-shop-page">

    <div class="shop-hero">
        <div class="shop-hero-content">

            <div class="shop-image-box">
                <?php if (!empty($shop['shop_image'])): ?>
                    <img 
                        src="../uploads/shops/<?php echo h($shop['shop_image']); ?>" 
                        alt="<?php echo h($shop['shop_name']); ?>"
                    >
                <?php else: ?>
                    <div class="no-shop-image">
                        <i class="fa-solid fa-store"></i>
                    </div>
                <?php endif; ?>
            </div>

            <div class="shop-hero-text">
                <span class="shop-label">Shop</span>

                <h1><?php echo h($shop['shop_name']); ?></h1>

                <p class="shop-subtitle">
                    Browse the available services offered by this shop.
                </p>

                <div class="shop-info-list">
                    <p>
                        <strong>Email:</strong>
                        <?php echo h($shop['email'] ?: 'No email yet'); ?>
                    </p>

                    <p>
                        <strong>Mobile Number:</strong>
                        <?php echo h($shop['mobile_number'] ?: 'No mobile number yet'); ?>
                    </p>

                    <p>
                        <strong>Address:</strong>
                        <?php echo h($shop['address'] ?: 'No address yet'); ?>
                    </p>
                </div>

                <div class="shop-meta">
                    <span>
                        <strong>Shop Type:</strong>
                        <?php echo h($shop['shop_type'] ?: 'N/A'); ?>
                    </span>

                    <span>
                        <strong>Main Services:</strong>
                        <?php echo h($shop['main_services'] ?: 'N/A'); ?>
                    </span>
                </div>
            </div>

        </div>
    </div>

    <div class="services-section">
        <div class="section-head">
            <h2>Available Services</h2>
            <p>
                Explore the services currently offered by 
                <?php echo h($shop['shop_name'] ?? 'this shop'); ?>.
            </p>
        </div>

        <?php if (!empty($services)): ?>
            <div class="services-grid">
                <?php foreach ($services as $service): ?>
                    <div class="service-card">

                        <div class="service-image-wrap">
                            <?php if (!empty($service['service_image'])): ?>
                                <img 
                                    src="../uploads/services/<?php echo h($service['service_image']); ?>" 
                                    alt="<?php echo h($service['service_name']); ?>"
                                >
                            <?php else: ?>
                                <div class="no-image-box">No Image Available</div>
                            <?php endif; ?>
                        </div>

                        <div class="service-card-body">
                            <span class="service-type-badge">
                                <?php echo h($service['service_type']); ?>
                            </span>

                            <h3><?php echo h($service['service_name']); ?></h3>

                            <p class="service-description">
                                <?php echo h($service['description'] ?: 'No description available.'); ?>
                            </p>

                            <p class="service-price">
                                <strong>Price:</strong>
                                ₱<?php echo number_format((float)($service['price'] ?? 0), 2); ?>
                            </p>

                            <p class="service-days">
                                <strong>Estimated Days:</strong>
                                <?php echo h($service['estimated_days'] ?: 'N/A'); ?>
                            </p>
                        </div>

                    </div>
                <?php endforeach; ?>
            </div>
        <?php else: ?>
            <div class="empty-services-box">
                No active services available for this shop yet.
            </div>
        <?php endif; ?>
    </div>

    <div class="back-link-wrap">
        <a href="index.php" class="back-link-btn">Back to Shops</a>
    </div>

</div>

<style>
body {
    margin: 0;
    background: #f5f7fb;
    font-family: Arial, Helvetica, sans-serif;
    color: #1f2937;
}

.landing-shop-page {
    max-width: 1280px;
    margin: 0 auto;
    padding: 40px 24px 60px;
}

.shop-hero {
    background: linear-gradient(135deg, #ffffff 0%, #f7f9fc 100%);
    border: 1px solid #e8edf3;
    border-radius: 24px;
    padding: 36px;
    margin-bottom: 28px;
    box-shadow: 0 8px 24px rgba(15, 23, 42, 0.06);
}

.shop-hero-content {
    display: flex;
    gap: 28px;
    align-items: center;
}

.shop-image-box {
    width: 180px;
    height: 180px;
    border-radius: 22px;
    overflow: hidden;
    background: #eef2f7;
    flex-shrink: 0;
}

.shop-image-box img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.no-shop-image {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #0097a7;
    color: white;
    font-size: 60px;
}

.shop-label {
    display: inline-block;
    padding: 7px 14px;
    background: #eaf3ff;
    color: #2563eb;
    border-radius: 999px;
    font-size: 13px;
    font-weight: 700;
    margin-bottom: 14px;
}

.shop-hero h1 {
    margin: 0 0 10px;
    font-size: 48px;
    line-height: 1.1;
    font-weight: 800;
    color: #111827;
}

.shop-subtitle {
    margin: 0 0 18px;
    font-size: 17px;
    color: #6b7280;
    line-height: 1.6;
}

.shop-info-list p {
    margin: 0 0 8px;
    color: #4b5563;
    font-size: 15px;
}

.shop-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    margin-top: 14px;
}

.shop-meta span {
    background: #ffffff;
    border: 1px solid #e5e7eb;
    border-radius: 12px;
    padding: 10px 14px;
    font-size: 14px;
    color: #374151;
}

.services-section {
    margin-top: 10px;
}

.section-head {
    margin-bottom: 20px;
}

.section-head h2 {
    margin: 0 0 8px;
    font-size: 30px;
    font-weight: 800;
    color: #111827;
}

.section-head p {
    margin: 0;
    color: #6b7280;
    font-size: 15px;
}

.services-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 24px;
}

.service-card {
    background: #ffffff;
    border: 1px solid #e9edf3;
    border-radius: 22px;
    overflow: hidden;
    box-shadow: 0 8px 22px rgba(15, 23, 42, 0.05);
}

.service-image-wrap {
    width: 100%;
    height: 230px;
    background: #eef2f7;
    overflow: hidden;
}

.service-image-wrap img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    background: #eef2f7;
}

.no-image-box {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #6b7280;
    font-size: 15px;
    font-weight: 600;
}

.service-card-body {
    padding: 20px;
}

.service-type-badge {
    display: inline-block;
    margin-bottom: 12px;
    padding: 6px 12px;
    border-radius: 999px;
    background: #eef4ff;
    color: #1d4ed8;
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
}

.service-card-body h3 {
    margin: 0 0 10px;
    font-size: 24px;
    font-weight: 800;
    color: #111827;
    text-transform: capitalize;
}

.service-description,
.service-price,
.service-days {
    margin: 0 0 8px;
    font-size: 15px;
    line-height: 1.7;
    color: #4b5563;
}

.empty-services-box {
    background: #ffffff;
    border: 1px dashed #d7dce3;
    border-radius: 20px;
    padding: 40px 24px;
    text-align: center;
    color: #6b7280;
    font-size: 16px;
}

.back-link-wrap {
    margin-top: 28px;
}

.back-link-btn {
    display: inline-block;
    text-decoration: none;
    background: #1f2937;
    color: #ffffff;
    padding: 12px 18px;
    border-radius: 12px;
    font-weight: 700;
}

.back-link-btn:hover {
    background: #111827;
}

@media (max-width: 768px) {
    .landing-shop-page {
        padding: 24px 16px 40px;
    }

    .shop-hero {
        padding: 24px;
    }

    .shop-hero-content {
        flex-direction: column;
        align-items: flex-start;
    }

    .shop-image-box {
        width: 100%;
        height: 220px;
    }

    .shop-hero h1 {
        font-size: 36px;
    }

    .section-head h2 {
        font-size: 26px;
    }
}
</style>