<?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_name = mysqli_real_escape_string($conn, $_POST['category_name']);
    $status = mysqli_real_escape_string($conn, $_POST['status']); // new field

    if(!empty($category_name)){

        $query = "INSERT INTO category (category_name, status, created_at) 
                  VALUES ('$category_name', '$status', NOW())";

        if(mysqli_query($conn, $query)){
            echo "<script>alert('Category Added Successfully'); window.location.href='category_list.php';</script>";
        } else {
            echo "<script>alert('Error: Something went wrong');</script>";
        }

    } else {
        echo "<script>alert('Please enter category name');</script>";
    }
}
?>

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

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

        <form class="form-add-category" method="POST">
            
            <div class="wg-box mb-30">

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

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

                    <!-- Status Dropdown -->
                    <fieldset class="status" style="flex:1; min-width:150px;">
                        <div class="body-title mb-10">
                            Status <span class="tf-color-1">*</span>
                        </div>
                        <select name="status" required class="mb-10">
                            <option value="Active" selected>Active</option>
                            <option value="Inactive">Inactive</option>
                        </select>
                    </fieldset>

                </div>

            </div>

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

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

        </form>

    </div>
</div>

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