<?php

   session_start();

    include_once('../../config/conn/db-connection.php');
    include_once('../../config/class/sap_model.php');


   if(!isset($_SESSION['logged_in']))
    {
       header("location:../../index.php");
    }else{

    $user_session = trim($_SESSION['user_no']);
    $SAP = new SAP_infosytem();
    $tblbeneficiaries = $SAP->fetch_beneficiaries();  

    /////////////////////////////////////////////////

   if(isset($_POST['save'])){
        error_reporting(0);
        $checkbox = $_POST['check'];
          for($i=0;$i<count($checkbox);$i++){
          $del_id = $checkbox[$i]; 

          $sql = "DELETE FROM `tbl_beneficiaries` WHERE beneficiaries_id = ?";
          $db->prepare($sql)->execute([$del_id]);
          $message = "<div class='alert alert-success'>Data deleted successfully !</div>";
          header('Refresh: 1;url='.$_SERVER['REQUEST_URI']);
        }
      }

     $stmt = $db->query("SELECT * FROM tbl_beneficiaries");

?>


<?php include '../header/main-header2.php';?>

<div class="content-wrapper">
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1>Beneficiaries</h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item"><a href="../home-page.php">Home</a></li>
                        <li class="breadcrumb-item active">Beneficiaries</li>
                    </ol>
                </div>
            </div>
        </div>
    </section>
    <section class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-12">

                    <div class="card">
                        <form method="post" action="">
                            <div class="card-header">
                                <button type="button" class="btn btn-success" data-toggle="modal" data-target="#add-beneficiaries<?= htmlentities($user_session); ?>">Add Beneficiaries</button>&nbsp;<button type="submit" class="btn btn-danger" name="save">Delete</button>
                            </div>
                            <div><?php if(isset($message)) { echo $message; } ?></div>
                            <div class="card-body">
                              <div class="table-responsive">
                                <table id="beneficiaries-table" class="table table-bordered table-striped">
                                    <thead>
                                        <tr>
                                            <th><input type="checkbox" id="checkAl">&nbsp;Select All</th>
                                            <th>Purok #</th>
                                            <th>Image</th>
                                            <th>Age</th>
                                            <th>Gender</th>
                                            <th>Former Address</th>
                                            <th>Action</th>

                                        </tr>
                                    </thead>
                                    <tbody>
                                     <?php
                                      include_once('../../config/conn/db-connection.php');
                                        $hib = intval($_GET['beneficiariesfemale-group']);
                                        $sql = "SELECT * FROM  tbl_beneficiaries  WHERE gender = 'Female'  ORDER BY ?";
                                        $stmt = $db->prepare($sql);
                                        $stmt->execute([$hib]);
                                        $getben = $stmt->fetchAll(PDO::FETCH_ASSOC);
                                       ?>
                                      <?php foreach ($getben as $beneficiaries): ?>
                                        <tr>
                                            <td><input type="checkbox" id="checkItem" name="check[]" value="<?= htmlentities($beneficiaries['beneficiaries_id']); ?>"></td>

                                            <td><?= ucfirst(htmlentities($beneficiaries['purok_no'])); ?></td>
                                            <td><img src="../../<?= ucfirst(htmlentities($beneficiaries['image_profile'])); ?>" alt="Profile" class="brand-image img-square elevation-3" width="60" height="60" style="opacity: .8;border: 1px solid gray">&nbsp;<i class="fa fa-edit edit-image" style="color: green" title="Edit Image" data-toggle="modal"  data-target="#edit-image" data-image="<?= htmlentities($beneficiaries['beneficiaries_id']); ?>"></i></td>
                                            <td><?= ucfirst(htmlentities($beneficiaries['age'])); ?></td>
                                            <td><?= ucfirst(htmlentities($beneficiaries['gender'])); ?></td>
                                            <td><?= ucfirst(htmlentities($beneficiaries['former_address'])); ?></td>
                                            <td>
                                            <div class="d-flex">
                                            <div class="flex-1">
                                            <button type="button" class="btn btn-info edit_beneficiaries" data-toggle="modal"  data-target="#edit-beneficiaries<?= htmlentities($user_session); ?>" data-id="<?= htmlentities($beneficiaries['beneficiaries_id']); ?>"><i class="fa fa-edit"></i> Edit 
                                            </button>
                                          </div>&nbsp;
                                          <div class="flex-1">
                                            <button type="button"  class="btn btn-info details_beneficiaries" data-toggle="modal" data-target="#details-beneficiaries" data-details="<?= htmlentities($beneficiaries['beneficiaries_id']); ?>"><i class="fa fa-info"></i> Details 
                                            </button>
                                          </div>
                                          </div>
                                        </td>
                                        </tr>
                                        <?php endforeach ?>  

                                    </tbody>
                              </table>
                        </form>
                      </div>
                    </div>
                </div>
            </div>
        </div>
   </div>
</section>
</div>

<?php } ?>
<?php include 'modal/beneficiaries_modal.php';?>
<?php include 'modal/beneficiariesedit_modal.php';?>
<?php include 'modal/beneficiariesdetails_modal.php';?>
<?php include 'modal/beneficiariesimage_modal.php';?>

      <script>
           $(document).ready(function() {   
               load_data();    
               var count = 1; 
               function load_data() {
                   $(document).on('click', '.edit_beneficiaries', function() {
                        var beneficiaries_id = $(this).data("id");
                          getID(beneficiaries_id); //argument    
                 
                   });
                }

                 function getID(beneficiaries_id) {
                      $.ajax({
                          type: 'POST',
                          url: 'fetch_row/beneficiaries_row.php',
                          data: {
                              beneficiaries_id: beneficiaries_id
                          },
                          dataType: 'json',
                          success: function(response) {
                          $('#edit_beneficiariesid').val(response.beneficiaries_id);
                          $('#edit_lastname').val(response.lastname);
                          $('#edit_firstname').val(response.firstname);
                          $('#edit_middlename').val(response.middlename);
                          $('#edit_age').val(response.age);
                          $('#edit_birthdate').val(response.birthdate);
                          $('#edit_gender').val(response.gender);
                          $('#edit_householdnumber').val(response.household_number);
                          $('#edit_birthplace').val(response.birthplace);
                          $('#edit_differentlyabledperson').val(response.differently_abledperson);
                          $('#edit_gcashnumber').val(response.gcash_number);
                          $('#edit_bloodtype').val(response.blood_type);
                          $('#edit_purokno').val(response.purok_no);
                          $('#edit_civilstatus').val(response.civil_status);
                          $('#edit_totalhouseholdmember').val(response.total_householdmember);
                          $('#edit_religion').val(response.religion);
                          $('#edit_occupation').val(response.occupation);
                          $('#edit_contactnumber').val(response.contact_number);
                          $('#edit_monthlyincome').val(response.monthly_income);
                          $('#edit_educationalattainment').val(response.educational_attainment);
                          $('#edit_nationality').val(response.nationality);
                          $('#edit_philhealthnumber').val(response.philhealth_number);
                          $('#edit_skills').val(response.skills);
                          $('#edit_waterusage').val(response.water_usage);
                          $('#edit_landownershipstatus').val(response.land_ownershipstatus);
                          $('#edit_sektor').val(response.sektor);
                          $('#edit_benificiariesof').val(response.benificiaries_of);
                          $('#edit_lightfacilities').val(response.light_facilities);
                          $('#edit_remark').val(response.remark);
                          $('#edit_formeraddress').val(response.former_address);
                          $('#edit_username').val(response.username);
                          $('#edit_password').val(response.password);


                       }
                    });
                 }
           
           });
            
     </script>
      <script>
           $(document).ready(function() {   
               load_data();    
               var count = 1; 
               function load_data() {
                   $(document).on('click', '.details_beneficiaries', function() {
                        var beneficiaries_id = $(this).data("details");
                          get_details(beneficiaries_id); //argument    
                 
                   });
                }

                 function get_details(beneficiaries_id) {
                      $.ajax({
                          type: 'POST',
                          url: 'fetch_row/beneficiaries_row.php',
                          data: {
                              beneficiaries_id: beneficiaries_id
                          },
                          dataType: 'json',
                          success: function(response2) {
                          $('#detatil_beneficiariesid').val(response2.beneficiaries_id);
                          $('#detatil_lastname').val(response2.lastname);
                          $('#detatil_firstname').val(response2.firstname);
                          $('#detatil_middlename').val(response2.middlename);
                          $('#detatil_age').val(response2.age);
                          $('#detatil_birthdate').val(response2.birthdate);
                          $('#detatil_gender').val(response2.gender);
                          $('#detatil_householdnumber').val(response2.household_number);
                          $('#detatil_birthplace').val(response2.birthplace);
                          $('#detatil_differentlyabledperson').val(response2.differently_abledperson);
                          $('#detatil_gcashnumber').val(response2.gcash_number);
                          $('#detatil_bloodtype').val(response2.blood_type);
                          $('#detatil_purokno').val(response2.purok_no);
                          $('#detatil_civilstatus').val(response2.civil_status);
                          $('#detatil_totalhouseholdmember').val(response2.total_householdmember);
                          $('#detatil_religion').val(response2.religion);
                          $('#detatil_occupation').val(response2.occupation);
                          $('#detatil_contactnumber').val(response2.contact_number);
                          $('#detatil_monthlyincome').val(response2.monthly_income);
                          $('#detatil_educationalattainment').val(response2.educational_attainment);
                          $('#detatil_nationality').val(response2.nationality);
                          $('#detatil_philhealthnumber').val(response2.philhealth_number);
                          $('#detatil_skills').val(response2.skills);
                          $('#detatil_waterusage').val(response2.water_usage);
                          $('#detatil_landownershipstatus').val(response2.land_ownershipstatus);
                          $('#detatil_sektor').val(response2.sektor);
                          $('#detatil_benificiariesof').val(response2.benificiaries_of);
                          $('#detatil_lightfacilities').val(response2.light_facilities);
                          $('#detatil_remark').val(response2.remark);
                          $('#detatil_formeraddress').val(response2.former_address);
                          $('#detatil_username').val(response2.username);
                          $('#detatil_password').val(response2.password);
                          $('#detatil_imageprofile').attr("src", '../../'+ response2.image_profile.slice(0));//image

                       }
                    });
                 }
           
           });
            
     </script>


           <script>
           $(document).ready(function() {   
               load_data();    
               var count = 1; 
               function load_data() {
                   $(document).on('click', '.edit-image', function() {
                        var beneficiaries_id = $(this).data("image");
                          get_image(beneficiaries_id); //argument    
                 
                   });
                }

                 function get_image(beneficiaries_id) {
                      $.ajax({
                          type: 'POST',
                          url: 'fetch_row/beneficiaries_row.php',
                          data: {
                              beneficiaries_id: beneficiaries_id
                          },
                          dataType: 'json',
                          success: function(response3) {
                          $('#img_beneficiariesid').val(response3.beneficiaries_id);
                          $('#img_imageprofile').attr("src", '../../'+ response3.image_profile.slice(0));//image

                       }
                    });
                 }
           
           });
            
     </script>

<footer class="main-footer">
    <div class="float-right d-none d-sm-block">
        <b>Version</b> 1.0.0
    </div>
    <strong>Copyright &copy; 2021 <b>SAP Information System </b>.</strong>
</footer>

<aside class="control-sidebar control-sidebar-dark">
</aside>
</div>

<script>
    $("#checkAl").click(function() {
        $('input:checkbox').not(this).prop('checked', this.checked);
    });
</script>

<script src="../../plugins/jquery/jquery.min.js"></script>
<script src="../../plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="../../plugins/datatables/jquery.dataTables.min.js"></script>
<script src="../../plugins/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>
<script src="../../plugins/datatables-responsive/js/dataTables.responsive.min.js"></script>
<script src="../../plugins/datatables-responsive/js/responsive.bootstrap4.min.js"></script>
<script src="../../dist/js/adminlte.min.js"></script>
<script src="../../dist/js/demo.js"></script>
<script>
    $(function() {
        $("#beneficiaries-table").DataTable({
            // "responsive": true,
            // "autoWidth": false,
        });
    });
</script>
</body>

</html>