<?php include "headerLink.php"?>
<!DOCTYPE html>
<html lang="en">


<body class="animsition">
    <div class="page-wrapper">
       

        <!-- PAGE CONTAINER-->
        <!--<div class="page-container">-->
            <!-- HEADER DESKTOP-->
			
            <!-- END HEADER DESKTOP-->
			
			
			
			<!-- MAIN CONTENT-->
			<?php
			date_default_timezone_set("Asia/Kuala_Lumpur");
             $tenant_id = $_GET["tenant_id"];
             $tenant_name = $_GET["tenant_name"];
             $unit_no = $_GET["unit_no"];
			 
			 $successUpdate = "";
			 
			 //deactivate TENANT
			 //update 2 table
			 //1. table tenants - update status from enable to disable, start_date (if Null) and end_date and updated_at column
			 //2. table tenant_rental_rate - update status from enable to disable, start_date (if Null) and end_date and updated_at column
			 
			 
            //get date start_date, end_date, rental_rate, status start
             $sqlTenant = "select rental_rate, start_date, end_date, status from tenant_rental_rate where tenant_id = $tenant_id ";
            $runTenant = mysqli_query($link, $sqlTenant);
            if($runTenant === FALSE){
               die(mysqli_error($link));
            }
            $resultTenant = mysqli_fetch_array($runTenant);
            $rental_rate = $resultTenant["rental_rate"];
            $start_date = $resultTenant["start_date"];
            $end_date = $resultTenant["end_date"];
            $tenant_status = $resultTenant["status"];
			
			 "startDateFormat: " . $startDateFormat = date("Y-m-d",strtotime($start_date));
			 "endDateFormat: " .$endDateFormat = date("Y-m-d",strtotime($end_date));
			$startDateMonthYear = date("m/Y",strtotime($start_date));
			
             $startDateMonthYear = "0";
            IF($start_date == "0000-00-00 00:00:00" || $end_date == "0000-00-00 00:00:00"){
                  // "START DATE 1:::: ". $start_date;
                  $startDateFormat = "";
                  $endDateFormat = "";
            }
			
			
			
			if (isset($_GET["submit"])) {
				 "post";
			  // form has been submitted, process the data
			  $tenant_id = $_GET["tenant_id"];
			  $tenant_name = $_GET["tenant_name"];
			  $unit_no = $_GET["unit_no"];
			  $tenant_status = $_GET["tenant_status"];
			  $start_date = date("Y-m-d",strtotime($_GET["start_date"]));
			  $end_date =date("Y-m-d",strtotime($_GET["end_date"]));
			  $date_update = date("Y-m-d H:i:s");
			  $locationName = $_SESSION["locationName"];
			  
			  //update table tenants
			 $sqlUpdateTenant = "update tenants set status = '$tenant_status', start_date = '$start_date', end_date = '$end_date' , updated_at = '$date_update' where id = '$tenant_id'";
			$runUpdateTenant = mysqli_query($link, $sqlUpdateTenant);
			if($runUpdateTenant === FALSE){
			   die(mysqli_error($link));
			}
			
			//update tenant_rental_rate
			 $sqlUpdateTenantRentalRate = "update tenant_rental_rate set status = '$tenant_status', start_date = '$start_date', end_date = '$end_date' , updated_at = '$date_update'  where tenant_id = '$tenant_id'";
			$runUpdateTenantRentalRate = mysqli_query($link, $sqlUpdateTenantRentalRate);
			if($runUpdateTenantRentalRate === FALSE){
			   die(mysqli_error($link));
			}
			
			$successUpdate = "SUCCESS UPDATE TENANT $tenant_name ($unit_no, $locationName) ";
			
			}
         
			?>
			
            
            <div class="main-content"  >
                <div class="section__content section__content--p30" >
                    <div class="container-fluid" >
                        <!-- menu show on page after selected - start -->
                        
						<!-- menu show on page after selected - end -->
						<div class="row" >
                            <div class="col col-md-12" >
                                <div class="card">
								<?php if($successUpdate == ""){?>
                                 <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="get">
                                    <div class="card-header ">
                                        <input type="hidden" name="tenant_id" value="<?php echo $tenant_id?>">
                                                <label  class="form-check-label ">
                                                    <B>DEACTIVATE TENANT</B>
                                                </label>
                                    </div>
                                    <div class="card-header">
                                        <h5 class="m-0 font-weight-bold text-primary">
                                            TENANT INFO
                                        </h5>
                                    </div>
                                    <div class="card-body card-block">
                                        <div class="row" valign="middle">
                                            <div class="col col-md-6">
                                                <label class=" form-control-label">NAME </label>
                                            </div>
                                            <div class="col col-md-6" >
                                               <input type="text" id="tenant_name"  name="tenant_name"  class="form-control" required="required"   placeholder="" style="text-align: right;" value="<?php echo $tenant_name?>" readonly="readonly">
                                            </div>
                                        </div>
                                        <div class="row" valign="middle">
                                            <div class="col col-md-6">
                                                <label class=" form-control-label">UNIT NO.</label>
                                            </div>
                                            <div class="col col-md-6" >
                                                 <input type="text" id="unit_no"  name="unit_no"  class="form-control" required="required"   placeholder="" style="text-align: right;" value="<?php echo $unit_no . ", ".   $_SESSION["locationName"]?>" readonly="readonly">
                                            </div>
                                        </div>
										<div class="row" valign="middle">
                                            <div class="col col-md-6">
                                                <label class=" form-control-label">TENANT STATUS</label>
                                            </div>
                                            <div class="col col-md-6" >
                                                <select name="tenant_status" id="tenant_status" class="form-control form-control" required="required" >
													<option value="">Please select</option>
													<option value="enable" <?php echo ($tenant_status == 'enable' ? 'selected':'')?>>Active</option>
													<option value="disable" <?php echo ($tenant_status == 'disable' ? 'selected':'')?> >Deactive</option>
												</select>
                                            </div>
                                        </div>
										<div class="row" valign="middle">
                                            <div class="col col-md-6">
                                                <label class=" form-control-label">START DATE (MULA MENYEWA)</label>
                                            </div>
                                            <div class="col col-md-6" >
                                                 <input type="date" id="start_date"  name="start_date"  class="form-control" required="required"   placeholder="" style="text-align: right;" value="<?php echo date("Y-m-d",strtotime($start_date))?>" >
                                            </div>
                                        </div>
										<div class="row" valign="middle">
                                            <div class="col col-md-6">
                                                <label class=" form-control-label">END DATE (TAMAT SEWA)</label>
                                            </div>
                                            <div class="col col-md-6" >
                                                 <input type="date" id="end_date"  name="end_date"  class="form-control" required="required"   placeholder="" style="text-align: right;" value="<?php echo $endDateFormat?>" >
                                            </div>
                                        </div>
                                    </DIV>
                                   

                                    <div class="card-footer">
                                         
                                        <button type="submit" id="submitBtn" name="submit" class="btn btn-outline-success btn-sm" style="float:right"  >Submit</button>
                                        <button type="reset" style="float:right" class="btn btn-outline-warning btn-sm">Reset</button>
                                    </div>
                                    </form>
								<?php } 
									else{
										?>
									<div class="card-header ">
										<label  class="form-check-label ">
											<B>DEACTIVATE TENANT</B>
										</label>
                                    </div>
                                    <div class="card-header">
                                        <h5 class="m-0 font-weight-bold text-primary">
                                          <?php echo  $successUpdate;?>
                                        </h5>
                                    </div>
										
										<?php
										
									}
								?>
                                </div>
                            </div>
                        </div>
                        
                        <?PHP include "footer.php";?>
                        
                    </div>
                </div>
            </div>
            <!-- END MAIN CONTENT-->
            <!-- END PAGE CONTAINER-->
        </div>
		
   <!-- </div>-->
    <?PHP include "footerLink.php";?>
</body>

</html>
<!-- end document-->

<script>


//window.close();
    window.onunload = refreshParent;
    function refreshParent() {
        window.opener.location.reload();
    }

</script>