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

// ERROR SHOW (DEBUG)
error_reporting(E_ALL);
ini_set('display_errors', 1);

// INSERT
if(isset($_POST['submit_brand'])){

    $category_id = intval($_POST['category_id']);
    $variety_id  = intval($_POST['variety_id']);
    $brand_name  = trim(mysqli_real_escape_string($conn, $_POST['brand_name']));
    $status      = mysqli_real_escape_string($conn, $_POST['status']);

    if($category_id > 0 && $variety_id > 0 && !empty($brand_name)){

        $query = "INSERT INTO brand (category_id, variety_id, brand, status, created_at) 
                  VALUES ($category_id, $variety_id, '$brand_name', '$status', NOW())";

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

    } else {
        $error = "Please select crop, variety and enter brand name.";
    }
}

// FETCH CATEGORY
$cat_result = mysqli_query($conn, "SELECT * FROM category ORDER BY category_name ASC");
?>

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

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

        <div class="wg-box">

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

            <form method="POST">

                <div class="wg-box mb-30" style="display:flex; gap:20px; flex-wrap:wrap;">

                    <!-- CROP -->
                    <fieldset style="flex:1; min-width:200px;">
                        <div class="body-title mb-10">Crop <span class="tf-color-1">*</span></div>
                        <select name="category_id" id="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>

                    <!-- VARIETY -->
                    <fieldset style="flex:1; min-width:200px;">
                        <div class="body-title mb-10">Variety <span class="tf-color-1">*</span></div>
                        <select name="variety_id" id="variety_id" required>
                            <option value="">Select Variety</option>
                        </select>
                    </fieldset>

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

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

                </div>

                <div class="cols gap10">
                    <button type="submit" name="submit_brand" class="tf-button w380">Add Brand</button>
                    <a href="brand_list.php" class="tf-button style-3 w380">Cancel</a>
                </div>

            </form>
        </div>

    </div>
</div>

<!-- JQUERY -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<!-- AJAX FOR VARIETY -->
<script>
$('#category_id').on('change', function(){
    var category_id = $(this).val();

    if(category_id != ''){
        $.ajax({
            url: 'get_variety_brand_page.php',
            type: 'POST',
            data: {category_id: category_id},
            success: function(data){
                $('#variety_id').html(data);
            }
        });
    } else {
        $('#variety_id').html('<option value="">Select Variety</option>');
    }
});
</script>

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