<!DOCTYPE html>
<html lang="en">

<?php include "headerLink.php"?>

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

        <!-- PAGE CONTAINER-->
        <div class="page-container2">
            <!-- HEADER DESKTOP-->
            <?php include "headerPage.php"?>
           
            <!-- END HEADER DESKTOP-->

			<?php
			$sqlLocationSelected = "";
			$sqlLocationSelected_forOccupancy = "";
			if($_SESSION["projectLocation"] == ""){
				$total_invoice = 0;
				$total_payment = 0;
				$percent_tenanted = 0;
				$total_outstanding = 0;
			}
			else if($_SESSION["location_id"] != ""){
				//echo "LOCATION ID: ".$_SESSION["location_id"];
				$sqlLocationSelected = " and properties.location_id =  " .$_SESSION["location_id"];
				$sqlLocationSelected_forOccupancy = " where properties.location_id = " .$_SESSION["location_id"];
				//Invoice (Current Month - Rental)
				//total_invoice start
				//get current month
				$currentDate = date("Y-m-01");
				//search total invoice out for current month - tenant active only
				$sqlTotalInvoice = "select sum(bill.amount) as total_invoice from bill 
									inner join tenant on bill.tenant_id = tenant.tenant_id
									inner join properties_unit on tenant.unit = properties_unit.id
									inner join properties on properties.id = properties_unit.property_id
									inner join locations on properties.location_id = locations.location_id 
									inner join extra_locations on locations.location_id = extra_locations.location_id
									inner join r_rental_program on locations.rental_id_program = r_rental_program.id_rental_progrm
									where tenant.tenant_status = 'Active' and bill.rental_month = '$currentDate'  $sqlLocationSelected
									";
				$runTotalInvoice = mysqli_query($link, $sqlTotalInvoice);//or die ("Cannot execute program");

				if($runTotalInvoice === FALSE){
					die(mysqli_error($link));
				}
				$resultTotalInvoice = mysqli_fetch_assoc($runTotalInvoice);
				$total_invoice = $resultTotalInvoice["total_invoice"];
				//total invoice end
				
				//total payment start
				//search total payment receive for current month - knock off with invoice
				$sqlTotalPayment = "select sum(pay_amount) as total_payment from payment
									inner join bill on payment.bill_id = bill.bill_id
									inner join tenant on bill.tenant_id = tenant.tenant_id
									inner join properties_unit on tenant.unit = properties_unit.id
									inner join properties on properties.id = properties_unit.property_id
									inner join locations on properties.location_id = locations.location_id 
									inner join extra_locations on locations.location_id = extra_locations.location_id
									inner join r_rental_program on locations.rental_id_program = r_rental_program.id_rental_progrm
									where bill.rental_month = '$currentDate'  $sqlLocationSelected
									";
				$runTotalPayment = mysqli_query($link, $sqlTotalPayment);//or die ("Cannot execute program");

				if($runTotalPayment === FALSE){
					die(mysqli_error($link));
				}
				$resultTotalPayment = mysqli_fetch_assoc($runTotalPayment);
				$total_payment = $resultTotalPayment["total_payment"];
				//total payment end
				
				//occupancy start
				//tenanted = ada penyewa
				 $sqlOccupancy_tenanted = "select count(properties_unit.id) as tenant_active, count(properties_unit.id) * 100 / (select count(*) from properties_unit
													inner join properties on properties_unit.property_id = properties.id
													inner join locations on properties.location_id = locations.location_id
													inner join r_rental_program on locations.rental_id_program = r_rental_program.id_rental_progrm  $sqlLocationSelected_forOccupancy) 
											as percent_tenanted from properties_unit
											inner join properties on properties.id = properties_unit.property_id
											inner join locations on properties.location_id = locations.location_id 
											inner join extra_locations on locations.location_id = extra_locations.location_id
											inner join r_rental_program on locations.rental_id_program = r_rental_program.id_rental_progrm
											where properties_unit.unit_status = 'disable' $sqlLocationSelected ";
				$runOccupancy_tenanted  = mysqli_query($link, $sqlOccupancy_tenanted);//or die ("Cannot execute program");

				if($runOccupancy_tenanted === FALSE){
					die(mysqli_error($link));
				}
				$resultOccupancy_tenanted = mysqli_fetch_assoc($runOccupancy_tenanted);
				$tenant_active = $resultOccupancy_tenanted["tenant_active"];
				$percent_tenanted = $resultOccupancy_tenanted["percent_tenanted"];
				//tenanted = ada penyewa end
				
				//total outstanding all start
				//get current month
				$currentDate = date("Y-m-01");
				//search total invoice out for current month - tenant active only
				 $sqlTotalOutstanding = "select sum(bill.bill_bal) as total_outstanding from bill 
									inner join tenant on bill.tenant_id = tenant.tenant_id
									inner join properties_unit on tenant.unit = properties_unit.id
									inner join properties on properties.id = properties_unit.property_id
									inner join locations on properties.location_id = locations.location_id 
									inner join extra_locations on locations.location_id = extra_locations.location_id
									inner join r_rental_program on locations.rental_id_program = r_rental_program.id_rental_progrm
									where bill.rental_month < '$currentDate'  $sqlLocationSelected
									";
				$runTotalOustanding = mysqli_query($link, $sqlTotalOutstanding);//or die ("Cannot execute program");

				if($runTotalOustanding === FALSE){
					die(mysqli_error($link));
				}
				$resultTotalOutstanding = mysqli_fetch_assoc($runTotalOustanding);
				$total_outstanding = $resultTotalOutstanding["total_outstanding"];
				//total outstanding end
				
			}
			?>
			
            <!-- DASHBOARD STATISTIC-->
            <section class="statistic">
                <div class="section__content section__content--p30">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-md-6 col-lg-3">
                                <div class="statistic__item">
                                    <h2 class="number"><?php echo 'RM'.number_format($total_invoice,2,'.',',') ?></h2>
                                    <span class="desc">Total Invoice 
                                   <br/>(Current Month) </span>
                                    <div class="icon">
                                        <i class="zmdi zmdi-calendar-note"></i>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6 col-lg-3">
                                <div class="statistic__item">
                                    <h2 class="number"><?php echo 'RM'.number_format($total_payment,2,'.',',') ?></h2>
                                     <span class="desc">Total Payment 
                                   <br/>(Current Month) </span>
                                    <div class="icon">
                                        <i class="zmdi zmdi-money"></i>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6 col-lg-3">
                                <div class="statistic__item">
                                    <h2 class="number"><?php echo number_format($percent_tenanted,2)?>%</h2>
                                    <span class="desc">Occupancy</span>
                                    <div class="icon">
                                        <i class="zmdi zmdi-city"></i>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6 col-lg-3">
                                <div class="statistic__item">
                                    <h2 class="number"><?php echo 'RM'.number_format($total_outstanding,2,'.',',')?></h2>
                                    <span class="desc">total Outstanding</span>
                                    <div class="icon">
                                        <i class="zmdi zmdi-alert-circle-o"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>
            <!-- END STATISTIC-->

            <section>
                <div class="section__content section__content--p30">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-xl-8">
                                <!-- RECENT REPORT 2-->
                                <div class="recent-report2">
                                    <h3 class="title-3">recent reports</h3>
                                    <div class="chart-info">
                                        <div class="chart-info__left">
                                            <div class="chart-note">
                                                <span class="dot dot--blue"></span>
                                                <span>products</span>
                                            </div>
                                            <div class="chart-note">
                                                <span class="dot dot--green"></span>
                                                <span>Services</span>
                                            </div>
                                        </div>
                                        <div class="chart-info-right">
                                            <div class="rs-select2--dark rs-select2--md m-r-10">
                                                <select class="js-select2" name="property">
                                                    <option selected="selected">All Properties</option>
                                                    <option value="">Products</option>
                                                    <option value="">Services</option>
                                                </select>
                                                <div class="dropDownSelect2"></div>
                                            </div>
                                            <div class="rs-select2--dark rs-select2--sm">
                                                <select class="js-select2 au-select-dark" name="time">
                                                    <option selected="selected">All Time</option>
                                                    <option value="">By Month</option>
                                                    <option value="">By Day</option>
                                                </select>
                                                <div class="dropDownSelect2"></div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="recent-report__chart">
                                        <canvas id="recent-rep2-chart"></canvas>
                                    </div>
                                </div>
                                <!-- END RECENT REPORT 2             -->
                            </div>
                            <div class="col-xl-4">
                                <!-- TASK PROGRESS-->
                                <div class="task-progress">
                                    <h3 class="title-3">task progress</h3>
                                    <div class="au-skill-container">
                                        <div class="au-progress">
                                            <span class="au-progress__title">Web Design</span>
                                            <div class="au-progress__bar">
                                                <div class="au-progress__inner js-progressbar-simple" role="progressbar" data-transitiongoal="90">
                                                    <span class="au-progress__value js-value"></span>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="au-progress">
                                            <span class="au-progress__title">HTML5/CSS3</span>
                                            <div class="au-progress__bar">
                                                <div class="au-progress__inner js-progressbar-simple" role="progressbar" data-transitiongoal="85">
                                                    <span class="au-progress__value js-value"></span>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="au-progress">
                                            <span class="au-progress__title">WordPress</span>
                                            <div class="au-progress__bar">
                                                <div class="au-progress__inner js-progressbar-simple" role="progressbar" data-transitiongoal="95">
                                                    <span class="au-progress__value js-value"></span>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="au-progress">
                                            <span class="au-progress__title">Support</span>
                                            <div class="au-progress__bar">
                                                <div class="au-progress__inner js-progressbar-simple" role="progressbar" data-transitiongoal="95">
                                                    <span class="au-progress__value js-value"></span>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <!-- END TASK PROGRESS-->
                            </div>
                        </div>
                    </div>
                </div>
            </section>

            <section>
                <div class="section__content section__content--p30">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-xl-6">
                                <!-- USER DATA-->
                                <div class="user-data m-b-40">
                                    <h3 class="title-3 m-b-30">
                                        <i class="zmdi zmdi-account-calendar"></i>user data</h3>
                                    <div class="filters m-b-45">
                                        <div class="rs-select2--dark rs-select2--md m-r-10 rs-select2--border">
                                            <select class="js-select2" name="property">
                                                <option selected="selected">All Properties</option>
                                                <option value="">Products</option>
                                                <option value="">Services</option>
                                            </select>
                                            <div class="dropDownSelect2"></div>
                                        </div>
                                        <div class="rs-select2--dark rs-select2--sm rs-select2--border">
                                            <select class="js-select2 au-select-dark" name="time">
                                                <option selected="selected">All Time</option>
                                                <option value="">By Month</option>
                                                <option value="">By Day</option>
                                            </select>
                                            <div class="dropDownSelect2"></div>
                                        </div>
                                    </div>
                                    <div class="table-responsive table-data">
                                        <table class="table">
                                            <thead>
                                                <tr>
                                                    <td>
                                                        <label class="au-checkbox">
                                                            <input type="checkbox">
                                                            <span class="au-checkmark"></span>
                                                        </label>
                                                    </td>
                                                    <td>name</td>
                                                    <td>role</td>
                                                    <td>type</td>
                                                    <td></td>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <tr>
                                                    <td>
                                                        <label class="au-checkbox">
                                                            <input type="checkbox">
                                                            <span class="au-checkmark"></span>
                                                        </label>
                                                    </td>
                                                    <td>
                                                        <div class="table-data__info">
                                                            <h6>lori lynch</h6>
                                                            <span>
                                                                <a href="#">johndoe@gmail.com</a>
                                                            </span>
                                                        </div>
                                                    </td>
                                                    <td>
                                                        <span class="role admin">admin</span>
                                                    </td>
                                                    <td>
                                                        <div class="rs-select2--trans rs-select2--sm">
                                                            <select class="js-select2" name="property">
                                                                <option selected="selected">Full Control</option>
                                                                <option value="">Post</option>
                                                                <option value="">Watch</option>
                                                            </select>
                                                            <div class="dropDownSelect2"></div>
                                                        </div>
                                                    </td>
                                                    <td>
                                                        <span class="more">
                                                            <i class="zmdi zmdi-more"></i>
                                                        </span>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        <label class="au-checkbox">
                                                            <input type="checkbox" checked="checked">
                                                            <span class="au-checkmark"></span>
                                                        </label>
                                                    </td>
                                                    <td>
                                                        <div class="table-data__info">
                                                            <h6>lori lynch</h6>
                                                            <span>
                                                                <a href="#">johndoe@gmail.com</a>
                                                            </span>
                                                        </div>
                                                    </td>
                                                    <td>
                                                        <span class="role user">user</span>
                                                    </td>
                                                    <td>
                                                        <div class="rs-select2--trans rs-select2--sm">
                                                            <select class="js-select2" name="property">
                                                                <option value="">Full Control</option>
                                                                <option value="" selected="selected">Post</option>
                                                                <option value="">Watch</option>
                                                            </select>
                                                            <div class="dropDownSelect2"></div>
                                                        </div>
                                                    </td>
                                                    <td>
                                                        <span class="more">
                                                            <i class="zmdi zmdi-more"></i>
                                                        </span>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        <label class="au-checkbox">
                                                            <input type="checkbox">
                                                            <span class="au-checkmark"></span>
                                                        </label>
                                                    </td>
                                                    <td>
                                                        <div class="table-data__info">
                                                            <h6>lori lynch</h6>
                                                            <span>
                                                                <a href="#">johndoe@gmail.com</a>
                                                            </span>
                                                        </div>
                                                    </td>
                                                    <td>
                                                        <span class="role user">user</span>
                                                    </td>
                                                    <td>
                                                        <div class="rs-select2--trans rs-select2--sm">
                                                            <select class="js-select2" name="property">
                                                                <option value="">Full Control</option>
                                                                <option value="" selected="selected">Post</option>
                                                                <option value="">Watch</option>
                                                            </select>
                                                            <div class="dropDownSelect2"></div>
                                                        </div>
                                                    </td>
                                                    <td>
                                                        <span class="more">
                                                            <i class="zmdi zmdi-more"></i>
                                                        </span>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        <label class="au-checkbox">
                                                            <input type="checkbox">
                                                            <span class="au-checkmark"></span>
                                                        </label>
                                                    </td>
                                                    <td>
                                                        <div class="table-data__info">
                                                            <h6>lori lynch</h6>
                                                            <span>
                                                                <a href="#">johndoe@gmail.com</a>
                                                            </span>
                                                        </div>
                                                    </td>
                                                    <td>
                                                        <span class="role member">member</span>
                                                    </td>
                                                    <td>
                                                        <div class="rs-select2--trans rs-select2--sm">
                                                            <select class="js-select2" name="property">
                                                                <option selected="selected">Full Control</option>
                                                                <option value="">Post</option>
                                                                <option value="">Watch</option>
                                                            </select>
                                                            <div class="dropDownSelect2"></div>
                                                        </div>
                                                    </td>
                                                    <td>
                                                        <span class="more">
                                                            <i class="zmdi zmdi-more"></i>
                                                        </span>
                                                    </td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                    <div class="user-data__footer">
                                        <button class="au-btn au-btn-load">load more</button>
                                    </div>
                                </div>
                                <!-- END USER DATA-->
                            </div>
                            <div class="col-xl-6">
                                <!-- MAP DATA-->
                                <div class="map-data m-b-40">
                                    <h3 class="title-3 m-b-30">
                                        <i class="zmdi zmdi-map"></i>map data</h3>
                                    <div class="filters">
                                        <div class="rs-select2--dark rs-select2--md m-r-10 rs-select2--border">
                                            <select class="js-select2" name="property">
                                                <option selected="selected">All Worldwide</option>
                                                <option value="">Products</option>
                                                <option value="">Services</option>
                                            </select>
                                            <div class="dropDownSelect2"></div>
                                        </div>
                                        <div class="rs-select2--dark rs-select2--sm rs-select2--border">
                                            <select class="js-select2 au-select-dark" name="time">
                                                <option selected="selected">All Time</option>
                                                <option value="">By Month</option>
                                                <option value="">By Day</option>
                                            </select>
                                            <div class="dropDownSelect2"></div>
                                        </div>
                                    </div>
                                    <div class="map-wrap m-t-45 m-b-80">
                                        <div id="vmap" style="height: 284px;"></div>
                                    </div>
                                    <div class="table-wrap">
                                        <div class="table-responsive table-style1">
                                            <table class="table">
                                                <tbody>
                                                    <tr>
                                                        <td>United States</td>
                                                        <td>$119,366.96</td>
                                                    </tr>
                                                    <tr>
                                                        <td>Australia</td>
                                                        <td>$70,261.65</td>
                                                    </tr>
                                                    <tr>
                                                        <td>United Kingdom</td>
                                                        <td>$46,399.22</td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                        <div class="table-responsive table-style1">
                                            <table class="table">
                                                <tbody>
                                                    <tr>
                                                        <td>Germany</td>
                                                        <td>$20,366.96</td>
                                                    </tr>
                                                    <tr>
                                                        <td>France</td>
                                                        <td>$10,366.96</td>
                                                    </tr>
                                                    <tr>
                                                        <td>Russian</td>
                                                        <td>$5,366.96</td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                                <!-- END MAP DATA-->
                            </div>
                        </div>
                    </div>
                </div>
            </section>

            <?php include "footer.php"?>
            
            <!-- END PAGE CONTAINER-->
        </div>
		
    </div>
	<?php include "footerLink.php"?>

</body>

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