<?php include('db_connect.php');

// Check if the category ID is set
if(isset($_GET['id'])) {
    // Prepare the SQL query
    $stmt = $conn->prepare("SELECT * FROM categories WHERE id=?");
    // Bind the category ID parameter
    $stmt->bind_param("i", $_GET['id']);
    // Execute the query
    $stmt->execute();
    // Get the result
    $result = $stmt->get_result();
    // Fetch the record
    $record = $result->fetch_assoc();
    // Close the statement
    $stmt->close();
} else {
    // If no category ID is provided, handle the error or redirect as needed
    // For example:
    // header("Location: error.php");
    // exit();
}

?>

<div class="container-fluid">
    <div class="col-lg-12">
        <div class="row">
            <!-- FORM Panel -->
            <div class="col-md-4">
                <form action="" id="manage-category">
                    <div class="card">
                        <div class="card-header">
                            Edit Category Form
                        </div>
                        <div class="card-body">
                            <input type="hidden" name="id" value="<?php echo $record['id']; ?>">
                            <div class="form-group">
                                <label class="control-label">Name</label>
                                <input type="text" class="form-control" name="name" value="<?php echo $record['name']; ?>">
                            </div>
                        </div>
                        <div class="card-footer">
                            <div class="row">
                                <div class="col-md-12">
                                    <button class="btn btn-sm btn-primary col-sm-3 offset-md-3">Save</button>
                                    <button class="btn btn-sm btn-default col-sm-3" type="button" onclick="$('#manage-category').get(0).reset()">Cancel</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <!-- FORM Panel -->
        </div>
    </div>
</div>

<style>
    td {
        vertical-align: middle !important;
    }
</style>
<script>
    $('#manage-category').submit(function(e){
        e.preventDefault();
        start_load();
        $.ajax({
            url:'ajax.php?action=edit_cate',
            data: new FormData($(this)[0]),
            cache: false,
            contentType: false,
            processData: false,
            method: 'POST',
            type: 'POST',
            success:function(resp){
                if(resp==1){
                    alert_toast("Data successfully added",'success');
                    setTimeout(function(){
                        window.location.href = 'index.php?page=manage_categories'; // Redirect to manage-category.php
                    },1500);
                }
                else if(resp==2){
                    alert_toast("Data successfully updated",'success');
                    setTimeout(function(){
                        location.reload();
                    },1500);
                }
            }
        });
    });
</script>