   <?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();
    $tblsapform = $SAP->fetch_sapform(); 
    $tblsapform1 = $SAP->fetch_sapform1();   

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

   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_sapform` WHERE sapform_id = ?";
          $db->prepare($sql)->execute([$del_id]);


         // ==========audit trail==================
              $user = $user_session;
               if(!empty($_SERVER["HTTP_CLIENT_IP"])){
                     $ip = $_SERVER["HTTP_CLIENT_IP"];
                  }elseif(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){ 
                     $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
                  }else{
                     $ip = $_SERVER["REMOTE_ADDR"];
                }
               $host = gethostbyaddr($_SERVER['REMOTE_ADDR']);
               date_default_timezone_set("asia/manila");
               $logdate =  date('M-d-Y h:i A', strtotime("+0 HOURS"));
               $action = "'Admin' Deleted SAP Form ID: '".$del_id."'";    
            // ==========end audit trail==============

            $stmt = $db->prepare("INSERT INTO  tbl_audittrail(`user_name`, `user_ip`, `user_host`, `logdate`, `action`) VALUES(?,?,?,?,?)");
            $true = $stmt->execute([$user, $ip, $host,  $logdate, $action]);



          $message = "<div class='alert alert-success'>Data deleted successfully !</div>";
          header('Refresh: 1;url='.$_SERVER['REQUEST_URI']);
        }
      }

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


  if(isset($_POST['save2'])){
        error_reporting(0);
        $checkbox2 = $_POST['check2'];
          for($i=0;$i<count($checkbox2);$i++){
          $del_id2 = $checkbox2[$i]; 

          $sql = "DELETE FROM `tbl_sapform` WHERE sapform_id = ?";
          $db->prepare($sql)->execute([$del_id2]);


         // ==========audit trail==================
              $user = $user_session;
               if(!empty($_SERVER["HTTP_CLIENT_IP"])){
                     $ip = $_SERVER["HTTP_CLIENT_IP"];
                  }elseif(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){ 
                     $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
                  }else{
                     $ip = $_SERVER["REMOTE_ADDR"];
                }
               $host = gethostbyaddr($_SERVER['REMOTE_ADDR']);
               date_default_timezone_set("asia/manila");
               $logdate =  date('M-d-Y h:i A', strtotime("+0 HOURS"));
               $action = "'Admin' Deleted SAP Form ID: '".$del_id2."'";    
            // ==========end audit trail==============

            $stmt = $db->prepare("INSERT INTO  tbl_audittrail(`user_name`, `user_ip`, `user_host`, `logdate`, `action`) VALUES(?,?,?,?,?)");
            $true = $stmt->execute([$user, $ip, $host,  $logdate, $action]);



          $message = "<div class='alert alert-success'>Data deleted successfully !</div>";
          header('Refresh: 1;url='.$_SERVER['REQUEST_URI']);
        }
      }




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

?>



   <?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>Social Amelioration Program Form</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">SAP Form</li>
                       </ol>
                   </div>
               </div>
           </div>
       </section>

       <section class="content">
           <div class="container-fluid">
               <div class="row">
                   <div class="col-12">
            <div class="card card-primary card-outline card-tabs">
              <div class="card-header p-0 pt-1 border-bottom-0">
                <ul class="nav nav-tabs" id="custom-tabs-three-tab" role="tablist">
                  <li class="nav-item">
                    <a class="nav-link active" id="custom-tabs-three-approved-tab" data-toggle="pill" href="#custom-tabs-three-approved" role="tab" aria-controls="custom-tabs-three-approved" aria-selected="true">Approved</a>
                  </li>
                  <li class="nav-item">
                    <a class="nav-link" id="custom-tabs-three-disapproved-tab" data-toggle="pill" href="#custom-tabs-three-disapproved" role="tab" aria-controls="custom-tabs-three-disapproved" aria-selected="false">Disapproved</a>
                  </li>
    
                </ul>
              </div>
              <div class="card-body">
                <div class="tab-content" id="custom-tabs-three-tabContent">
                  <div class="tab-pane fade show active" id="custom-tabs-three-approved" role="tabpanel" aria-labelledby="custom-tabs-three-approved-tab">
              
               <!-- approved -->


                       <div class="card">
                           <form method="post" action="">
                               <div class="card-header">
                                   <button type="button" class="btn btn-success" data-toggle="modal" data-target="#add-SAPForm<?= htmlentities($user_session); ?>">Add SAP Form</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="sapform-table1" class="table table-bordered table-striped">
                                       <thead>
                                           <tr>
                                               <th><input type="checkbox" id="checkAl">&nbsp;Select All</th>
                                               <th>SAPForm_ #</th>
                                               <th>Beneficiaries Name</th>
                                               <th>Purpose</th>
                                               <th>Financial Service Provider</th>
                                               <th>Action</th>

                                           </tr>
                                       </thead>
                                       <tbody>
                                           <?php
                                      foreach($tblsapform as $sapform){ ?>
                                           <tr>
                                               <td><input type="checkbox" id="checkItem" name="check[]" value="<?= htmlentities($sapform['sapform_id']); ?>"></td>

                                               <td><?= ucfirst(htmlentities($sapform['sapform_no'])); ?></td>
                                               <td><?= ucfirst(htmlentities($sapform['beneficiary_name'])); ?></td>
                                               <td><?= ucfirst(htmlentities($sapform['purpose'])); ?></td>
                                                <td><?= ucfirst(htmlentities($sapform['financial_serviceprovider'])); ?></td>
                                               <td>
                                                   <div class="d-flex">
                                                       <div class="flex-1">
                                                           <button type="button" class="btn btn-info edit_sapform" data-toggle="modal" data-target="#edit-SAPForm<?= htmlentities($user_session); ?>" data-id="<?= htmlentities($sapform['sapform_id']); ?>"><i class="fa fa-edit"></i> Edit
                                                           </button>
                                                       </div>&nbsp;
                                                       <div class="flex-1">
                                                           <button type="button" class="btn btn-info print" data-getid="<?= htmlentities($sapform['sapform_id']); ?>"><i class="fa fa-print"></i> Generate
                                                           </button>
                                                       </div>
                                                   </div>
                                               </td>
                                           </tr>
                                           <?php $found_rows = true; }  if (!isset($found_rows)) echo "<tr><td colspan ='6'><div class='alert alert-dark' style='font-weight:bold;'>No records found.</div></td></tr>"; 
                                       ?>

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


               <!-- end approved -->
                  </div>
                  <div class="tab-pane fade" id="custom-tabs-three-disapproved" role="tabpanel" aria-labelledby="custom-tabs-three-disapproved-tab">
              
               <!-- disapproved -->


                       <div class="card">
                           <form method="post" action="">
                               <div class="card-header">
                               <button type="submit" class="btn btn-danger" name="save2">Delete</button>
                               </div>
                               <div><?php if(isset($message)) { echo $message; } ?></div>
                               <div class="card-body">
                                <div class="table-responsive">
                                   <table id="sapform-table2" class="table table-bordered table-striped">
                                       <thead>
                                           <tr>
                                               <th><input type="checkbox" id="checkAl">&nbsp;Select All</th>
                                               <th>Beneficiaries Name</th>
                                               <th>Purpose</th>
                                          <!--      <th>Action</th> -->
                        

                                           </tr>
                                       </thead>
                                       <tbody>
                                       <?php
                                        foreach($tblsapform1 as $sapform1){ ?>
                                           <tr>
                                               <td><input type="checkbox" id="checkItem" name="check2[]" value="<?= htmlentities($sapform1['sapform_id']); ?>"></td>

                                               <td><?= ucfirst(htmlentities($sapform1['beneficiary_name'])); ?></td>
                                               <td><?= ucfirst(htmlentities($sapform1['purpose'])); ?></td>
<!--                                                 <td><?= ucfirst(htmlentities($sapform['financial_serviceprovider'])); ?></td> -->
<!--                                            <td>
                                                   <div class="d-flex">
                                                       <div class="flex-1">
                                                           <button type="button" class="btn btn-info del_sapform" data-toggle="modal" data-target="#del-SAPForm<?= htmlentities($user_session); ?>" data-del="<?= htmlentities($sapform['sapform_id']); ?>"><i class="fa fa-trash"></i> Delete
                                                           </button>
                                                       </div>
                                                   </div>
                                               </td>
 -->
                                           </tr>
                                           <?php $found_rows = true; }  if (!isset($found_rows)) echo "<tr><td colspan ='5'><div class='alert alert-danger' style='font-weight:bold;'>No records found.</div></td></tr>"; 
                                       ?>

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


               <!-- end disapproved -->

                </div>
                </div>
              </div>
              <!-- /.card -->
            </div>         
          </div>
        </div>
       </section>
   </div>

   <?php } ?>
   <?php include 'modal/sapform_modal.php';?>
   <?php include 'modal/sapformedit_modal.php';?>
   <?php include 'modal/sapformedelete_modal.php';?>


   <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>

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

                 function getID(sapform_id) {
                      $.ajax({
                          type: 'POST',
                          url: 'fetch_row/sapform_row.php',
                          data: {
                              sapform_id: sapform_id
                          },
                          dataType: 'json',
                          success: function(response) {
                          $('#edit_sapformid').val(response.sapform_id);
                          $('#edit_sapformno').val(response.sapform_no);
                          $('#edit_beneficiariesid').val(response.beneficiaries_id);
                          $('#edit_purpose').val(response.purpose);
                          $('#edit_financialserviceprovider').val(response.financial_serviceprovider);
                       }
                    });
                 }
           
           });
            
     </script>
         <script>
           $(document).ready(function() {   
               load_data();    
               var count = 1; 
               function load_data() {
                   $(document).on('click', '.del_sapform', function() {
                        var sapform_id = $(this).data("del");
                          getDELID(sapform_id); //argument    
                 
                   });
                }

                 function getDELID(sapform_id) {
                      $.ajax({
                          type: 'POST',
                          url: 'fetch_row/sapform_row.php',
                          data: {
                              sapform_id: sapform_id
                          },
                          dataType: 'json',
                          success: function(response2) {
                          $('#delete_sapformid').val(response2.sapform_id);
                          $('#delete_fullname').val(response2.full_name);
                       }
                    });
                 }
           
           });
            
     </script>
<script>
$("#checkAl").click(function () {
  $('input:checkbox').not(this).prop('checked', this.checked);
});
</script>

 <script>
      $(document).ready(function() {   
           load_data();    
           var count = 1; 
           function load_data() {
               $(document).on('click', '.print', function() {
                    let sapform_id = $(this).data("getid");
                      window.open('Print_sapform.php?sapform_id=' + sapform_id, 'Print', 'scrollbars=1,width=100%');
                      return false;                  
             
               });
            }   
       });
          
   </script>


   <aside class="control-sidebar control-sidebar-dark">
   </aside>
   </div>
   <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 src="assets/js/modernizr.min.js"></script>
<script src="assets/js/modernizr.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link href="https://cdn.datatables.net/1.10.18/css/dataTables.bootstrap4.min.css" rel="stylesheet">

<script src="https://cdn.datatables.net/1.10.18/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.18/js/dataTables.bootstrap4.min.js"></script>
<link href="css/modern-business.css" rel="stylesheet">

 <script>
    $(document).ready(function() {
        $('#sapform-table1').DataTable();
    });
</script>
<script>
    $(document).ready(function() {
        $('#sapform-table2').DataTable();
    });
</script>


<script>
  $(".select2").select2();
</script>
   </body>

   </html>