<?php 
include('usersession.php');
if(isset($_POST['submit'])) { 
 $insert = $conn->query("UPDATE users_complaint SET department='".$_POST['dept_id']."', engineer='0' WHERE ucid='".$_POST['ucid']."'");   
 echo '<script>alert("Department assigned Successfully.");window.location.href="complaints.php"</script>';
}
if(isset($_POST['submiteng'])) {
 $insert = $conn->query("UPDATE users_complaint SET engineer='".$_POST['engineer_id']."', department='".$_POST['did']."' WHERE ucid='".$_POST['ucid']."'");  
 $conn->query("INSERT INTO complaint_assignment (cid, eid, did) VALUES ('".$_POST['ucid']."', '".$_POST['engineer_id']."','".$_POST['did']."')");
 echo '<script>alert("Engineer assigned Successfully.");window.location.href="complaints.php"</script>';
}
?>
<!DOCTYPE html>
<html lang="en">
  <head>
	<title>ECIMS USER | CLOSED 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 $activePage = basename($_SERVER['PHP_SELF'], ".php"); ?>
<nav class="navbar top-navbar col-lg-12 col-12 p-0">
  <div class="container-fluid">
	<div class="text-center navbar-brand-wrapper d-flex align-items-center justify-content-start">
	  <a class="navbar-brand brand-logo" href="index.php"><img style="width: 115px;height:auto;" src="assets/images/logo.png" alt="logo" /></a>
	  <a class="navbar-brand brand-logo-mini" href="index.php"><img style="width: 115px;height:auto;" src="assets/images/logo.png" alt="logo" /></a>
	</div>
	<div class="text-center navbar-menu-wrapper d-none d-lg-block d-flex align-items-center justify-content-center">
	  <span style="text-transform:uppercase;font-size: 30px;font-weight: bold;color: #000;">military engineer services <br> HISAR/ALWAR</span>
	</div>
	<div class="navbar-brand-wrapper d-flex align-items-center justify-content-end">
	  <a class="navbar-brand brand-logo d-none d-lg-block" href="index.php"><img style="width: 115px;height:auto;" src="assets/images/logo-right.png" alt="logo" /></a>
	  <button class="navbar-toggler navbar-toggler-right d-lg-none align-self-center" type="button" data-toggle="horizontal-menu-toggle">
		<i class="fa fa-bars text-white"></i>
	  </button>
	</div>
  </div>
</nav>
<p class="text-center p-0 m-1 fw-bolder">Login Date : <?php echo date('d/m/Y'); ?></p>
<nav class="bottom-navbar">
  <div class="container-fluid">
	<?php include('includes/nav.php'); ?>
  </div>
