   <?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();
    $contacts = $SAP->fetch_contact();  

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

   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_contact` WHERE contact_id = ?";
          $db->prepare($sql)->execute([$del_id]);
          $message = "<div class='alert alert-success'>Data deleted successfully !</div>";


         // ==========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 Complaint 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]);



          header('Refresh: 1;url='.$_SERVER['REQUEST_URI']);
        }
      }

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

?>


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

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1>Complaints</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">Complaints</li>
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

    <!-- Main content -->

       <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="submit" class="btn btn-danger" name="save">Delete</button>
                           </div>
                          <div><?php if(isset($message)) { echo $message; } ?></div>
                           <div class="card-body">
                   

                               <table id="contact-table" class="table table-bordered table-striped">
                                   <thead>
                                       <tr>
                                           <th><input type="checkbox" id="checkAl">&nbsp;Select All</th>
                                           <th>Name</th>
                                           <th>Email</th>
                                           <th>Contact Number</th>
                                           <th>Message</th>

                                       </tr>
                                   </thead>
                                  <tbody>
                                 <?php
                                      foreach($contacts  as $con){ ?>
                                       <tr>
                                        <td><input type="checkbox" id="checkItem" name="check[]" value="<?= htmlentities($con['contact_id']); ?>"></td>
                                    
                                           <td><?= ucwords(htmlentities($con['full_name'])); ?></td>
                                           <td><?= htmlentities($con['email']); ?></td>
                                           <td><?= htmlentities($con['contact_number']); ?></td>
                                           <td><?= htmlentities($con['message']); ?></td>

                                       </tr>
                                       <?php $found_rows = true; }  if (!isset($found_rows)) echo "<tr><td colspan ='5'><div class='alert alert-dark' style='font-weight:bold;'>No records found.</div></td></tr>"; 
                                       ?>
  
                                   </tbody>

                               </table>
                             </form>
                           </div>
                       </div>
                   </div>
               </div>
           </div>
       </section>
  </div>
 <?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>
  <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>
       $(function() {
           $("#contact-table").DataTable({
               // "responsive": true,
               // "autoWidth": false,
           });
       });
   </script>
</body>
</html>
