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


include 'include/database.php'; ?>

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


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

        <div class="content-page-header content-page-headersplit mb-0">
            <h5>All Categories Details</h5>

            <div class="page-headers">
                <div class="search-bar">
                    <span><i class="fe fe-search"></i></span>
                    <input type="text" placeholder="Search....." id="searchInput" class="form-control">
                </div>
            </div>



            <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>
        <div class="row">
            <div class="col-12 ">
                <div class="table-resposnive table-div">
                    <table class="table datatable">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Category_Name</th>
                                <th>Category_Image</th>
                                <th>Category_Description</th>

                                <th>Action</th>

                            </tr>
                        </thead>

                        <tbody>

                            <?php
                            $index = 1;
                            $sql = "select * from `category`";
                            $result = mysqli_query($con, $sql);
                            if ($result) {
                                foreach ($result as $key) {
                                    $id = $key['id'];
                                    $category_name = $key['category_name'];
                                    $image = $key['image'];
                                    $category_description = $key['description'];

                                    $i = $index++
                                        ?>

                                    <tr>
                                        <td class="main_id" style="display:none;">
                                            <?php echo $id ?>
                                        </td>
                                        <td>
                                            <?php echo $i ?>
                                        </td>
                                        <td>
                                            <?php echo $category_name ?>
                                        </td>
                                        <td>
                                            <div class="table-imgname">
                                                <img src="img_uploads/<?php echo $image ?>" class="me-2" alt="img">

                                            </div>
                                        </td>

                                        <td>
                                            <div class="scrollable-td">
                                                <?php echo $category_description ?>
                                            </div>
                                        </td>

                                        <td>
                                            <div class="table-actions d-flex">
                                                <a class="delete-table me-2" href="category-update.php?up=<?php echo $id ?>">
                                                    <img src="assets/img/icons/edit.svg" alt="svg">
                                                </a>

                                                <a class="delete-table me-2 banner_image_id" style="cursor: pointer;">
                                                    <img src="assets/img/icons/delete.svg" alt="svg">
                                                </a>
                                            </div>
                                        </td>
                                    </tr>

                                <?php }
                            }

                            ?>

                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<?php
if (isset($_POST['yes'])) {
    $delete_id = $_POST['form-post-id'];
    // $delsql = "delete from `category` where id=$delete_id ";
    $delsql = "DELETE c, p
           FROM category c
           LEFT JOIN product p ON c.category_name = p.category_name
           WHERE c.id = $delete_id";
    $delresult = mysqli_query($con, $delsql);
    if ($delresult) {
        echo '<script>alert("Successfully Deleted Category")</script>';

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

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

    }
}
?>


<div class="modal fade" id="pop_up_id" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered ">
        <div class="modal-content">

            <form action="#" method="post">
                <div class="modal-header">
                    <input type="text" name="form-post-id" id="form_id" style="display: none;">

                    <h5 class="modal-title" id="exampleModalLabel">Delete Categories</h5>
                    <button type="button" class="btn-close close-modal" data-bs-dismiss="modal" aria-label="Close">
                        <i class="fe fe-x"></i>
                    </button>
                </div>
                <div class="modal-body py-0">
                    <div class="del-modal">
                        <p>Are you sure want to Delete?</p>
                        <p class="pt-2" style="color: red;">Note:
                            If You Delete This Category, All Related  Products Will Be Deleted As
                            Well.
                        </p>
                    </div>

                </div>
                <div class="modal-footer pt-0">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" name="yes" class="btn btn-danger">Yes</button>
                </div>
            </form>
        </div>
    </div>
</div>




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



<!-- delete  -->
<script>
    $('.banner_image_id').click(function (e) {
        e.preventDefault();
        var EE = $(this).closest('tr').find('.main_id').text();
        // console.log(EE);
        $('#form_id').val(EE);

        $('#pop_up_id').modal('show');

    })
</script>

<script>
    $(document).ready(function () {

        $("#searchInput").on("keyup", function () {
            var value = $(this).val().toLowerCase();
            $(".datatable tbody tr").filter(function () {
                $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1);
            });
        });
    });
</script>