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


<body class="animsition">
    <div class="page-wrapper">
        <!-- HEADER MOBILE-->
        <?php include "menuSideBar_mobile.php" ;?>
        <!-- END HEADER MOBILE-->
		
        <!-- MENU SIDEBAR-->
        <?php include "menuSideBar.php" ;?>
        <!-- END MENU SIDEBAR-->

        <!-- PAGE CONTAINER-->
        <div class="page-container">
            <!-- HEADER DESKTOP-->
			<?php include "headerPage.php"?>
            <!-- END HEADER DESKTOP-->
			
			
			
			<!-- MAIN CONTENT-->
			<?php
				$report_type=$_GET["report_type"];
				$_SESSION["report_type"] =  $report_type;
				if($_SESSION["projectLocation"] != ""){
					$projectLocation = $_SESSION["projectLocation"];
				}
				else if($_GET["projectLocation"] != ""){
					$projectLocation = $_GET["projectLocation"];
				}
				//$projectLocation = $_GET["projectLocation"];
				if($projectLocation != "" || $projectLocation != null ){
					$_SESSION["projectLocation"];
					include 'splitLocationName_project.php';
				}
				else{
					echo "<center><font size='6' color='red'>Please Select Location Above.</font></center>";
					$location_id = "";
				}
				$bill_types_id = "";
				if($report_type != "" || $report_type != null){
					 $sqlBillTypeID = "select id from bill_types where UPPER(bill_type_name) = UPPER('$report_type')";
					$runBillTypeID = mysqli_query($link, $sqlBillTypeID);
						if($runBillTypeID === FALSE){
							die(mysqli_error("SQL DIE! Error for SQL: ".$sqlBillTypeID));
						}
					$resultBillTypeID = mysqli_fetch_assoc($runBillTypeID);
					$bill_types_id = $resultBillTypeID["id"];
				}
				$bill_type_Outstanding = 0;

				if($location_id != ""){
					//echo "MASUK LOCATION ID: ".$location_id;
					if($location_id == 2)
						$bill_type_Outstanding = 4;
					else if($location_id == 3)
						$bill_type_Outstanding = 3;
					else if($location_id == 4)
						$bill_type_Outstanding = 5;
				}


				if(isset($_GET["submit"])){
					echo "submit!";
					$dateFrom = $_GET["dateFrom"];
					$dateTo = $_GET["dateTo"];
					$_SESSION["dateFrom"] = $dateFrom;
					$_SESSION["dateTo"] = $dateTo;
					
					$dateTo_cutOFF = date('Y-m-d', strtotime($dateTo. ' + 1 days'));	
					
					if($report_type == "Deposit"){
						  $sqlDeposit = "select deposits.*,tenants.id as tenant_id, tenants.tenant_name, properties.unit_no  from deposits inner join 
										tenants on deposits.tenant_id = tenants.id
										inner join properties on tenants.property_id = properties.id
										inner join locations on properties.location_id = locations.id
										inner join projects on locations.project_id = projects.id 
										where locations.id = $location_id 
										and projects.id = $project_id
										and (deposits.created_at between '$dateFrom' and '$dateTo') order by deposits.created_at ASC
										";
						$runDeposit = mysqli_query($link, $sqlDeposit);
							if($runDeposit === FALSE){
							die(mysqli_error("SQL DIE! Error for SQL: ".$sqlDeposit));
						}
					}
					else if($report_type == "Rental"){
						  $sqlRental = "select payments.*, tenants.tenant_name, properties.unit_no, tenants.rental_rate, bills.issue_date,  users.name, locations.location_name,projects.project_name, bill_types.bill_type_name, bills.bill_type_id
										from payments 
										inner join bills on payments.bill_id = bills.id
										inner join bill_types on bills.bill_type_id = bill_types.id
										inner join tenants on bills.tenant_id = tenants.id 
										inner join properties on tenants.property_id = properties.id
										inner join locations on properties.location_id = locations.id
										inner join projects on locations.project_id = projects.id
										inner join users on payments.created_by =  users.id
										where bills.bill_type_id in ($bill_types_id,$bill_type_Outstanding)  and locations.id = $location_id 
										and projects.id = $project_id and (payments.created_at >= '$dateFrom' and payments.created_at < '$dateTo_cutOFF') order by payments.created_at ASC
										
										";
										
						$runRental= mysqli_query($link, $sqlRental);
						if($runRental=== FALSE){
							//die(mysqli_error("SQL DIE! Error for SQL: ".$sqlRental));
							die(mysqli_error($link));
						}
					}
					else if($report_type == "Water"){
						 $sqlWater = "select payments.*, tenants.tenant_name, properties.unit_no, tenants.rental_rate, bills.issue_date,  users.name, locations.location_name,projects.project_name, bill_types.bill_type_name, bills.bill_type_id
										from payments 
										inner join bills on payments.bill_id = bills.id
										inner join bill_types on bills.bill_type_id = bill_types.id
										inner join tenants on bills.tenant_id = tenants.id 
										inner join properties on tenants.property_id = properties.id
										inner join locations on properties.location_id = locations.id
										inner join projects on locations.project_id = projects.id
										inner join users on payments.created_by =  users.id
										where bills.bill_type_id = $bill_types_id and locations.id = $location_id 
										and projects.id = $project_id and (payments.created_at >= '$dateFrom' and payments.created_at < '$dateTo_cutOFF') order by payments.created_at ASC
										";
						$runWater= mysqli_query($link, $sqlWater);
						if($runWater=== FALSE){
							die(mysqli_error("SQL DIE! Error for SQL: ".$sqlWater));
						}
					}
				}
				?>
		
			
            
            <div class="main-content"  >
                <div class="section__content section__content--p30" >
                    <div class="container-fluid" >
                        <!-- menu show on page after selected - start -->
                        <?php include "selectedMenuSubMenu.php"?>
						<!-- menu show on page after selected - end -->
						<div class="row" >
							<div class="col-lg-12">	
								<div class="card shadow mb-4">
									<div class="card-header py-3">
										<h3 class="m-0 font-weight-bold text-primary">
											Report
										</h3>
									</div>
									<div class="card-body">
										<div class="table-responsive">
											<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0" style="font-size: 12px">
												<tr>
													<td  width="30%">
														<div class="card-header py-3">
															<h5 class="m-0 font-weight-bold text-primary">Search</h5>
														</div>
														<div class="card-body">
															<div class="table-responsive">
																<form action="<?php $_SERVER['PHP_SELF']; ?>" method="get" enctype="multipart/form data" >
																	<input type="hidden" name="login" value="<?php echo $_GET["login"]?>">
																	<table class="table " id="dataTable" width="30%" cellspacing="0" style="font-size: 12px">
																		<tr>
																			<td>
																				<div class="row form-group">
																					<div class="col col-md-3">
																						<label class=" form-control-label">Report Type</label>
																					</div>
																					<div class="col col-md-9">
																						<div class="form-check">
																							<div class="radio">
																								<label for="radio1" class="form-check-label ">
																									<input type="radio" id="radio1" name="report_type" value="Deposit" class="form-check-input" <?php echo ($_SESSION["submenu"] == 'Deposit' || $_SESSION["report_type"] == 'Deposit' ? 'CHECKED' : '');?>> Deposit 
																								</label>
																							</div>
																							<div class="radio">
																								<label for="radio2" class="form-check-label ">
																									<input type="radio" id="radio2" name="report_type" value="Rental" class="form-check-input" <?php echo ($_SESSION["submenu"] == 'Rental' || $_SESSION["report_type"] == 'Rental' ? 'CHECKED' : '');?>> Rental   
																								</label>
																							</div>
																							<div class="radio">
																								<label for="radio3" class="form-check-label ">
																									<input type="radio" id="radio3" name="report_type" value="Water" class="form-check-input" <?php echo ($_SESSION["submenu"] == 'Water' || $_SESSION["report_type"] == 'Water' ? 'CHECKED' : '');?>> Water
																								</label>
																							</div>
																						</div>
																					</div>
																				</div>
																			</td>
																		</tr>
																		<tr>
																			<td>
																				<div><b>From Date</b></div>
																				<div>
																					<input id='dateFrom' name='dateFrom' type='date' size='10' value="<?php echo $_SESSION["dateFrom"]?>"> 
																				</div>
																			</td>
																		</tr>
																		<tr>
																			<td>
																				<div><b>To Date</b></div>
																				<div>
																					<input id='dateTo' name='dateTo' type='date' size='10' value="<?php echo $_SESSION["dateTo"]?>"> 
																				</div>
																			</td>
																		</tr>
																		<tr>
																			<td>
																				<button class="btn btn-primary" type="submit" name="submit">Submit</button>
																			</td>
																		</tr>
																	</table>
																</form>
															</div>
														</div>
													</td>
													<td width="70%">                                                                                                                                                                                                                                                                                                                                             
														<div class="card-header py-3">
															<h5 class="m-0 font-weight-bold text-primary">Showing Result</h5>
														</div>
														<?php 
															if($report_type == "Deposit"){
																include "report_deposit.php";
															}
															else if($report_type == "Rental"){
																include "report_rental.php";
															}
															else if($report_type == "Water"){
																include "report_water.php";
															}
														?>
													</td>
												</tr>
											</table>
										</div>
									</div>
								</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-->
