<?php

require_once (dirname(__FILE__) . '/php/config.php');

if (extension_loaded('intl'))
{
	$nf = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
}

$reportStartDate = $utilities->validateData($_GET, 'startDate');
$reportEndDate = $utilities->validateData($_GET, 'endDate');

if (0 == strlen($reportStartDate))
{
	// $reportStartDate = DateTime::createFromFormat('m/d/Y', date('m/d/Y', strtotime(date('o-\\WW'))));
	$reportStartDate = DateTime::createFromFormat('m/d/Y', '07/06/2026');
}
else
{
	$reportStartDate = DateTime::createFromFormat('m/d/Y', $reportStartDate);
}

if (0 == strlen($reportEndDate))
{
	// $reportEndDate = DateTime::createFromFormat('m/d/Y', date('m/d/Y', strtotime(date('o-\\WW', strtotime('next week'))) - (60 * 60 * 24)));
	$reportEndDate = DateTime::createFromFormat('m/d/Y', '07/12/2026');
}
else
{
	$reportEndDate = DateTime::createFromFormat('m/d/Y', $reportEndDate);
}

$priorWeekStart = date('m/d/Y', strtotime('-1 week', $reportStartDate->format('U')));
$priorWeekEnd = date('m/d/Y', strtotime('-1 day', $reportStartDate->format('U')));

$nextWeekStart = date('m/d/Y', strtotime('+1 week', $reportStartDate->format('U')));
$nextWeekEnd = date('m/d/Y', strtotime('+1 week', $reportEndDate->format('U')));

$data = array();
$submissionData = array();
$propertyData = null;
$file = dirname(__FILE__) . '/demo_data/properties.json';

if (file_exists($file))
{
	$data = json_decode(file_get_contents($file), true);
}

?>

<!doctype html>
<html lang="en">
<head>
    <title>Weekly Status Report</title>

	<?php include_once ('includes/head.php'); ?>
