<?php
session_start();
require_once '../utils/errorhandler.php';
require_once '../utils/response.php';
require_once '../model/dbclass.php';
require_once '../model/model.php';
require_once '../model/Property.php';

if ($_SESSION['wuseremail'] === null || !isset($_SESSION['wuseremail'])) {
     exit(badRequest(204,'Invalid session data. Proceed to login'));
}

$property = new Property(); 

$status = (isset($_POST["status"]) && $_POST["status"] !== "") ? $_POST["status"] : "%";
$startdate = (isset($_POST["startdate"]) && $_POST["startdate"] !== "") ? $_POST["startdate"] : date('Y-m-d');
$enddate = (isset($_POST["enddate"]) && $_POST["enddate"] !== "") ? $_POST["enddate"] : date('Y-m-d');
$id = (isset($_POST["id"]) && intval($_POST["id"]) > 0) ? $_POST["id"] : 0;
$location = (isset($_POST["location"]) && $_POST["location"] !== "") ? $_POST["location"] : "%";
$propertyidno = (isset($_POST["propertyidno"]) && $_POST["propertyidno"] !== "") ? $_POST["propertyidno"] : "%";

if($id == 0){
    if($propertyidno !== "%"){
        $result = $property->executeByQuerySelector("SELECT * FROM rentedproperty WHERE status LIKE '$status' AND propertyid IN (SELECT id FROM property WHERE propertyidno = '$propertyidno') ORDER BY id DESC");
    }elseif($location !== "%"){
        $result = $property->executeByQuerySelector("SELECT * FROM rentedproperty WHERE status LIKE '$status' AND propertyid IN (SELECT id FROM property WHERE location LIKE '$location%') ORDER BY id DESC");
    }else{
        $result = $property->executeByQuerySelector("SELECT * FROM rentedproperty WHERE status LIKE '$status' AND DATE(paymentdate) BETWEEN '$startdate' AND '$enddate' ORDER BY id DESC");
    }
}else{
    $result = $property->executeByQuerySelector("SELECT * FROM rentedproperty WHERE id = $id");
}
if($result){
    $rentapropertyarray = array();
    foreach($result as $resulta){
        $resunits = $property->executeByQuerySelector("SELECT * FROM propertyunit WHERE id = " . $resulta["unitid"]);
        $restenant = $property->executeByQuerySelector("SELECT * FROM tenants WHERE id = " . $resulta["tenantid"]);
        $resproperty = $property->executeByQuerySelector("SELECT * FROM property WHERE id = " . $resulta["propertyid"]);
        $resrentalfees = $property->executeByQuerySelector("SELECT *,(SELECT feename FROM morefees WHERE morefees.id = rentalfees.feenameid) AS feename FROM rentalfees WHERE rentalid = " . $resulta["id"]);
        
        $rentapropertyarray[] = array(
            "rentdata" => $resulta,
            // Use firstname + lastname for tenant full name
            "tenant" => $restenant[0]["firstname"] . " " . $restenant[0]["lastname"],
            "property" => $resproperty[0]["propertyname"],
            "propertyidno" => $resproperty[0]["propertyidno"], 
            "unitname" => $resunits[0]["unitname"],
            "rentalfees" => $resrentalfees
            );
    }
	echo success($rentapropertyarray,200, "Successful","Successful");
}else{
	exit(badRequest(204,'There may be no record to match this search filters'));
}
?>
