<?php 
session_start();
include 'includes/header.php'; 
include 'includes/sidebar.php'; 
include 'includes/connect.php';

error_reporting(E_ALL);
ini_set('display_errors', 1);

/* GET CATEGORY */
$category_id = isset($_GET['category_id']) ? intval($_GET['category_id']) : 0;

/* FETCH CATEGORY NAME */
$category_name = "";

if($category_id > 0){
    $cat_query = mysqli_query($conn, "SELECT category_name FROM category WHERE id = '$category_id' LIMIT 1");
    if($cat_query && mysqli_num_rows($cat_query) > 0){
        $cat_row = mysqli_fetch_assoc($cat_query);
        $category_name = $cat_row['category_name'];
    }
}

/* ================= QUERY ================= */
$query = "
SELECT 
    lc.id,
    lc.label_name,
    COUNT(DISTINCT im.brand) as brands,
    SUM(im.tray_no) as trays,
    AVG(DATEDIFF(CURDATE(), im.creation_date)) as days
FROM lifeline_configuration lc
LEFT JOIN inventory_management im ON im.variety_id = lc.id
WHERE lc.category_id = '$category_id'
GROUP BY lc.id
ORDER BY LOWER(lc.label_name) ASC
";

$result = mysqli_query($conn, $query);
?>

<style>

.main-content-wrap {
    background: #f8fafc;
}

/* ===== BREADCRUMB ===== */
.breadcrumb {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
    font-size: 15px;
    margin-bottom: 22px;
    padding: 14px 18px;
    background: linear-gradient(135deg, #ffffff, #f9fafb);
    border-radius: 12px;
    box-shadow: 0 6px 18px rgba(0,0,0,0.06);
    font-family: "Segoe UI", Roboto, Arial, sans-serif;
}

.breadcrumb a {
    text-decoration: none;
    color: #ff7a18;
    font-weight: 600;
    transition: all 0.2s ease;
}

.breadcrumb a:hover {
    color: #e65c00;
    text-decoration: underline;
}

.breadcrumb span {
    color: #374151;
    font-weight: 500;
}

.breadcrumb .divider {
    color: #9ca3af;
    margin: 0 6px;
}

/* ===== GRID ===== */
.variety-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(270px, 1fr));
    gap: 22px;
}

/* ===== CARD ===== */
.variety-card {
    background: linear-gradient(135deg, #ffffff, #f9fafb);
    border-radius: 16px;
    padding: 18px;
    box-shadow: 0 8px 25px rgba(0,0,0,0.06);
    transition: all 0.3s ease;
    position: relative;
    border: 1px solid rgba(0,0,0,0.05);
    cursor: pointer;
    overflow: hidden;
}

.variety-card:hover {
    transform: translateY(-6px);
    box-shadow: 0 15px 35px rgba(0,0,0,0.12);
}

/* Hover Overlay */
.variety-card::after {
    content: "View Brands →";
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(34, 197, 94, 0.15);
    color: #111;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    font-weight: 600;
    opacity: 0;
    transition: all 0.3s ease;
}

.variety-card:hover::after {
    opacity: 1;
}

/* Top Gradient */
.variety-card::before {
    content: "";
    position: absolute;
    height: 4px;
    width: 100%;
    top: 0;
    left: 0;
    border-radius: 16px 16px 0 0;
    background: linear-gradient(90deg, #22c55e, #4ade80);
}

/* HEADER */
.variety-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 15px;
}

.variety-icon {
    width: 42px;
    height: 42px;
    border-radius: 10px;
    background: linear-gradient(135deg, #ecfdf5, #d1fae5);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
}

.variety-title {
    font-size: 16px;
    font-weight: 600;
    color: #111827;
}

/* STATS */
.variety-stat {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 0;
    border-bottom: 1px dashed #e5e7eb;
    font-size: 13px;
}

.variety-stat:last-child {
    border-bottom: none;
}

.variety-stat span:first-child {
    color: #6b7280;
}

/* BADGES */
.badge {
    padding: 6px 12px;
    border-radius: 8px;
    font-size: 11px;
    font-weight: 600;
}

.badge-green {
    background: #dcfce7;
    color: #166534;
}

.badge-blue {
    background: #dbeafe;
    color: #1e40af;
}

.badge-orange {
    background: #fef3c7;
    color: #92400e;
}

/* EMPTY */
.no-data {
    text-align: center;
    padding: 40px;
    color: #6b7280;
}

</style>

<div class="main-content-inner">
<div class="main-content-wrap">

<!-- ===== BREADCRUMB ===== -->
<div class="breadcrumb">
    <span>🏠</span>
    <a href="index.php">Home</a>

    <span class="divider">›</span>
    <a href="products.php">Products</a>

    <span class="divider">›</span>
    <span><?php echo htmlspecialchars($category_name ?: 'Category'); ?></span>

    <span class="divider">›</span>
    <span>Varieties</span>
</div>

<div class="flex items-center justify-between mb-30">
    <h3 style="font-size:20px; font-weight:600;">
        🌿 <?php echo htmlspecialchars($category_name ?: 'Varieties'); ?>
    </h3>
</div>

<div class="variety-grid">

<?php if(mysqli_num_rows($result) > 0){ ?>

<?php while($row = mysqli_fetch_assoc($result)){ ?>

<div class="variety-card" 
onclick="window.location.href='brands.php?category_id=<?php echo $category_id; ?>&variety_id=<?php echo $row['id']; ?>'">

    <div class="variety-header">
        <div class="variety-icon">🌱</div>
        <div class="variety-title">
            <?php echo htmlspecialchars($row['label_name']); ?>
        </div>
    </div>

    <div class="variety-stat">
        <span>Brands</span>
        <span class="badge badge-blue"><?php echo $row['brands'] ?? 0; ?></span>
    </div>

    <div class="variety-stat">
        <span>Available Trays</span>
        <span class="badge badge-green"><?php echo $row['trays'] ?? 0; ?></span>
    </div>

    <div class="variety-stat">
        <span>Avg Growing Days</span>
        <span class="badge badge-orange"><?php echo round($row['days'] ?? 0); ?> days</span>
    </div>

</div>

<?php } ?>

<?php } else { ?>

<div class="no-data">
    No varieties found
</div>

<?php } ?>

</div>

</div>
</div>

<?php include 'includes/footer.php'; ?>