<?php
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>Members | <?php echo $church_name; ?></title>

</head>
<style>
    @media only screen and (min-width: 768px) {
        .modal .modal-dialog {
            width: 400px !important;
        }
    }

    .demo-card-scrollable {
        height: 100px;
    }
        .scrollable.scroll-content {
        height: 56px !important;
    }

    .table tbody tr td {
        padding: .75rem;
    }
    .dataTables_wrapper .row > div {
        flex-direction: column;
    }

    .table > tfoot > tr > td,
    .table > tfoot > tr > th{
    text-transform: uppercase;
    font-weight: 500;
    font-family: "Montserrat", -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
    font-size: 10.5px;
    letter-spacing: 0.06em;
    padding-top: 14px;
    padding-bottom: 14px;
    vertical-align: middle;
    color: #4b4b4b;
    border-top: none;}
</style>
<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 ROW -->
                <div class="row mb-5">
                    <div class="col-lg-4">
                        <div id="card-linear" class="card card-default">
                            <div class="card-header  ">
                                <div class="card-title">Total Members
                                </div>
                                <div class="card-controls">
                                    <ul>
                                        <li><a href="#" onclick="totalmembers()" class="card-refresh" data-toggle="refresh"><i class="card-icon card-icon-refresh"></i></a>
                                        </li>
                                        <li><a href="#" class="card-close" data-toggle="close"><i class="card-icon card-icon-close"></i></a>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                            <div class="card-body" id="total">
                                <h3>
                                    <span class="semi-bold"><?php
                                        $sql = "SELECT * FROM members";;
                                        $conn->query($sql);
                                        $total = $conn->rowCount();
                                        $num = $total;
                                        $totalNum = number_format($num)."<br>";
                                        ?></span>

                                    <?php echo $totalNum; ?></h3>

                            </div>
                        </div>
                    </div>
                    <div class="col-lg-4">
                        <div id="card-circular" class="card card-default">
                            <div class="card-header  ">
                                <div class="card-title">Total Males &nbsp;
                                </div>
                                <div class="card-controls">
                                    <ul>
                                        <li><a href="#" onclick="totalmale()" class="card-refresh" data-toggle="refresh"><i class="card-icon card-icon-refresh"></i></a>
                                        </li>
                                        <li><a href="#" class="card-close" data-toggle="close"><i class="card-icon card-icon-close"></i></a>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                            <div class="card-body" id="totalmale">
                                <h3>
                                    <span class="semi-bold"><?php
                                        $sql = "SELECT * FROM members WHERE sex='Male'";;
                                        $conn->query($sql);
                                        $male= $conn->rowCount();
                                        $num = $male;
                                        $totalMale = number_format($num)."<br>";
                                        ?></span>

                                    <?php echo $totalMale; ?></h3>

                            </div>
                        </div>
                    </div>
                    <div class="col-lg-4">
                        <div id="card-linear-color" class="card card-default">
                            <div class="card-header  ">
                                <div class="card-title">Total Females
                                </div>
                                <div class="card-controls">
                                    <ul>
                                        <li><a href="#" onclick="totalfemale()" class="card-refresh" data-toggle="refresh"><i class="card-icon card-icon-refresh"></i></a>
                                        </li>
                                        <li><a href="#" class="card-close" data-toggle="close"><i class="card-icon card-icon-close"></i></a>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                            <div class="card-body" id="totalfemale">
                                <h3>
                                    <span class="semi-bold"><?php
                                        $sql = "SELECT * FROM members WHERE sex='Female'";;
                                        $conn->query($sql);
                                        $female= $conn->rowCount();
                                        $num = $female;
                                        $totalFemale = number_format($num)."<br>";
                                        ?></span>

                                    <?php echo $totalFemale; ?></h3>

                            </div>
                        </div>
                    </div>

                    

                </div>
                <div class="row">
                    <section style="padding-bottom: 50px" class="col-lg-12 connectedSortable">
                        <a href="add-member"><button class="btn btn-outline-success pull-left btn-lg btn-animated from-left">
                                <span>Add Member</span>
                                <span class="hidden-block">
													<i class="pg-icon">user_add</i>
												</span></button> </a>
                    </section>

                    <section class="col-lg-12 connectedSortable">
                        <?php
                        $sql = "SELECT * FROM members";
                        $conn->query($sql);
                        $rowCount =$conn->rowCount();
                        $result = $conn->fetchMultiple();

 
                     
                        
                        ?>


                        <div class="card">
                            <div class="card-header">
                                <h3 class="card-title">Manage Members</h3>
                            </div>
                            <div class="card-body">
                                <table id="member" class="table table-bordered table-striped">
                                    <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>Serial Number</th>
                                        <th>First Name, Other Names</th>
                                        <th>Surname</th>
                                        <th>Amount</th>
                                        <th>Sex</th>
                                        <th>District</th>
                                        <th>Church</th>
                                        <th>Year Attended</th>
                                        <th>Phone</th>
                                        <th>Action</th>
                                    </tr>
                                    </thead>

                                    <tbody>
                                    <?php
                                    $i = 1;
                                    if ($rowCount > 0){
                                        foreach ($result as $member):
                                            $id = $member->id;
                                            $serial = $member->serial;
                                            $firstname = $member->firstname;
                                            $surname = $member->surname;
                                            $sex = $member->sex;
                                            $district = $member->districtID;
                                            $occupation = $member->occupation;
                                            $phone = $member->phone;
                                            $church = $member->church;
                                            $conyear = $member->conyear;
                                            $amount = $member->amount;
                                            $amounts = preg_replace("/\B(?=(\d{3})+(?!\d))/", ",", $amount);


                                            
                                            //fetch name of district
                                            $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 $serial; ?></td>
                                                <td><?php echo $firstname; ?></td>
                                                <td><?php echo $surname; ?></td>
                                                <td><?php echo $amounts; ?></td>
                                                <td><?php echo $sex; ?></td>
                                                <td><?php echo $districtName; ?></td>
                                                <td><?php echo $church; ?></td>
                                                <td><?php echo $conyear; ?></td>
                                                <td><?php echo $phone; ?></td>
                                                <td>
                                                 <div class="dropdown dropdown-default">
                                                <button aria-label="" class="btn dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                                    Action
                                                </button>
                                                <div class="dropdown-menu" style="width: auto; will-change: transform;">
                                                    <a class="dropdown-item" href="view-member/<?php echo $id; ?>" target="_blank">View</a>
                                                    <a class="dropdown-item" href="edit-member/<?php echo $id; ?>" target="_blank">Edit </a>
                                                    <a class="dropdown-item" href="javascript:void(0); " data-toggle="modal" data-target="#delete-member<?php echo $member->id; ?>">Delete</a>
                                                </div>
                                                </div>
                                                </td>
                                            </tr>
                                        <?php endforeach; ?>
                                        <?php
                                    }else{
                                        ?>
                                        <tr>
                                        <td colspan="11" 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>Serial Number</th>
                                        <th>First Name, Other Names</th>
                                        <th>Surname</th>
                                        <th>Amount</th>
                                        <th>Sex</th>
                                        <th>District</th>
                                        <th>Church</th>
                                        <th>Year Attended</th>
                                        <th>Phone</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 -->

<!-- END QUICKVIEW-->

<?php include('includes/footer.php'); ?>


<?php
foreach ($result as $member){
    $id = $member->id;
    ?>

    <!--        Delete Teacher Modal Start-->
    <div class="modal fade slide-up disable-scroll" id="delete-member<?php echo $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 Member 😞</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 $member->firstname; ?> <?php echo $member->surname; ?></span></p>
                    <form action="" method="post">
                        <div class="form-group">
                            <input type="hidden" name="id" value="<?php echo $id; ?>">
                        </div>

                        <div class="col-xl-12 m-t-50">
                            <button aria-label="" type="submit" name="delete_member" 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 Teacher Modal End-->


    <?php
}

//Delete member Script

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

    try{
        $conn->execute();
        echo "<script>
                            toastr.options.progressBar = true, toastr.options.preventDuplicates = true,
                            toastr['success']('Member Data Deleted Successfully');
                            </script><meta http-equiv='refresh' content='5; member'>";
    }catch (PDOException $err){
        echo $err->getMessage();
    }
}
?>





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

<script>
    $(function (){
        $("#member").DataTable({
            "responsive": true, "lengthChange": false, "autowidth": true, "pageLength": 100, "deferRender": true,
            "buttons": ["copy", "csv", "pdf", "print"]
        }).buttons().container().appendTo('#member_wrapper .col-md-6:eq(0)');
    });


</script>
<script>
    function totalmembers() {
        const xhttp = new XMLHttpRequest();
        xhttp.onload = function() {
            document.getElementById("total").innerHTML = this.responseText;
        }
        xhttp.open("GET", "totalmembers.php", true);
        xhttp.send();
    }

</script>

<script>
    function totalmale() {
        const xhttp = new XMLHttpRequest();
        xhttp.onload = function() {
            document.getElementById("totalmale").innerHTML = this.responseText;
        }
        xhttp.open("GET", "totalmale.php", true);
        xhttp.send();
    }

</script>

<script>
    function totalfemale() {
        const xhttp = new XMLHttpRequest();
        xhttp.onload = function() {
            document.getElementById("totalfemale").innerHTML = this.responseText;
        }
        xhttp.open("GET", "totalfemale.php", true);
        xhttp.send();
    }

</script>




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

    <?php

}

?>