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

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

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

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

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

$brand = mysqli_fetch_assoc($result);

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

// Update brand
if(isset($_POST['update_brand'])){
    $category_id = intval($_POST['category_id']);
    $brand_name = mysqli_real_escape_string($conn, $_POST['brand_name']);
    $status = mysqli_real_escape_string($conn, $_POST['status']);

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

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

        <div class="flex items-center flex-wrap justify-between gap20 mb-30">
            <h3>Edit Brand</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="brand_list.php"><div class="text-tiny">Brands</div></a></li>
                <li><i class="icon-chevron-right"></i></li>
                <li><div class="text-tiny">Edit Brand</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">

                    <!-- FLEX ROW: Category + Brand Name + Status -->
                    <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">Category <span class="tf-color-1">*</span></div>
                            <select name="category_id" required>
                                <option value="">-- Select Category --</option>
                                <?php while($cat = mysqli_fetch_assoc($cat_result)){ ?>
                                    <option value="<?php echo $cat['id']; ?>" <?php if($brand['category_id']==$cat['id']) echo 'selected'; ?>>
                                        <?php echo htmlspecialchars($cat['category_name']); ?>
                                    </option>
                                <?php } ?>
                            </select>
                        </fieldset>

                        <!-- Brand Name -->
                        <fieldset class="status" 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" value="<?php echo htmlspecialchars($brand['brand']); ?>" 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>
                                <option value="Active" <?php if($brand['status']=='Active') echo 'selected'; ?>>Active</option>
                                <option value="Inactive" <?php if($brand['status']=='Inactive') echo 'selected'; ?>>Inactive</option>
                            </select>
                        </fieldset>

                    </div>

                </div>

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

            </form>

        </div>

    </div>
</div>

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