</head>
<body>

	<?php include_once ('includes/nav.php'); ?>

	<div class="pageContent">
		<div class="container-fluid">
			<div class="row mt-3">
				<h3 class="text-center">
					<span class="d-block d-sm-inline-block">Weekly Status Report:</span>
					<span class="d-block d-sm-inline-block"><?php echo $reportStartDate->format('m/d/Y'); ?> - <?php echo $reportEndDate->format('m/d/Y'); ?></span>
				</h3>
				<div class="toolbar justify-content-between row align-items-center">
					<div class="col-sm-auto text-center text-sm-start">
						<a href="weeklyReport.php?startDate=<?php echo $priorWeekStart; ?>&endDate=<?php echo $priorWeekEnd; ?>">&laquo; <?php echo $priorWeekStart; ?> - <?php echo $priorWeekEnd; ?></a>
					</div>
					<div class="col-sm text-center mb-3 mb-sm-0 overflow-y-hidden">
					</div>
					<div class="col-sm-auto text-center text-sm-end">
						<a href="weeklyReport.php?startDate=<?php echo $nextWeekStart; ?>&endDate=<?php echo $nextWeekEnd; ?>"><?php echo $nextWeekStart; ?> - <?php echo $nextWeekEnd; ?> &raquo;</a>
					</div>
				</div>
			</div>
			<div class="row py-3 d-print-none">
				<div class="col text-center">
					<a class="btn btn-info" href="executiveSummary.php?startDate=<?php echo $reportStartDate->format('m/d/Y'); ?>">Executive Summary</a>
					<a class="btn btn-info" href="aiPrompts.php?startDate=<?php echo $reportStartDate->format('m/d/Y'); ?>">AI Analysis</a>
				</div>
			</div>

			<div class="row py-3">
				<div class="table-responsive">
					<table class="table table-bordered table-striped table-sm bg-light" id="tablePropertyStatus">
						<thead>
							<tr class="text-center">
								<th scope="col" class="d-print-none text-center" style="" data-mdb-sort="false">District</th>
								<th scope="col" class="d-print-none text-center" style="" data-mdb-sort="false">Property</th>
								<th scope="col" class="d-print-none text-center" style="" data-mdb-sort="false"># of Units</th>
								<th scope="col" class="d-print-none text-center" style="" data-mdb-sort="false"># of Vacant Units</th>
								<th scope="col" class="d-print-none text-center" style="" data-mdb-sort="false"># of RA Units</th>
								<th scope="col" class="d-print-none text-center" style="" data-mdb-sort="false">Open RA</th>
								<th scope="col" class="d-print-none text-center" style="" data-mdb-sort="false"># on the Waiting List</th>
								<th scope="col" class="d-print-none text-center" style="" data-mdb-sort="false">Unit Status</th>
								<th scope="col" class="d-print-none text-center" style="" data-mdb-sort="false">Rent Loss Vacated</th>
								<th scope="col" class="d-print-none text-center" style="min-width: 200px;" data-mdb-sort="false">Delinquency</th>
								<th scope="col" class="d-print-none text-center" style="" data-mdb-sort="false">Damages</th>
								<th scope="col" class="d-print-none text-center" style="" data-mdb-sort="false">Application In Process</th>
								<th scope="col" class="d-print-none text-center" style="min-width: 200px;" data-mdb-sort="false">Evictions</th>
								<th scope="col" class="d-print-none text-center" style="" data-mdb-sort="false">Move Out Notices</th>
								<th scope="col" class="d-print-none text-center" style="" data-mdb-sort="false">Recert Time</th>
								<th scope="col" class="d-print-none text-center" style="" data-mdb-sort="false">Recert Date</th>
								<th scope="col" class="d-print-none text-center" style="" data-mdb-sort="false">Recerts to Complete</th>
							</tr>
						</thead>
						<tbody>
						<?php
							$submissionFiles = glob(dirname(__FILE__) . '/demo_data/submissions/' . $reportStartDate->format('Y-m-d') . '_*' . '.json');
							natsort($submissionFiles);

							if (0 < count($submissionFiles))
							{
								foreach ($submissionFiles as $item)
								{
									$submissionData = json_decode(file_get_contents($item), true);

									if ($submissionData)
									{
										if (isset($data['properties']))
										{
											foreach ($data['properties'] as $item)
											{
												if ($item['propertyId'] == $submissionData['property']['propertyId'])
												{
													$propertyData = $item;
													break;
												}
											}
										}
						?>
							<tr>
								<td><?php echo $propertyData['region']; ?></td>
								<td><?php echo $submissionData['property']['name']; ?></td>
								<td><?php echo $propertyData['totalUnits']; ?></td>
								<td><?php echo $submissionData['propertyInformation']['vacantUnits']; ?></td>
								<td></td>
								<td></td>
								<td><?php echo $submissionData['propertyInformation']['waitingListCount']; ?></td>
								<td>
								<?php
									if (0 < count($submissionData['vacantUnits']))
									{
										$numReady = 0;
										$numNotReady = 0;

										foreach ($submissionData['vacantUnits'] as $key => $item)
										{
											if ($item['readyToRent'])
											{
												++$numReady;
											}
											else
											{
												++$numNotReady;
											}
										}
								?>
									<h6 class="text-lg-center">Vacant Units Status</h6>
									<ul>
										<li><?php echo $numNotReady; ?> - No</li>
										<li><?php echo $numReady; ?> - Yes</li>
									</ul>
									<hr />
									<ul>
									<?php
										foreach ($submissionData['vacantUnits'] as $key => $item)
										{
											if (!$item['readyToRent'])
											{
									?>
										<li><?php echo $item['unitNumber']; ?> - <?php echo $item['makeReadyNeeds']; ?></li>
									<?php
											}
										}
									?>
									</ul>
								<?php
									}
								?>
								</td>
								<td>
								<?php
									if (0 < count($submissionData['vacantUnits']))
									{
								?>
									<ul>
									<?php
										foreach ($submissionData['vacantUnits'] as $key => $item)
										{
									?>
										<li><?php echo $item['unitNumber']; ?> - <?php echo date('m/d/Y', strtotime($item['dateVacant'])); ?> - <?php echo $nf->formatCurrency($item['rentLoss'], 'USD'); ?></li>
									<?php
										}
									?>
									</ul>
								<?php
									}
								?>
								</td>
								<td>
								<?php
									if (0 < count($submissionData['delinquentRent']))
									{
								?>
									<ul>
									<?php
										foreach ($submissionData['delinquentRent'] as $key => $item)
										{
									?>
										<li>
											<?php echo $item['tenantName']; ?> - <?php echo $item['unitNumber']; ?> -
											<?php
												if (0 < $item['lateRentAmount'])
												{
													echo $nf->formatCurrency($item['lateRentAmount'], 'USD') . ' (R) - ';
												}

												if (0 < $item['lateFeeAmount'])
												{
													echo $nf->formatCurrency($item['lateFeeAmount'], 'USD') . ' (F) - ';
												}

												if (0 < $item['damagesAmount'])
												{
													echo $nf->formatCurrency($item['damagesAmount'], 'USD') . ' (D) - ';
												}
											?>
										</li>
									<?php
										}
									?>
									</ul>
								<?php
									}
								?>
								</td>
								<td>
								<?php
									if (0 < count($submissionData['delinquentRent']))
									{
								?>
									<ul>
									<?php
										foreach ($submissionData['delinquentRent'] as $key => $item)
										{
									?>
										<li>
											<?php echo $item['tenantName']; ?> - <?php echo $item['unitNumber']; ?> -
											<?php
												if (0 < $item['damagesAmount'])
												{
													echo $nf->formatCurrency($item['damagesAmount'], 'USD') . ' - ';

													if ($item['damagesPaymentPlan'])
													{
														echo 'Yes';
													}
													else
													{
														echo 'No';
													}
												}
											?>
										</li>
									<?php
										}
									?>
									</ul>
								<?php
									}
								?>
								</td>
								<td>
								<?php
									if (0 < count($submissionData['applications']))
									{
								?>
									<ul>
									<?php
										foreach ($submissionData['applications'] as $key => $item)
										{
									?>
										<li>
											<?php echo $item['applicantName']; ?> - <?php echo $item['status']; ?> - <?php echo $item['unitNumber']; ?>
										</li>
									<?php
										}
									?>
									</ul>
								<?php
									}
								?>
								</td>
								<td></td>
								<td>
								<?php
									if (0 < count($submissionData['moveOutNotices']))
									{
								?>
									<ul>
									<?php
										foreach ($submissionData['moveOutNotices'] as $key => $item)
										{
									?>
										<li>
											<?php echo $item['tenantName']; ?> - <?php echo $item['unitNumber']; ?> - <?php echo date('m/d/Y', strtotime($item['noticeGivenDate'])); ?> - <?php echo date('m/d/Y', strtotime($item['moveOutDate'])); ?> - <?php echo $item['reason']; ?>
										</li>
									<?php
										}
									?>
									</ul>
								<?php
									}
								?>
								</td>
								<td>
								<?php
									if ($submissionData['propertyInformation']['recertTime'])
									{
										echo 'Yes';
									}
									else
									{
										echo 'No';
									}
								?>
								</td>
								<td></td>
								<td>
								<?php
									if ($submissionData['propertyInformation']['recertTime'])
									{
										echo $submissionData['propertyInformation']['recertsToComplete'];
									}
								?>
								</td>
							</tr>
						<?php
									}
								}
							}
						?>
						</tbody>
					</table>
				</div>
			</div>
        </div>
	</div>

	<?php include_once (dirname(__FILE__) . '/includes/footer.php'); ?>

	<?php include_once (dirname(__FILE__) . '/includes/scripts.php'); ?>
	<?php include_once (dirname(__FILE__) . '/includes/analytics.php'); ?>

</body>
</html>
