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

// Insert Code
if(isset($_POST['submit_category'])){

    $category_id = intval($_POST['category_id']);
    $label_name = mysqli_real_escape_string($conn, $_POST['label_name']);
    $start_day = intval($_POST['start_day']);
    $end_day = intval($_POST['end_day']);

    // Basic validation
    if($category_id == 0){
        $error = "Please select a category.";
    } elseif(empty($label_name)){
        $error = "Label Name is required.";
    } elseif(!is_numeric($start_day) || $start_day < 0){
        $error = "Start Day must be a positive number.";
    } elseif(!is_numeric($end_day) || $end_day < $start_day){
        $error = "End Day must be a number greater than or equal to Start Day.";
    } else {
        // Insert into lifeline_configuration
        $query = "INSERT INTO lifeline_configuration (category_id, label_name, created_at)
                  VALUES ($category_id, '$label_name',  NOW())";

        if(mysqli_query($conn, $query)){
            echo "<script>alert('Variety Added Successfully'); window.location.href='lifeline_list.php';</script>";
            exit;
        } else {
            $error = "Error: " . mysqli_error($conn);
        }
    }
}

// Fetch categories for dropdown
$cat_query = "SELECT id, category_name FROM category ORDER BY category_name ASC";
$cat_result = mysqli_query($conn, $cat_query);
?>

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

        <div class="flex items-center flex-wrap justify-between gap20 mb-30">
            <h3>Add Variety</h3>
        </div>

        <div class="wg-box">
            <?php if(isset($error)){ ?>
                <div class="text-red-500 mb-4"><?php echo $error; ?></div>
            <?php } ?>

            <form class="form-add-category" method="POST">

                <div class="wg-box mb-30">

                    <!-- FLEX ROW: Category + Label + Start Day + End Day -->
                    <div style="display:flex; gap:20px; flex-wrap:wrap;">

                        <!-- Category Dropdown -->
                        <fieldset class="name" style="flex:1; min-width:200px;">
                            <div class="body-title mb-10">
                                Crop <span class="tf-color-1">*</span>
                            </div>
                            <select name="category_id" required>
                                <option value="">Select Crop</option>
                                <?php while($cat = mysqli_fetch_assoc($cat_result)){ ?>
                                    <option value="<?php echo $cat['id']; ?>"><?php echo htmlspecialchars($cat['category_name']); ?></option>
                                <?php } ?>
                            </select>
                        </fieldset>

                        <!-- Label Name -->
                        <fieldset class="name" style="flex:1; min-width:200px;">
                            <div class="body-title mb-10">
                                Variety <span class="tf-color-1">*</span>
                            </div>
                            <input type="text" name="label_name" placeholder="Enter Variety" required>
                        </fieldset>

                        <!-- Start Day -->
                        <!--<fieldset class="name" style="flex:1; min-width:150px;">-->
                        <!--    <div class="body-title mb-10">-->
                        <!--        Start Day <span class="tf-color-1">*</span>-->
                        <!--    </div>-->
                        <!--    <input type="number" name="start_day" placeholder="Start Day" min="0" required>-->
                        <!--</fieldset>-->

                        <!--<fieldset class="name" style="flex:1; min-width:150px;">-->
                        <!--    <div class="body-title mb-10">-->
                        <!--        End Day <span class="tf-color-1">*</span>-->
                        <!--    </div>-->
                        <!--    <input type="number" name="end_day" placeholder="End Day" min="0" required>-->
                        <!--</fieldset>-->

                    </div>

                </div>

                <div class="cols gap10">
                    <button class="tf-button w380" type="submit" name="submit_category">
                        Add Variety
                    </button>

                    <a href="lifeline_list.php" class="tf-button style-3 w380">
                        Cancel
                    </a>
                </div>

            </form>

        </div>

    </div>
</div>

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