<?php
include 'inc/header.php';

if (isset($_SESSION['user_role']) && $_SESSION['user_role'] == 'user') {
    echo "<script>window.location.href='index.php'</script>";
    exit;
}

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

$res = mysqli_query($conn, "SELECT * FROM sorting_options WHERE id=$id");
$opt = mysqli_fetch_assoc($res);

if (!$opt) {
    echo "<script>window.location.href='sorting_options.php'</script>";
    exit;
}
?>

<style>
body { background:#f6f7f9; }
.card { border-radius:12px; }
.form-section{
    background:#fafafa;
    border:1px dashed #ddd;
    border-radius:10px;
    padding:15px;
}
</style>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css">

<div class="card shadow mb-4">
    <div class="card-header py-3">
        <h6 class="m-0 font-weight-bold text-secondary">Edit Sorting Option</h6>
    </div>

    <div class="container my-4">

        <form id="editOptionForm">

            <input type="hidden" name="id" value="<?= $opt['id'] ?>">

            <!-- BASIC INFO -->
            <div class="card mb-3">
                <div class="card-body">
                    <h5>Basic Information</h5>

                    <!-- GROUP -->
                    <div class="row mt-3">
                        <div class="col-md-6">
                            <label class="form-label">Sorting Group</label>
                            <select name="sorting_group_id" class="form-control" required>
    <option value="">Select Group</option>

    <?php
    $gq = mysqli_query($conn,"
        SELECT sg.id, sg.name, c.name AS cat_name
        FROM sorting_groups sg
        LEFT JOIN categories c ON c.id = sg.category_id
        ORDER BY sg.name ASC
    ");

    while ($g = mysqli_fetch_assoc($gq)) {
        $sel = ($g['id'] == $opt['sorting_group_id']) ? 'selected' : '';
        echo "<option value='{$g['id']}' $sel>
                {$g['cat_name']} → {$g['name']}
              </option>";
    }
    ?>
</select>

                        </div>

                        <div class="col-md-6">
                            <label class="form-label">Option Name</label>
                            <input type="text"
                                   name="name"
                                   value="<?= htmlspecialchars($opt['name']) ?>"
                                   class="form-control"
                                   required>
                        </div>
                    </div>

                    <!-- SORT + STATUS -->
                    <div class="row mt-3">
                        <div class="col-md-6">
                            <label class="form-label">Sort Order</label>
                            <input type="number"
                                   name="sort_order"
                                   value="<?= (int)$opt['sort_order'] ?>"
                                   class="form-control">
                        </div>

                        <div class="col-md-6">
                            <label class="form-label">Status</label>
                            <select name="status" class="form-control">
                                <option value="active" <?= $opt['status']=='active'?'selected':'' ?>>Active</option>
                                <option value="inactive" <?= $opt['status']=='inactive'?'selected':'' ?>>Inactive</option>
                            </select>
                        </div>
                    </div>

                </div>
            </div>

            <button type="submit" class="btn btn-primary">
                Update Option
            </button>

            <a href="show_sorting_options.php" class="btn btn-secondary">
                Cancel
            </a>

        </form>
    </div>
</div>

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

<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>

<script>
toastr.options = {
    closeButton: true,
    progressBar: true,
    positionClass: "toast-top-right",
    timeOut: "3000"
};

$('#editOptionForm').submit(function(e){
    e.preventDefault();

    $.post('api/update_sorting_option.php', $(this).serialize(), function(r){

        if (r.status === 'success') {
            toastr.success(r.message || 'Option updated');

            setTimeout(() => {
                window.location.href = 'show_sorting_options.php';
            }, 1200);

        } else {
            toastr.error(r.message || 'Update failed');
        }

    }, 'json');
});
</script>
