<?php include('usersession.php'); 
if(isset($_SESSION['ecimsuser']) && !empty($_SESSION['ecimsuser'])) { 
$sqlu = "SELECT * FROM users WHERE uid = '".$_SESSION['ecimsuser']."'";
$queryu = $conn->query($sqlu);
$ecimsuser = $queryu->fetch_assoc();?>
<!DOCTYPE html>
<html lang="en">
  <head>
	<title>ECIMS USER | NEW COMPLAINT</title>
    <?php include('includes/head.php'); ?>
    <style>
        .alert-success {
            color: #000;
        }
    </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="page-header">
              <h3 class="page-title"> New Complaint </h3>
            </div>
            <div class="row">
              <div class="col-12 grid-margin stretch-card">
                <div class="card">
                  <div class="card-body">
                      <?php $err = $msg = '';
							if(isset($_POST['submit'])) {
								$date = date("d/m/Y");
                                $d = date("dmy"); 
                                $time = date("g:i a");
                                $curtime = date("H:i");
                                $uid = $_POST['uid'];
                                $b_list = $_POST['b_list'];
                                $details = $_POST['details'];
                                $c_list = $_POST['c_list'];
                                $complaint = $conn->query("SELECT * FROM complaint_list WHERE cid = '".$c_list."'")->fetch_assoc();
                                $priority = $complaint['priority'];
                                $subdivision = $complaint['subdivision'];
                                $todaycompcount = $conn->query("SELECT ucid FROM users_complaint WHERE date = '".$date."'")->num_rows;
                                $thiscount = $todaycompcount + 1;
                                $docketNumber = $d.sprintf("%02d", $thiscount);
                                if($curtime >= '21:30') {
                                    if($priority == '1') {
                                        $newQ = "INSERT INTO users_complaint(date , b_list  , uid , details , c_list , time , priority , subdivision , docketNumber ) VALUES ('".$date."' , '".$b_list."' , '".$uid."' , '".$details."' , '".$c_list."' , '".$time."' , '".$priority."' , '".$subdivision."' , '".$docketNumber."') ";
                                        $result = $conn->query($newQ);
                                    } else {
                                        $newQ = "INSERT INTO users_complaint(date , b_list  , uid , details , c_list , time , priority , subdivision , docketNumber, c_status ) VALUES ('".$date."' , '".$b_list."' , '".$uid."' , '".$details."' , '".$c_list."' , '".$time."' , '".$priority."' , '".$subdivision."' , '".$docketNumber."', '4') ";
                                        $result = $conn->query($newQ);
                                    }
                                } else {
                                    $newQ = "INSERT INTO users_complaint(date , b_list  , uid , details , c_list , time , priority , subdivision , docketNumber ) VALUES ('".$date."' , '".$b_list."' , '".$uid."' , '".$details."' , '".$c_list."' , '".$time."' , '".$priority."' , '".$subdivision."' , '".$docketNumber."') ";
                                    $result = $conn->query($newQ);
                                }
                                
                                
                                $str = "";
                                if($result) {
                                    $ucid = $conn->insert_id;
                                    $targetDir = "Images/"; 
                                    $allowTypes = array('jpg','png','jpeg','gif'); 
                                     
                                    $insertValuesSQL = $errorUpload = $errorUploadType = ''; 
                                    $fileNames = array_filter($_FILES['files']['name']); 
                                    if(!empty($fileNames)){ 
                                        foreach($_FILES['files']['name'] as $key=>$val){ 
                                            // File upload path
                                            
                                            $fileName = basename($_FILES['files']['name'][$key]); 
                                            $targetFilePath = $targetDir . $fileName; 
                                             
                                            // Check whether file type is valid 
                                            $fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION); 
                                            if(in_array($fileType, $allowTypes)){ 
                                                // Upload file to server 
                                                if(move_uploaded_file($_FILES["files"]["tmp_name"][$key], $targetFilePath)){ 
                                                    // Image db insert sql 
                                                    $insertValuesSQL .= "('".$fileName."', '".$uid."', '".$ucid."'),"; 
                                                }else{ 
                                                    $errorUpload .= $_FILES['files']['name'][$key].' | '; 
                                                } 
                                            }else{ 
                                                $errorUploadType .= $_FILES['files']['name'][$key].' | '; 
                                            } 
                                        } 
                                         
                                        // Error message 
                                        $errorUpload = !empty($errorUpload)?'Upload Error: '.trim($errorUpload, ' | '):''; 
                                        $errorUploadType = !empty($errorUploadType)?'File Type Error: '.trim($errorUploadType, ' | '):''; 
                                        $err1 = !empty($errorUpload)?'<br/>'.$errorUpload.'<br/>'.$errorUploadType:'<br/>'.$errorUploadType; 
                                         
                                        if(!empty($insertValuesSQL)){ 
                                            $insertValuesSQL = trim($insertValuesSQL, ','); 
                                            // Insert image file name into database 
                                            $insert = $conn->query("INSERT INTO users_complaint_images (img, uid, ucid) VALUES $insertValuesSQL"); 
                                            if($insert){ 
                                                $msg = "Complaint added successfully. <br> Your complaint number is ".$docketNumber; 
                                            }else{ 
                                                $err = "Sorry, there was an error uploading your file."; 
                                            } 
                                        }else{ 
                                            $err = "Upload failed! "; 
                                        } 
                                    } else{ 
                                        $msg = 'Complaint added successfully with no image. <br> Your complaint number is '.$docketNumber;
                                    }
                                }
    						}
    						if(!empty(trim($err))){
    						  echo "
    							<div class='alert alert-danger alert-dismissible'>
    							  <h4><i class='fa fa-warning'></i> Error!</h4>
    							  ".$err."
    							</div>
    						  ";
    						}
    						if(!empty($msg)){
    						  echo "
    							<div class='alert alert-success alert-dismissible'>
    							  <h4><i class='fa fa-check'></i> Success!</h4>
    							  ".$msg."
    							</div>
    						  ";
    						}
						?>
                    <form class="forms-sample" method="POST" enctype="multipart/form-data">
                        <input type="hidden" name="uid" value="<?php echo $ecimsuser['uid']; ?>"/>
                      <div class="form-group">
                        <label for="c_list">Select Complaint</label>
                        <select class="form-control" name="c_list" required id="c_list">
                            <option value="">Type / Select Complaint</option>
						      <?php $comp = $conn->query("SELECT * FROM complaint_list"); 
						  while($fetchcomp = $comp->fetch_assoc()) { ?>
                          <option value="<?php echo $fetchcomp['cid']; ?>"><?php echo $fetchcomp['details']; ?></option>
						  <?php } ?>
                        </select>
                      </div>
                      <div class="form-group">
                        <label for="b_list">Select Address</label>
                        <select class="form-control" name="b_list" required id="b_list">
                            <option value="">Type / Select Address</option>
						      <?php $buil = $conn->query("SELECT * FROM building_list WHERE isDeleted='0'"); 
						  while($fetchbuild = $buil->fetch_assoc()) { ?>
                          <option value="<?php echo $fetchbuild['bid']; ?>"><?php echo $fetchbuild['bNo'].', '.$fetchbuild['bName']; ?></option>
						  <?php } ?>
                        </select>
                      </div>
                      <div class="form-group">
                        <label for="details">Brief Description</label>
                        <textarea class="form-control" name="details" required id="details" rows="4" placeholder="Write about your complaint."></textarea>
                      </div>
                      <div class="form-group">
                        <label>Attach Images(optional)</label>
                        <p style="color:red">You can add multiple images using (ctrl + click)</p>
                        <input type="file" name="files[]" class="file-upload-default" multiple>
                        <div class="input-group col-xs-12">
                          <input type="text" class="form-control file-upload-info" disabled placeholder="Upload Image">
                          <span class="input-group-append">
                            <button class="file-upload-browse btn btn-primary" style="height: 50px;border-radius: 0px 5px 5px 1px;" type="button">Upload</button>
                          </span>
                        </div>
                        <ul class="mt-2">
                            <li>Please upload valid image of complaint area. (its optional not compulsory to share image).</li>
                            <li>Image should be crisp & clear and should not be crop or resize.</li>
                        </ul>
                      </div>
                      <button type="submit" name="submit" class="btn btn-primary me-2">Submit</button>
                    </form>
                  </div>
                </div>
              </div>
            </div>
          </div>
		  <?php include('includes/footer.php'); ?>
        </div>
      </div>
    </div>
    <?php include('includes/foot.php'); ?>
  </body>
</html>
<?php } ?>