<?php
error_reporting(0);
session_start();
if(isset($_SESSION['username'])){

    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
        <meta charset="utf-8" />
        <?php include('includes/styles.php'); ?>
        <title>Clergies | <?php echo $church_name; ?></title>


    </head>
    <body class="fixed-header dashboard">
<?php include('includes/sidebar.php'); ?>
    <!-- START PAGE-CONTAINER -->
    <div class="page-container ">
        <?php include('includes/header.php'); ?>
        <!-- START PAGE CONTENT WRAPPER -->
        <div class="page-content-wrapper ">
            <!-- START PAGE CONTENT -->
            <div class="content sm-gutter">
                <!-- START CONTAINER FLUID -->
                <div class="container-fluid padding-25 sm-padding-10">
                   <!-- START BREADCRUMB -->
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="<?php echo $base_url; ?>">Home</a></li>
                    <li class="breadcrumb-item active">Clergies</li>
                </ol>
                <!-- END BREADCRUMB -->


                <section style="padding-bottom: 50px;padding-left: 0;" class="m-t-30 m-b-30 col-lg-12 connectedSortable">
                        <a href="<?php echo $base_url; ?>add-clergy"><button class="btn btn-outline-success pull-left btn-lg btn-animated from-left">
                                <span>Add Clergy</span>
                                <span class="hidden-block">
													<i class="pg-icon">user_add</i>
												</span></button> </a>
                    </section>


                <div class="row">
                    <section class="col-lg-12 connectedSortable">
                        <div class="card card-default">
                        <div class="card-header  ">
                            <div class="card-title"> Manage Clergies
                            </div>
                          </div>
                          <?php
                        $sql = "SELECT * FROM clergy";
                        $conn->query($sql);
                        $rowCount = $conn->rowCount();
                        $result = $conn->fetchMultiple();
                        ?>
                            <div class="card-body">
                                <table id="clergy" class="table table-bordered table-striped">
                                    <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>Name</th>
                                        <th>Designation</th>
                                        <th>District</th>
                                        <th>Last Conference Attended</th>
                                        <th>Registration Date</th>
                                        <th>Action</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php
                                    $i = 1;
                                    if($rowCount > 0){
                                        foreach ($result as $clergy):
                                            $name = $clergy->name;
                                            $email = $clergy->email;
                                            $clergy_id = $clergy->id;
                                            $designation= $clergy->designation;
                                            $district= $clergy->districtID;
                                            $conyear= $clergy->conyear;
                                            $regdate= $clergy->reg_date;

                                            ?>
                                            <?php
                                                $sql = "SELECT name FROM districts WHERE id=:id";
                                                $conn->query($sql);
                                                $conn->bind(":id", $district);
                                                $districtName = $conn->fetchColumn();
                                                ?> 
                                            <tr>
                                                <td><?php echo $i++; ?></td>
                                                <td><?php echo $name; ?></td>
                                                <td><?php echo $designation; ?></td>
                                                <td><?php echo $districtName; ?></td>
                                                <td><?php echo $conyear; ?></td>
                                                <?php  
    $orgDate = $regdate;  
    $newDate = date("l, jS F, Y h:i A", strtotime($orgDate)); 
?> 
                                                <td><?php echo $newDate; ?></td>
                                                <td>
                                                    <a href="<?php echo $base_url;?>view-clergy/<?php echo $clergy->id;?>" data-toggle="tooltip" title="View">
                                                        <i class="fa fa-check-square"></i>
                                                    </a>&nbsp;
                                                    <a href="<?php echo $base_url;?>edit-clergy/<?php echo $clergy->id;?>" data-toggle="tooltip" title="Edit">
                                                        <i class="fa fa-edit"></i>
                                                    </a>&nbsp;
                                                    <a href="javascript:void(0);" data-toggle="modal" data-target="#delete-clergy<?php echo $clergy_id;?>">
                                                        <i class="fa fa-trash" data-toggle="tooltip" title="Delete"></i>
                                                    </a>
                                                </td>
                                            </tr>
                                        <?php endforeach; ?>
                                        <?php
                                    }else{
                                        ?>
                                        <tr>
                                        <td colspan="6" style="border-bottom: 1px solid #dee2e6;padding-bottom: 100px;" class="text-center"><lottie-player src='<?php echo $jsonfound; ?>'  background='transparent'  speed='1'  style='width: 300px; height: 300px;margin: 20px auto;'  loop  autoplay></lottie-player>No Data in table</td>
                                        </tr>
                                        <?php
                                    }
                                    ?>
                                    </tbody>

                                    <tfoot>
                                    <tr>
                                    <th>#</th>
                                        <th>Name</th>
                                        <th>Designation</th>
                                        <th>District</th>
                                         <th>Last Conference Attended</th>
                                        <th>Registration Date</th>
                                        <th>Action</th>
                                    </tr>
                                    </tfoot>

                                </table>
                            </div>

                        </div>
                    </section>




                </div>
                    

                    
                    

                </div>
                <!-- END CONTAINER FLUID -->
            </div>
            <!-- END PAGE CONTENT -->
            <?php include('includes/foot.php'); ?>
        </div>
        <!-- END PAGE CONTENT WRAPPER -->
    </div>
    <!-- END PAGE CONTAINER -->
<?php include('includes/footer.php'); ?>
<?php
    foreach ($result as $clergy_details){
        $name = $clergy_details->name;
        $clergy_id = $clergy_details->id;
        ?>

        <!--        Delete clergy Modal Start-->
    <div class="modal fade slide-up disable-scroll" id="delete-clergy<?php echo $clergy_id; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-sm" role="document">
            <div class="modal-content-wrapper">
                <div class="modal-content text-center">
                    <div class="modal-header clearfix" style="display: block">
                        <button aria-label="" type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-icon">close</i>
                        </button>
                        <h5 style="margin-bottom: -6px"><span class="semi-bold">Delete Clergy 😞</span></h5>
                </div>
                <div class="modal-body">
                    <p class="p-b-10 p-t-30"><span>Are You Sure You Want To Delete <br></span> <span style="color: #f00; text-transform: uppercase"><?php echo $name; ?></span></p>
                    <form action="" method="post">
                        <div class="form-group">
                            <input type="hidden" name="id" value="<?php echo $clergy_id; ?>">
                        </div>

                        <div class="col-xl-12 m-t-50">
                            <button aria-label="" type="submit" name="delete_clergy" id="submit" class="btn btn-danger btn-lg btn-animated from-left">
                                <span>Delete</span>
                                <span class="hidden-block">
													<i class="pg-icon">trash_alt</i>
												</span></button>
                        </div>
                    </form>


                </div>
                </div>
            </div>
        </div>
    </div>
    <!--        Delete Student Modal End-->

       

        <?php
    }

  //delete clergy script

if(isset($_POST['delete_clergy'])){
    $id = $_POST['id'];
    //sql
    $sql = "DELETE FROM clergy WHERE id =:id";
    $conn->query($sql);
    $conn->bind(":id", $id);

    try{
        $conn->execute();
        echo "<script>
$(function () { //ready
          toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
          toastr.success('Clergy&#39;s Data Deleted Successfully');
      });
</script> <meta http-equiv='refresh' content='5; clergy'>";
    }catch (PDOException $err){
        echo $err->getMessage();
    }
}
?>

    <script>
        $('[data-toggle ="tooltip"]').tooltip();
    </script>

    <script>
        $(function () {
            $("#clergy").DataTable({
                "responsive": true, "lengthChange": false, "pageLength": 100, "autoWidth": false,
                "buttons": ["copy", "csv", "pdf", "print"]
            }).buttons().container().appendTo('#clergy_wrapper .col-md-6:eq(0)');
            $('#example2').DataTable({
                "paging": true,
                "lengthChange": false,
                "pageLength": 100,
                "searching": true,
                "ordering": true,
                "info": true,
                "autoWidth": false,
                "responsive": true
            });
        });
    </script>


<?php
}else{
    ?>
    
  <?php include('session.php'); ?>

    <?php

}

?>