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

// Get category ID from GET
if(!isset($_GET['id']) || empty($_GET['id'])){
    echo "<script>alert('Invalid Category ID'); window.location.href='category_list.php';</script>";
    exit;
}

$id = intval($_GET['id']);

// Fetch category data
$query = "SELECT * FROM category WHERE id = $id";
$result = mysqli_query($conn, $query);

if(mysqli_num_rows($result) == 0){
    echo "<script>alert('Category not found'); window.location.href='category_list.php';</script>";
    exit;
}

$category = mysqli_fetch_assoc($result);

// Update category
if(isset($_POST['update_category'])){
    $category_name = mysqli_real_escape_string($conn, $_POST['category_name']);
    $status = isset($_POST['status']) ? intval($_POST['status']) : 0;

    if(!empty($category_name)){
        $update_query = "UPDATE category SET category_name='$category_name', status='$status' WHERE id=$id";
        if(mysqli_query($conn, $update_query)){
            echo "<script>alert('Category updated successfully'); window.location.href='category_list.php';</script>";
            exit;
        } else {
            $error = "Error updating category.";
        }
    } else {
        $error = "Category name cannot be empty.";
    }
}
?>

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

        <div class="flex items-center flex-wrap justify-between gap20 mb-30">
            <h3>Edit Category</h3>
            <ul class="breadcrumbs flex items-center flex-wrap justify-start gap10">
                <li><a href="index.php"><div class="text-tiny">Dashboard</div></a></li>
                <li><i class="icon-chevron-right"></i></li>
                <li><a href="category-list.php"><div class="text-tiny">Category</div></a></li>
                <li><i class="icon-chevron-right"></i></li>
                <li><div class="text-tiny">Edit Category</div></li>
            </ul>
        </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">
    <div class="flex gap20 flex-wrap">

        <!-- Category Name -->
        <fieldset class="name flex-1">
            <div class="body-title mb-10">
                Category Name <span class="tf-color-1">*</span>
            </div>
            <input type="text" name="category_name" value="<?php echo htmlspecialchars($category['category_name']); ?>" required>
        </fieldset>

        <!-- Status -->
        <fieldset class="name flex-1">
            <div class="body-title mb-10">
                Status <span class="tf-color-1">*</span>
            </div>
            <select name="status" required>
                <option value="1" <?php if($category['status']==1) echo 'selected'; ?>>Active</option>
                <option value="0" <?php if($category['status']==0) echo 'selected'; ?>>Inactive</option>
            </select>
        </fieldset>

    </div>
</div>

                <div class="cols gap10">
                    <button type="submit" name="update_category" class="tf-button w380">Update Category</button>
                    <a href="category_list.php" class="tf-button style-3 w380">Cancel</a>
                </div>

            </form>
        </div>

    </div>
</div>

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