<?php

require_once 'auth.php';

// Check if the user is authenticated as an admin
checkAdminAuth();





include("config.php");

if (isset($_POST['delete'])) {
    $getid = $_POST['action_plan'];

    $sql = "delete from `action_plan` where id='$getid'";
    $query = mysqli_query($con, $sql);

    if ($query) {

        echo "<script>
        alert('Data Deleted Successfully');
        window.location.href = 'action-plan-list.php'; 
      </script>";
    } else {
        echo "<script>alert('Something Wrong')</script>";
    }
}

?>








<!DOCTYPE html>
<html lang="en">


<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
    <title>Admin</title>

    <link rel="shortcut icon" href="assets/img/favicon.png">

    <link rel="stylesheet" href="assets/css/select2.min.css">

    <link rel="stylesheet" href="assets/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="assets/plugins/bootstrap-tagsinput/css/bootstrap-tagsinput.css">

    <link rel="stylesheet" href="assets/plugins/fontawesome/css/fontawesome.min.css">
    <link rel="stylesheet" href="assets/plugins/fontawesome/css/all.min.css">

    <link rel="stylesheet" href="assets/css/dataTables.bootstrap4.min.css">

    <link rel="stylesheet" href="assets/plugins/feather/feather.css">

    <link rel="stylesheet" href="assets/css/style.css">
</head>

<body>
    <div class="main-wrapper">

        <?php

        include("header.php");
        include("sidebar.php");

        ?>

        <div class="page-wrapper page-settings">
            <div class="content">
                <div class="content-page-header content-page-headersplit mb-0">
                    <h5> Action Plan List</h5>
                    <div class="list-btn">
                        <ul>


                            <li>
                                <a class="btn btn-primary" href="action-plan.php"><i
                                        class="fa fa-plus me-2"></i>Add Action Plan </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>Year </th>
                                        <th>District</th>
                                        <th>Description</th>
                                        <th>Constituency</th>
                                        <th>Status</th>
                                        <th>Document</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>

                                    <?php
                                    $sql = "select * from `action_plan`";
                                    $query = mysqli_query($con, $sql);

                                    if ($query) {
                                        $index = 1;
                                        foreach ($query as $items) {
                                            $i = $index++;
                                            $id = $items['id'];
                                            $year = $items['year'];
                                            $district = $items['district'];
                                            $description = $items['description'];
                                            $constituency = $items['constituency'];
                                            $status = $items['status'];

                                            $document = $items['document']; ?>


                                            <tr>
                                                <td><?php echo $i ?></td>
                                                <td><?php echo $year ?></td>
                                                <td><?php echo $district ?></td>
                                                <td><?php echo $description ?></td>
                                                <td><?php echo $constituency ?></td>
                                                <td><?php echo $status ?></td>

                                                <td>
                                                    <a href="image/<?php echo $document; ?>" target="_blank">View PDF</a>
                                                </td>



                                                <td>
                                                    <div class="table-actions d-flex">
                                                        <a class="delete-table me-2" href="action-plan-edit.php?edit=<?php echo $id ?>">
                                                            <img src="assets/img/icons/edit.svg" alt="svg">
                                                        </a>
                                                        <a class="delete-table" href="javascript:void(0);"
                                                            data-bs-toggle="modal" data-bs-target="#delete-item" data-category-id="<?php echo $id ?>">
                                                            <img src="assets/img/icons/delete.svg" alt="svg">
                                                        </a>
                                                    </div>
                                                </td>
                                            </tr>

                                    <?php }
                                    } ?>


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


    <div class="modal fade" id="delete-item" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Delete Action Plan</h5>
                    <button type="button" class="btn-close close-modal" data-bs-dismiss="modal" aria-label="Close">
                        <i class="fe fe-x"></i>
                    </button>
                </div>
                <form method="post">
                    <input type="hidden" name="action_plan" id="category_id" values="">
                    <div class="modal-body py-0">
                        <div class="del-modal">
                            <p>Are you sure want to Delete?</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" class="btn btn-primary" name="delete">Yes</button>
                    </div>
                </form>
            </div>
        </div>
    </div>


    <script data-cfasync="false" src="../../cdn-cgi/scripts/5c5dd728/cloudflare-static/email-decode.min.js"></script>
    <script src="assets/js/jquery-3.6.4.min.js"></script>

    <script src="assets/js/select2.min.js"></script>

    <script src="assets/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>

    <script src="assets/plugins/bootstrap-tagsinput/js/bootstrap-tagsinput.js"></script>

    <script src="assets/js/feather.min.js"></script>

    <script src="assets/js/jquery.dataTables.min.js"></script>
    <script src="assets/js/dataTables.bootstrap4.min.js"></script>

    <script src="assets/plugins/slimscroll/jquery.slimscroll.min.js"></script>

    <script src="assets/plugins/sweetalert/sweetalert2.all.min.js"></script>
    <script src="assets/plugins/sweetalert/sweetalerts.min.js"></script>

    <script src="assets/js/admin.js"></script>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // Delegate event listener to a parent container (or document)
            document.addEventListener('click', function(e) {
                if (e.target && e.target.closest('.delete-table')) {
                    // Get the clicked element
                    var deleteLink = e.target.closest('.delete-table');

                    // Get the category ID from the data attribute
                    var categoryId = deleteLink.getAttribute('data-category-id');

                    // Set the category_id input field value in the modal form
                    document.getElementById('category_id').value = categoryId;

                    // Show the modal
                    var deleteModal = new bootstrap.Modal(document.getElementById('delete-item'));
                    deleteModal.show();
                }
            });

            // Close modal properly
            var deleteModalElement = document.getElementById('delete-item');

            deleteModalElement.addEventListener('hidden.bs.modal', function() {
                // Ensure the backdrop is properly removed when modal is closed
                document.body.classList.remove('modal-open');
                document.querySelector('.modal-backdrop')?.remove();
            });
        });
    </script>


</body>


</html>