<?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['category'];
    $title = $_POST['title'];
    $description = $_POST['b'];

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

    $floder = "img_uploads/" . $file_name;

    move_uploaded_file($tmp_name, $floder);


    $sql = "insert into `product` (category_name,product_name,image,description) values('$category_name','$title','$file_name','$description')";
    $result = mysqli_query($con, $sql);
    if ($result) {
        // echo '<script>alert("product added")</script>';
        header('Location:product.php');
    } else {
        echo '<script>alert("product not added")</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 Products</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
                                    Category </a>
                            </li>
                        </ul>
                    </div>


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

                        <div class="col-lg-12">
                            <div class="form-group">
                                <?php
                                $sql = "SELECT * FROM `category`";
                                $result = mysqli_query($con, $sql);
                                $category_name = array(); // Initialize an empty array to store category names
                                
                                if ($result) {
                                    while ($row = mysqli_fetch_assoc($result)) {
                                        $category_name[] = $row['category_name'];
                                    }
                                }
                                ?>

                                <label>Category Name :</label>
                                <select name="category" class="form-control">
                                    <option value="" selected disabled>Select Category Name :</option>
                                    <?php
                                    foreach ($category_name as $key) {
                                        echo "<option value='$key'>$key</option>";
                                    }
                                    ?>
                                </select>


                            </div>
                        </div>

                        <div class="col-lg-12">
                            <div class="form-group mb-0">
                                <label>Select Product 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>Product Name :</label>

                                <input class="form-control" placeholder="Enter  Product Name....." name="title"
                                    required>



                            </div>
                        </div>

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

                                <textarea id="editor" class="form-control" style="height: 500px;"
                                    placeholder="Enter  Product 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>