</nav>
      </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/Mobile No.</th>
                            <th>Complaint Details<br>Subdivision</th>
                            <th>Complaint Description</th>
                            <th>Linked Images</th>
                            <th>Priority</th>
                            <th>Tradesman Engineer</th>
                            <th>Date/Time of Completion</th>
                            <th>Official Remarks</th>
                            <th>User Satisfaction</th>
                            <th>Print</th>
                            <th>Feedback</th>
                          </tr>
                        </thead>
                        <tbody>
                            
                    <?php $count=0;
                        $tcomplaints1 = "SELECT * FROM users_complaint WHERE users_complaint.uid='".$_SESSION['ecimsuser']."' AND c_status='2'"; 
                            $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-info';$bgstatus = 'Pending';
                                } else if($fetchusers_complaint['c_status'] == '1') {
                                    $bgclass = 'bg-warning';$bgstatus = 'Confirmed';
                                } else if($fetchusers_complaint['c_status'] == '2') {
                                    $bgclass = 'bg-success';$bgstatus = 'Complete';
                                } else if($fetchusers_complaint['c_status'] == '3') {
                                    $bgclass = 'bg-danger';$bgstatus = 'Cancelled';
                                } else {
                                    $bgclass = 'bg-primary';$bgstatus = 'Pending';
                                }
                            ?>
                              <tr>
                                <td>
                                    <?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('d-m-y',$timestamp); ?><br>(<?php echo date ( 'H:i:s' , strtotime($fetchusers_complaint['time']) ); ?>)</td>
                                <td><a href="javascript:" data-bs-toggle="modal" data-bs-target="#assignBModal<?php echo $fetchusers_complaint['ucid']; ?>"><?php echo $building_list['bNo'].'<br>'.$users['phone']; ?></a></td>
                                <td><?php echo $complaint_list['details']; ?><br><?php echo $subdivision_list['value']; ?></td>
                                <td><?php echo $fetchusers_complaint['details']; ?></td>
                                <td><a href="javascript:" data-bs-toggle="modal" data-bs-target="#assignIModal<?php echo $fetchusers_complaint['ucid']; ?>">View Images</a></td>
                                <td><?php echo $fetchusers_complaint['priority']; ?></td>
                                <td><?php if(!empty($engname)) { echo $engname; } else {echo 'N/A';} ?></td>
                                <td><?php if($fetchusers_complaint['c_status'] == '2') { echo date('d-m-y H:i:s', strtotime($fetchusers_complaint['completiondate'])); } else {echo 'N/A<br>N/A';} ?></td>
                                <td><?php echo $fetchusers_complaint['remarks']; ?></td>
                                <td><div class="stars">
                                <?php $user_ratings = $fetchusers_complaint['user_ratings'];
                                        $whole = intval($user_ratings);
                                        $rem = 5 - $whole;
                                        $decimal1 = $user_ratings - $whole;
                                        $decimal2 = round($decimal1, 2);
                                        $decimal = substr($decimal2, 2);
                                    for($i=0;$i<$whole;$i++) {
                                        echo '<span class="star on"></span>';
                                    } ;
                                    if($decimal > 0) {
                                        $rem = $rem - 1;
                                        echo '<span class="star half"></span>';
                                    } else {
                                        $rem = $rem;
                                    }
                                    for($i=0;$i<$rem;$i++) {
                                        echo '<span class="star"></span>';
                                    } ;
                                    ?><br><?php echo $fetchusers_complaint['user_feedback']; ?>
                                    </div>
                                </td>
                                <td><a target="_blank" href="printComplaint.php?id=<?php echo $fetchusers_complaint['ucid']; ?>" class="btn btn-xs btn-outline-success">Print</a></td>
                                <td><a href="updateComplaint.php?id=<?php echo $fetchusers_complaint['ucid']; ?>" <?php if($fetchusers_complaint['c_status'] == '2') { } else { echo 'style="pointer-events:none;background-color: #cccccc;border: 1px solid #999999;cursor: not-allowed;color: #666666;"';}?> class="btn btn-xs btn-outline-primary">Add Feedback & Rating</a></td>
                                    <div class="modal fade" id="assignBModal<?php echo $fetchusers_complaint['ucid']; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel-2" aria-hidden="true">
                                      <div class="modal-dialog" role="document">
                                        <div class="modal-content">
                                          <div class="modal-header">
                                            <h5 class="modal-title" id="exampleModalLabel-2">Complaint Detail</h5>
                                            <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
                                              <span aria-hidden="true">&times;</span>
                                            </button>
                                          </div>
                                          <div class="modal-body">
                                              <b>Complaint No. : </b><?php echo $fetchusers_complaint['docketNumber']; ?><br>
                                              <b>Date/Time of Complaint Received : </b><?php $date =  $fetchusers_complaint['date'];
                                    $timestamp = strtotime($date);
                                    if ($timestamp === FALSE) {
                                      $timestamp = strtotime(str_replace('/', '-', $date));
                                    }
                                    echo date('d-m-y',$timestamp); ?> (<?php echo date ( 'H:i:s' , strtotime($fetchusers_complaint['time']) ); ?>)<br>
                    						 <?php $buildingls = $conn->query("SELECT * FROM building_list WHERE bid='".$fetchusers_complaint['b_list']."'")->fetch_assoc(); ?>
                    						 <b> Building Name : </b><?php echo $buildingls['bName']; ?><br>
                    						 <b> Building No. : </b><?php echo $buildingls['bNo']; ?><br>
                    						 <b> Complaint Details : </b><?php echo $complaint_list['details']; ?><br>
                    						 <b> Subdivision : </b><?php echo $subdivision_list['value']; ?><br>
                    						 <b> Complaint Description : </b><?php echo $fetchusers_complaint['details']; ?><br>
                    						 <b> Priority : </b><?php echo $fetchusers_complaint['priority']; ?><br>
                    						 <b> Tradesman Engineer : </b><?php if(!empty($engname)) { echo $engname; } else {echo 'N/A';} ?><br>
                    						 <b> Date/Time of Completion : </b><?php if($fetchusers_complaint['c_status'] == '2') { echo date('d-m-y H:i:s', strtotime($fetchusers_complaint['completiondate'])); } else {echo 'N/A';} ?><br>
                    						 <b> Official Remarks : </b><?php echo $fetchusers_complaint['remarks']; ?><br>
                    						 <b> User Satisfaction : </b><div class="stars" style="display:inline">
                                                                        <?php $user_ratings = $fetchusers_complaint['user_ratings'];
                                                                                $whole = intval($user_ratings);
                                                                                $rem = 5 - $whole;
                                                                                $decimal1 = $user_ratings - $whole;
                                                                                $decimal2 = round($decimal1, 2);
                                                                                $decimal = substr($decimal2, 2);
                                                                            for($i=0;$i<$whole;$i++) {
                                                                                echo '<span class="star on"></span>';
                                                                            } ;
                                                                            if($decimal > 0) {
                                                                                $rem = $rem - 1;
                                                                                echo '<span class="star half"></span>';
                                                                            } else {
                                                                                $rem = $rem;
                                                                            }
                                                                            for($i=0;$i<$rem;$i++) {
                                                                                echo '<span class="star"></span>';
                                                                            } ;
                                                                            ?><br>
                                                                            <?php echo $fetchusers_complaint['user_feedback']; ?>
                                                                            </div>
                                        </div>
                                      </div>
                                    </div>
                                    </div>
                                    <div class="modal fade" id="assignIModal<?php echo $fetchusers_complaint['ucid']; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel-2" aria-hidden="true">
                                      <div class="modal-dialog modal-lg" role="document">
                                        <div class="modal-content">
                                          <div class="modal-header">
                                            <h5 class="modal-title" id="exampleModalLabel-2">Complaint Images</h5>
                                            <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
                                              <span aria-hidden="true">&times;</span>
                                            </button>
                                          </div>
                                          <div class="modal-body">
                                              
                                              <div class="row grid-margin">
                                                  <div class="col-lg-12">
                                                    <iframe frameborder="0" scrolling="no" style="overflow:hidden;height:450px;width:100%;background:transparent;" src="gallery.php?id=<?php echo $fetchusers_complaint['ucid']; ?>"></iframe>
                                                  </div>
                                                </div>
                                            </div>
                                        </div>
                                      </div>
                                    </div>
                              </tr>
                          <?php }} else {
                          echo '<tr><td colspan="16"><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'); ?>
    <script>
    $( document ).ready(function() {
        $('#resetbtn').click(function(){
            $('#Filterform')[0].reset();
            $('input[type="text"]').val('');
            $('select').val('');
      });
    });
    function updatePrority(priority, id) {
        $.ajax({
				type: "POST",
				url: "updatePrority.php",
				data: {priority: priority, id: id},
				success: function (r) {
					alert('Priority updated successfully.');
				}
			});
    }
    function getmyparent(engineer, comp) {
        $.ajax({
				type: "POST",
				url: "getDept.php",
				data: {engineer: engineer},
				success: function (r) {
					$('#did'+comp).val(r);
				}
			});
    }
    $('#dept_id').removeClass('select2-hidden-accessible');
    $('#select2-dept_id-container').parents('.select2-selection').css('display','none');
    </script>
  </body>
</html>