<?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($_POST['submit'])) {
    $category_name = $_POST['c_name'];
    $category_description = $_POST['b'];

    $file_name = $_FILES['image']['name'];
    $tmp_name = $_FILES['image']['tmp_name'];

    $folder = "img_uploads/" . $file_name;

    move_uploaded_file($tmp_name, $folder);

    // Assuming $con is your database connection
    $sql_count = "SELECT COUNT(*) as category_count FROM `category`";
    $result_count = mysqli_query($con, $sql_count);
    $row_count = mysqli_fetch_assoc($result_count);
    $category_count = $row_count['category_count'];

    if ($category_count < 5) {
        $sql_insert = "INSERT INTO `category` (category_name,image,description) VALUES ('$category_name','$file_name','$category_description')";
        $result_insert = mysqli_query($con, $sql_insert);

        if ($result_insert) {
            echo '<script>alert("Successfully Added Category")</script>';
            echo '<script>window.location.href = "category.php";</script>';
        } else {
            echo '<script>alert("Failed To Add 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>Add Category</h5>
                    <div class="list-btn">
                        <ul>
                            <li>
                                <a class="btn btn-primary" href="add-product.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 mb-0">
                                <label>Select Category Image :</label>
                            </div>
                            <div class="form-uploads mb-4">
                                <div class="form-uploads-path">
                                    <img src="assets/img/icons/upload.svg" alt="img">
                                    <div class="file-browse">

                                        <div class="file-browse-path">
                                            <input type="file" name="image" required>
                                            <a href="javascript:void(0);" style="cursor: pointer;">Browse</a>
                                        </div>
                                        <h6> To Select Image </h6>
                                    </div>
                                    <h5>Supported formates: JPEG, PNG, JPG.</h5>
                                </div>
                            </div>
                        </div>

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

                                <input type="text" class="form-control" placeholder="Enter Category Name..."
                                    name="c_name" 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 Description....." name="b"></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('b');
</script>