   <?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();
    // $tblhousehold = $SAP->fetch_household();  

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

   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_household` WHERE household_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_household");

?>



   <?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>Household</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">Household</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-household<?= htmlentities($user_session); ?>">Add Household</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">
                   

                               <table id="household-table" class="table table-bordered table-striped">
                                   <thead>
                                       <tr>
                                           <th><input type="checkbox" id="checkAl">&nbsp;Select All</th>
                                           <th>Household #</th>
                                           <th>Purok #</th>
                                           <th>Total Members of the Family</th>
                                           <th>Action</th>

                                       </tr>
                                   </thead>
                                  <tbody>
                                     <?php
                                      include_once('../../config/conn/db-connection.php');
                                        $hid = intval($_GET['purokno-group']);
                                        $sql = "SELECT * FROM tbl_household WHERE purok_no = ?";
                                        $stmt = $db->prepare($sql);
                                        $stmt->execute([$hid]);
                                        $geth = $stmt->fetchAll(PDO::FETCH_ASSOC);
                                       ?>
                                      <?php foreach ($geth as $household): ?>
                                       <tr>
                                        <td><input type="checkbox" id="checkItem" name="check[]" value="<?= htmlentities($household['household_id']); ?>"></td>
                                    
                                           <td><?= ucfirst(htmlentities($household['household_no'])); ?></td>
                                           <td><?= ucfirst(htmlentities($household['purok_no'])); ?></td>
                                           <td><?= ucfirst(htmlentities($household['total_membersofthefamily'])); ?></td>
                                           <td>
                                            <div class="d-flex">
                                            <div class="flex-1">
                                            <button type="button" class="btn btn-info edit_household" data-toggle="modal"  data-target="#edit-household<?= htmlentities($user_session); ?>" data-id="<?= htmlentities($household['household_id']); ?>"><i class="fa fa-edit"></i> Edit 
                                            </button>
                                          </div>&nbsp;
                                          <div class="flex-1">
                                            <button type="button"  class="btn btn-info details_household" data-toggle="modal" data-target="#details-household" data-details="<?= htmlentities($household['household_id']); ?>"><i class="fa fa-info"></i> Details 
                                            </button>
                                          </div>
                                          </div>
                                        </td>
                                       </tr>
                                    <?php endforeach ?>  
                                  </tbody>
                               </table>
                             </form>
                           </div>
                       </div>
                   </div>
               </div>
           </div>
       </section>
   </div>
   <?php } ?>

   <?php include 'modal/household_modal.php';?>
   <?php include 'modal/householdedit_modal.php';?>
   <?php include 'modal/householddetails_modal.php';?>

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

                 function getID(household_id) {
                      $.ajax({
                          type: 'POST',
                          url: 'fetch_row/household_row.php',
                          data: {
                              household_id: household_id
                          },
                          dataType: 'json',
                          success: function(response) {
                          $('#edit_householdid').val(response.household_id);
                          $('#edit_householdno').val(response.household_no);
                          $('#edit_purokno').val(response.purok_no);
                          $('#edit_totalmembersofthefamily').val(response.total_membersofthefamily);
                       }
                    });
                 }
           
           });
            
     </script>
     <script>
           $(document).ready(function() {   
               load_data();    
               var count = 1; 
               function load_data() {
                   $(document).on('click', '.details_household', function() {
                        var household_id = $(this).data("details");
                          getDetail(household_id); //argument    
                 
                   });
                }

                 function getDetail(household_id) {
                      $.ajax({
                          type: 'POST',
                          url: 'fetch_row/household_row.php',
                          data: {
                              household_id: household_id
                          },
                          dataType: 'json',
                          success: function(response2) {
                          $('#detail_householdid').val(response2.household_id);
                          $('#detail_householdno').val(response2.household_no);
                          $('#detail_purokno').val(response2.purok_no);
                          $('#detail_totalmembersofthefamily').val(response2.total_membersofthefamily);
                       }
                    });
                 }
           
           });
            
     </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() {
           $("#household-table").DataTable({
               // "responsive": true,
               // "autoWidth": false,
           });
       });
   </script>
   </body>

   </html>