<?php
session_start();
if (!isset($_SESSION['user']) || $_SESSION['user'] !== 'pacific') {
    header('Location:login.php');
}

include 'include/database.php'; ?>

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


<?php include 'include/sidebar.php'; ?>



<?php
if (isset($_GET['up'])) {
    $cid = $_GET['up'];
    $sql = "select * from `category` where id=$cid";
    $result = mysqli_query($con, $sql);
    if ($result) {
        foreach ($result as $key) {
            $category_name = $key['category_name'];
            $image = $key['image'];
            $description = $key['description'];

        }
    }
}



if (isset($_POST['submit'])) {
    $category_name = $_POST['title'];
    $description = $_POST['b'];

    $old_image = $_POST['old_image'];
    $new_image = $_FILES['new_image']['name'];

    if ($new_image == "") {
        $floder = $old_image;
    } else {
        $floder = $new_image;

        $temp_name = $_FILES['new_image']['tmp_name'];
        $update_image = "img_uploads/" . $floder;

        // $update_image1=$update_image;
        move_uploaded_file($temp_name, $update_image);
    }
    $sql = "UPDATE `category` c
    LEFT JOIN `product` p ON c.category_name = p.category_name
    SET c.id='$cid', c.category_name = '$category_name',
        c.image = '$floder',c.description='$description',
     
        p.category_name = '$category_name' 
    WHERE c.id = $cid";


    $result = mysqli_query($con, $sql);


    if ($result) {
        echo '<script>alert("Successfully Updated Category.")</script>';

        echo '<script>window.location.href = "category.php";</script>';


    } else {
        echo '<script>alert("Failed To Update Category. ")</script>';

    }
}

?>


<div class="page-wrapper" style="background-color: #aad1e0;">
    <div class="content">

        <div class="row">

            <div class="col-lg-10 col-sm-12 m-auto">
                <div class="content-page-header content-page-headersplit mb-4">
                    <h5>Update Category</h5>
                    <div class="list-btn">
                        <ul>
                            <li>
                                <a class="btn btn-primary" href="add-category.php"><i class="fa fa-plus me-2"></i>Add
                                    Product </a>
                            </li>
                        </ul>
                    </div>


                </div>
                <form method="post" enctype="multipart/form-data">
                    <div class="row">


                        <div class="col-lg-12">
                            <div class="form-group">
                                <label>Category Image </label>
                                <input type="hidden" name="old_image" class="form-control"
                                    placeholder="Select News image" value="<?php echo $image ?>">
                                <input type="file" name="new_image" class="form-control"
                                    placeholder="Select News image">

                            </div>
                        </div>

                        <div class="col-lg-12">
                            <div class="form-group">
                                <label>Category Name :</label>

                                <input type="text" class="form-control" placeholder="Enter  Category Title....."
                                    value="<?php echo $category_name ?>" name="title" required>



                            </div>
                        </div>

                        <div class="col-lg-12">
                            <div class="form-group">
                                <label> Category Description </label>

                                <textarea id="editor" class="form-control" style="height: 500px;"
                                    placeholder="Enter Category Details....."
                                    name="b"><?php echo $description ?></textarea>


                            </div>
                        </div>


                        <div class="col-lg-12">
                            <div class="btn-path">

                                <button type="submit" name="submit" class="btn btn-submit "> Submit</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

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

<script>
    // CKEDITOR.replace('title');

    CKEDITOR.replace('b');
</script>