<?php 
include('session.php');
$sql = "SELECT * FROM admin WHERE id = '".$_SESSION['admin']."'";
$query = $conn->query($sql);
$admin = $query->fetch_assoc();
?>
<!DOCTYPE html>
<html lang="en">
  <head>
	<title>ECIMS ADMIN | COMPLAINTS</title>
    <?php include('includes/head.php'); ?>
    <style>
        th,td { text-align:center; }
    </style>
  </head>
  <body>
    <div class="container-scroller">
      <div class="horizontal-menu">
        <?php include('includes/header.php'); ?>
      </div>
      <div class="container-fluid page-body-wrapper">
        <div class="main-panel">
          <div class="content-wrapper">
            <div class="card grid-margin">
              <div class="card-body">
                <div class="row">
                  <div class="col-12">
                    <div class="table-responsive">
                      <table class="table table-striped table-bordered" width="100%">
                        <thead>
                          <tr>
                            <th>Complaint No.</th>
                            <th>Date/Time of Complaint Received</th>
                            <th>Building No.</th>
                            <th>Complaint Details</th>
                            <th>Assign to Officer</th>
                          </tr>
                        </thead>
                        <tbody>
                            
                    <?php 
                            $tcomplaints1 = "SELECT * FROM users_complaint";
                            $users_complaint = $conn->query($tcomplaints1);$bgclass='';
                            if($users_complaint->num_rows > 0) {
                            while($fetchusers_complaint = $users_complaint->fetch_assoc()) {
                                $building_list = $conn->query("SELECT * FROM building_list WHERE bid = '".$fetchusers_complaint['b_list']."'")->fetch_assoc();
                                $complaint_list = $conn->query("SELECT * FROM complaint_list WHERE cid = '".$fetchusers_complaint['c_list']."'")->fetch_assoc();
                                $subdivision_list = $conn->query("SELECT * FROM subdivision WHERE id = '".$fetchusers_complaint['subdivision']."'")->fetch_assoc();
                                $users = $conn->query("SELECT * FROM users WHERE uid = '".$fetchusers_complaint['uid']."'")->fetch_assoc();
                                $deptname=$engname='';
                                if($fetchusers_complaint['department'] != '0' || $fetchusers_complaint['department'] != 0 || !empty($fetchusers_complaint['department'])) {
                                    $departmentr1236 = $conn->query("SELECT * FROM admin WHERE id = '".$fetchusers_complaint['department']."'")->num_rows;
                                    if($departmentr1236 > 0) {
                                    $department = $conn->query("SELECT * FROM admin WHERE id = '".$fetchusers_complaint['department']."'")->fetch_assoc();
                                    $deptname = $department['name'].'('.$department['designation'].')';
                                    }
                                }
                                if($fetchusers_complaint['engineer'] != '0' || $fetchusers_complaint['engineer'] != 0 || !empty($fetchusers_complaint['engineer'])) {
                                    $engineer1236 = $conn->query("SELECT * FROM engineers_list WHERE id = '".$fetchusers_complaint['engineer']."'")->num_rows;
                                    if($engineer1236 > 0) {
                                    $engineer = $conn->query("SELECT * FROM engineers_list WHERE id = '".$fetchusers_complaint['engineer']."'")->fetch_assoc();
                                    $engname = $engineer['name'];
                                    }
                                }
                                // if($fetchusers_complaint['c_status'] == '0') {
                                //     $bgclass = 'bg-warning';
                                // } else if($fetchusers_complaint['c_status'] == '1') {
                                //     $bgclass = 'bg-warning';
                                // } else if($fetchusers_complaint['c_status'] == '2') {
                                //     $bgclass = 'bg-success';
                                // } else if($fetchusers_complaint['c_status'] == '3') {
                                //     $bgclass = 'bg-danger';
                                // } else if($fetchusers_complaint['c_status'] == '4') {
                                //     $bgclass = 'bg-info';
                                // } else {
                                //     $bgclass = 'bg-primary';
                                // }
                            ?>
                              <tr class="<?php echo $fetchusers_complaint['ucid']; ?>">
                                <td class="<?php echo $bgclass; ?> text-dark"><?php echo $fetchusers_complaint['docketNumber']; ?></td>
                                <td><?php $date =  $fetchusers_complaint['date'];
                                    $timestamp = strtotime($date);
                                    if ($timestamp === FALSE) {
                                      $timestamp = strtotime(str_replace('/', '-', $date));
                                    }
                                    echo $date; ?>
                                    <br>(<?php echo date ( 'H:i:s' , strtotime($fetchusers_complaint['time']) ); ?>)</td>
                                <td><?php echo $building_list['bNo']; ?></td>
                                <td><?php echo $complaint_list['details']; ?></td>
                                <td><?php if(!empty($deptname)) { echo $deptname; } else {echo '';} ?></td>
                              </tr>
                          <?php }} else {
                          echo '<tr><td colspan="5"><center>No Records Found!</center></td></tr>';
                          } ?>
                        </tbody>
                      </table>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
		  <?php include('includes/footer.php'); ?>
        </div>
      </div>
    </div>
    </div>
    <?php include('includes/foot.php'); ?>
  </body>
</html>