<?php
session_start();
include('includes/config.php');
include_once('includes/settings.php');

$vendorId = isset($_GET['vid']) ? intval($_GET['vid']) : 0;

if ($vendorId == 0) {
    // Official Store (Admin Products)
    $vendor = array(
        'store_name' => 'Awesome Mall Official',
        'business_address' => 'Headquarters, Accra',
        'business_phone' => '+233 123 456 789',
        'status' => 'active'
    );
    // Note: Admin products have vendor_id as NULL or 0
    $query = true; // Bypass db check
} else {
    $query = mysqli_query($con, "SELECT * FROM vendors WHERE id='$vendorId' AND status='active'");
    if (mysqli_num_rows($query) == 0) {
        echo "Vendor not found or inactive.";
        exit;
    }
    $vendor = mysqli_fetch_array($query);
}

$pageTitle = $vendor['store_name'] . " - Awesome Mall";
?>
<!DOCTYPE html>
<html lang="en">
<?php include('includes/head.php'); ?>

<body class="cnt-home">

    <!-- Header -->
    <header class="header-style-1">
        <?php include('includes/top-header.php'); ?>
        <?php include('includes/main-header.php'); ?>
        <?php include('includes/menu-bar.php'); ?>
    </header>

    <!-- Main Body Wrapper -->
    <div class="body-content outer-top-xs" id="top-banner-and-menu">
        <div class="awesome-container">

            <!-- Vendor Profile Header -->
            <div class="card mb-4 border-0 shadow-sm overflow-hidden" style="border-radius: 12px;">
                <div class="card-body p-5 text-center bg-white position-relative overflow-hidden">
                    <!-- Background Decoration -->
                    <div
                        style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: linear-gradient(135deg, rgba(0,128,0,0.05) 0%, rgba(0,128,0,0) 100%); z-index: 1;">
                    </div>

                    <div style="position: relative; z-index: 2;">
                        <div class="mb-3">
                            <span
                                class="d-inline-flex align-items-center justify-content-center p-4 rounded-circle bg-light text-success shadow-sm"
                                style="width: 80px; height: 80px;">
                                <i class="fa fa-store fa-3x"></i>
                            </span>
                        </div>
                        <h1 class="display-6 fw-bold mb-2"><?php echo htmlentities($vendor['store_name']); ?></h1>
                        <p class="text-muted mb-3">
                            <?php if ($vendor['business_address']): ?>
                                <span class="mx-2"><i class="fa fa-map-marker me-1"></i>
                                    <?php echo htmlentities($vendor['business_address']); ?></span>
                            <?php endif; ?>
                            <?php if ($vendor['business_phone']): ?>
                                <span class="mx-2"><i class="fa fa-phone me-1"></i>
                                    <?php echo htmlentities($vendor['business_phone']); ?></span>
                            <?php endif; ?>
                        </p>
                    </div>
                </div>
            </div>

            <!-- Products Section -->
            <div class="white-box mb-4">
                <div class="section-header-plain">
                    <span><i class="fa fa-th-large mr-2"></i>Products from
                        <?php echo htmlentities($vendor['store_name']); ?></span>
                </div>
                <div class="p-3">
                    <div class="row">
                        <?php
                        if ($vendorId == 0) {
                            $products = mysqli_query($con, "SELECT * FROM products WHERE (vendor_id IS NULL OR vendor_id=0) AND approval_status='approved' ORDER BY id DESC");
                        } else {
                            $products = mysqli_query($con, "SELECT * FROM products WHERE vendor_id='$vendorId' AND approval_status='approved' ORDER BY id DESC");
                        }
                        if (mysqli_num_rows($products) > 0) {
                            while ($row = mysqli_fetch_array($products)) {
                                $disc = 0;
                                if ($row['productPriceBeforeDiscount'] > 0 && $row['productPriceBeforeDiscount'] > $row['productPrice']) {
                                    $disc = round((($row['productPriceBeforeDiscount'] - $row['productPrice']) / $row['productPriceBeforeDiscount']) * 100);
                                }
                                ?>
                                <div class="col-6 col-md-4 col-lg-2 mb-3">
                                    <div class="am-card h-100">
                                        <?php if ($disc > 0): ?>
                                            <div class="discount-tag"><?php echo $disc; ?>% <small>OFF</small></div>
                                        <?php endif; ?>

                                        <a href="index.php?pid=<?php echo htmlentities($row['id']); ?>&action=wishlist"
                                            class="btn-wishlist-app" title="Add to Wishlist">
                                            <i class="fa fa-heart"></i>
                                        </a>

                                        <a href="product-details.php?pid=<?php echo htmlentities($row['id']); ?>"
                                            class="text-decoration-none">
                                            <div class="img-wrap">
                                                <img src="__admin__/productimages/<?php echo htmlentities($row['id']); ?>/<?php echo htmlentities($row['productImage1']); ?>"
                                                    alt="<?php echo htmlentities($row['productName']); ?>"
                                                    onerror="this.src='assets/images/blank.gif'">
                                            </div>
                                            <div class="p-name"><?php echo htmlentities($row['productName']); ?></div>
                                            <div class="price-wrap">
                                                <span class="p-price">Ghs.
                                                    <?php echo number_format($row['productPrice'], 2); ?></span>
                                                <?php if ($disc > 0): ?>
                                                    <span class="p-old-price">Ghs.
                                                        <?php echo number_format($row['productPriceBeforeDiscount'], 2); ?></span>
                                                <?php endif; ?>
                                            </div>
                                        </a>

                                        <?php if ($row['productAvailability'] == 'In Stock'): ?>
                                            <a href="index.php?action=add&id=<?php echo $row['id']; ?>" class="btn-add-cart-app">
                                                <i class="fa fa-shopping-cart"></i>
                                            </a>
                                        <?php else: ?>
                                            <span class="btn-out-stock">Out of Stock</span>
                                        <?php endif; ?>
                                    </div>
                                </div>
                                <?php
                            }
                        } else {
                            echo '<div class="col-12 text-center py-5">
                                    <i class="fa fa-shopping-basket fa-3x text-muted mb-3"></i>
                                    <p class="text-muted">No products found for this vendor.</p>
                                  </div>';
                        }
                        ?>
                    </div>
                </div>
            </div>

        </div><!-- /.awesome-container -->
    </div><!-- /.body-content -->

    <?php include('includes/footer.php'); ?>
    <?php include('includes/foot.php'); ?>

</body>

</html>