<!DOCTYPE html>
<html lang="en">
<?php include 'includes/head.php'; ?>

<body>

	<div id="global-loader">
		<div class="whirly-loader"> </div>
	</div>


	<!-- Main Wrapper -->
	<div class="main-wrapper">

		<!-- Header -->
		<?php include './includes/navbar.php'; ?>
		<!-- /Header -->

		<?php $page = 'invoice-report';
		include './includes/sidebar.php'; ?>

		<div class="page-wrapper">
			<div class="content">
				<div class="row">
					<?php
					require_once 'php/databaseConnection.php';
					$mysqli = db_connect();

					$totalQuery = "
    SELECT 
        COALESCE(SUM(CAST(netTotal AS DECIMAL(10,2))), 0) AS totalNetAmount,
        COALESCE(SUM(CAST(totalPaid AS DECIMAL(10,2))), 0) AS totalPaidAmount,
        COALESCE(SUM(CAST(remainingAmount AS DECIMAL(10,2))), 0) AS totalRemainingAmount
    FROM jobCard
";

					$result = $mysqli->query($totalQuery);

					$totalNet = $totalPaid = $totalRemaining = 0;

					if ($result && $row = $result->fetch_assoc()) {
						$totalNet = is_numeric($row['totalNetAmount']) ? (float) $row['totalNetAmount'] : 0;
						$totalPaid = is_numeric($row['totalPaidAmount']) ? (float) $row['totalPaidAmount'] : 0;
						$totalRemaining = is_numeric($row['totalRemainingAmount']) ? (float) $row['totalRemainingAmount'] : 0;
					}
					?>
					<!-- Total Amount -->
					<div class="col-xl-3 col-sm-6 col-12 d-flex">
						<div class="dash-widget w-100">
							<div class="dash-widgetimg">
								<span><img src="images/dash1.svg" alt="img"></span>
							</div>
							<div class="dash-widgetcontent">
								<h5>Rs <span><?= number_format($totalNet, 2) ?></span></h5>
								<h6>Total Amount</h6>
							</div>
						</div>
					</div>
					<!-- Total Paid -->
					<div class="col-xl-3 col-sm-6 col-12 d-flex">
						<div class="dash-widget dash1 w-100">
							<div class="dash-widgetimg">
								<span><img src="images/dash2.svg" alt="img"></span>
							</div>
							<div class="dash-widgetcontent">
								<h5>Rs <span><?= number_format($totalPaid, 2) ?></span></h5>
								<h6>Total Paid</h6>
							</div>
						</div>
					</div>
					<!-- Total Unpaid -->
					<div class="col-xl-3 col-sm-6 col-12 d-flex">
						<div class="dash-widget dash2 w-100">
							<div class="dash-widgetimg">
								<span><img src="images/dash3.svg" alt="img"></span>
							</div>
							<div class="dash-widgetcontent">
								<h5>Rs <span><?= number_format($totalRemaining, 2) ?></span></h5>
								<h6>Total Unpaid</h6>
							</div>
						</div>
					</div>
					<div class="col-xl-3 col-sm-6 col-12 d-flex">
						<div class="dash-widget dash3 w-100">
							<div class="dash-widgetimg">
								<span><img src="images/dash4.svg" alt="img"></span>
							</div>
							<div class="dash-widgetcontent">
								<h5>$<span class="counters" data-count="40000.00">$400.00</span></h5>
								<h6>Overdue</h6>
							</div>
						</div>
					</div>
				</div>
				<div class="page-header">
					<div class="page-title me-auto">
						<h4>Invoice Report</h4>
						<h6>Manage your Invoice Report</h6>
					</div>
					<ul class="table-top-head low-stock-top-head">
						<li>
							<div class="status-toggle d-flex justify-content-between align-items-center">
								<input type="checkbox" id="user2" class="check" checked="">
								<label for="user2" class="checktoggle">checkbox</label>
								Notify
							</div>
						</li>
						<li>
							<a href="" class="btn btn-secondary" data-bs-toggle="modal" data-bs-target="#send-email"><i data-feather="mail" class="feather-mail"></i>Send Email</a>
						</li>
						<li>
							<a data-bs-toggle="tooltip" data-bs-placement="top" title="Pdf"><img src="images/pdf.svg" alt="img"></a>
						</li>
						<li>
							<a data-bs-toggle="tooltip" data-bs-placement="top" title="Excel"><img src="images/excel.svg" alt="img"></a>
						</li>
						<li>
							<a data-bs-toggle="tooltip" data-bs-placement="top" title="Print"><i data-feather="printer" class="feather-rotate-ccw"></i></a>
						</li>
						<li>
							<a data-bs-toggle="tooltip" data-bs-placement="top" title="Refresh"><i data-feather="rotate-ccw" class="feather-rotate-ccw"></i></a>
						</li>
						<li>
							<a data-bs-toggle="tooltip" data-bs-placement="top" title="Collapse" id="collapse-header"><i data-feather="chevron-up" class="feather-chevron-up"></i></a>
						</li>
					</ul>
				</div>
				<div class="table-tab">
					<ul class="nav nav-pills" id="pills-tab" role="tablist">
						<li class="nav-item" role="presentation">
							<button class="nav-link active" id="pills-home-tab" data-bs-toggle="pill" data-bs-target="#pills-home" type="button" role="tab" aria-controls="pills-home" aria-selected="true">Check Invoice</button>
						</li>
						<li class="nav-item" role="presentation">
							<button class="nav-link" id="pills-profile-tab" data-bs-toggle="pill" data-bs-target="#pills-profile" type="button" role="tab" aria-controls="pills-profile" aria-selected="false">Check Proforma</button>
						</li>

					</ul>
					<div class="tab-content" id="pills-tabContent">
						<div class="tab-pane fade show active" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab">
							<!-- /product list -->
							<div class="card table-list-card">
								<div class="card-body">
									<div class="table-top">
										<div class="search-set">
											<div class="search-input">
												<a href="" class="btn btn-searchset"><i data-feather="search" class="feather-search"></i></a>
											</div>
										</div>
										<div class="search-path">
											<a class="btn btn-filter" id="filter_search">
												<i data-feather="filter" class="filter-icon"></i>
												<span><img src="images/closes.svg" alt="img"></span>
											</a>
										</div>
										<div class="form-sort">
											<i data-feather="sliders" class="info-img"></i>
											<select class="select">
												<option>Sort by Date</option>
												<option>Newest</option>
												<option>Oldest</option>
											</select>
										</div>
									</div>
									<div class="table-responsive">
										<table class="table  datanew">
											<thead>
												<tr>
													<th class="no-sort">
														<label class="checkboxs">
															<input type="checkbox" id="select-all">
															<span class="checkmarks"></span>
														</label>
													</th>
													<th>Job No.</th>
													<th>Invoice No.</th>
													<th>Invoice Date</th>
													<th>Customer</th>
													<th>Amount</th>
													<th>Paid</th>
													<th>Amount Due</th>
													<th>Status</th>
												</tr>
											</thead>
											<tbody>
												<?php
												require_once 'php/databaseConnection.php';
												$mysqli = db_connect();

												$query = "
    SELECT 
        ir.invoiceNumber,
        jc.jobDate,
        ir.jobNumber,
        ir.createdAt,
        jc.customerName,
        jc.netTotal,
        jc.totalPaid,
        jc.remainingAmount,
        jc.paymentStatus
    FROM 
        invoiceRegistry AS ir
    JOIN 
        jobCard AS jc ON ir.jobNumber = jc.jobNumber
    ORDER BY 
        ir.createdAt DESC
";

												$result = $mysqli->query($query);
												if ($result && $result->num_rows > 0) {
													while ($row = $result->fetch_assoc()) {
														$statusBadge = '';
														$statusClass = '';
														switch (strtolower($row['paymentStatus'])) {
															case 'paid':
																$statusClass = 'badge-success';
																break;
															case 'partial':
																$statusClass = 'badge-warning';
																break;
															case 'unpaid':
															default:
																$statusClass = 'badge-danger';
																break;
														}

														// format amount
														$netTotal = 'Rs ' . number_format((float)$row['netTotal'], 2);
														$totalPaid = 'Rs ' . number_format((float)$row['totalPaid'], 2);
														$remaining = 'Rs ' . number_format((float)$row['remainingAmount'], 2);
														$jobDate = date('Y/m/d', strtotime($row['jobDate'])); // format like 2025/02/25

														echo "<tr>
			<td>
				<label class='checkboxs'>
					<input type='checkbox'>
					<span class='checkmarks'></span>
				</label>
			</td>
			 <td><a href='invoice-printed.php?jobNumber=" . urlencode($row['jobNumber']) . "&invoiceNumber=" . urlencode($row['invoiceNumber']) . "' class='link-primary'>" . $row['jobNumber'] . "</a></td>
			<td>{$row['invoiceNumber']}</td>
			<td>{$jobDate}</td>
			<td>{$row['customerName']}</td>
			<td>{$netTotal}</td>
			<td>{$totalPaid}</td>
			<td>{$remaining}</td>
			<td><span class='badge {$statusClass} d-inline-flex align-items-center'>
				<i class='ti ti-circle-filled fs-5 me-1'></i>" . ucfirst($row['paymentStatus']) . "</span></td>
		</tr>";
													}
												} else {
													echo "<tr><td colspan='9' class='text-center'>No invoice records found.</td></tr>";
												}
												?>
											</tbody>

										</table>
									</div>
								</div>
							</div>
							<!-- /product list -->
						</div>
						<div class="tab-pane fade" id="pills-profile" role="tabpanel" aria-labelledby="pills-profile-tab">
							<!-- /product list -->
							<div class="card table-list-card">
								<div class="card-body">
									<div class="table-top">
										<div class="search-set">
											<div class="search-input">
												<a href="" class="btn btn-searchset"><i data-feather="search" class="feather-search"></i></a>
											</div>
										</div>
										<div class="search-path">
											<a class="btn btn-filter" id="filter_search1">
												<i data-feather="filter" class="filter-icon"></i>
												<span><img src="images/closes.svg" alt="img"></span>
											</a>
										</div>
										<div class="form-sort">
											<i data-feather="sliders" class="info-img"></i>
											<select class="select">
												<option>Sort by Date</option>
												<option>Newest</option>
												<option>Oldest</option>
											</select>
										</div>
									</div>
									<div class="table-responsive">
										<table class="table  datanew proforma-table">
											<thead>
												<tr>
													<th class="no-sort">
														<label class="checkboxs">
															<input type="checkbox" id="select-all2">
															<span class="checkmarks"></span>
														</label>
													</th>
													<th>Job No.</th>
													<th>Proforma No.</th>
													<th>Proforma Date</th>
													<th>Customer</th>
													<th>Amount</th>
													<th>Paid</th>
													<th>Amount Due</th>
													<th>Status</th>
												</tr>
											</thead>
											<tbody>
												<?php
												require_once 'php/databaseConnection.php';
												$mysqli = db_connect();

												$query = "
    SELECT 
        pr.proformaNumber,
        jc.jobDate,
        pr.jobNumber,
        pr.createdAt,
        jc.customerName,
        jc.netTotal,
        jc.totalPaid,
        jc.remainingAmount,
        jc.paymentStatus
    FROM 
        proformaRegistry AS pr
    JOIN 
        jobCard AS jc ON pr.jobNumber = jc.jobNumber
    ORDER BY 
        pr.createdAt DESC
";

												$result = $mysqli->query($query);
												if ($result && $result->num_rows > 0) {
													while ($row = $result->fetch_assoc()) {
														$statusBadge = '';
														$statusClass = '';
														switch (strtolower($row['paymentStatus'])) {
															case 'paid':
																$statusClass = 'badge-success';
																break;
															case 'partial':
																$statusClass = 'badge-warning';
																break;
															case 'unpaid':
															default:
																$statusClass = 'badge-danger';
																break;
														}

														// format amount
														$netTotal = 'Rs ' . number_format((float)$row['netTotal'], 2);
														$totalPaid = 'Rs ' . number_format((float)$row['totalPaid'], 2);
														$remaining = 'Rs ' . number_format((float)$row['remainingAmount'], 2);
														$jobDate = date('Y/m/d', strtotime($row['jobDate'])); // format like 2025/02/25

														echo "<tr>
	<td>
		<label class='checkboxs'>
			<input type='checkbox'>
			<span class='checkmarks'></span>
		</label>
	</td>
	<td><a href='proforma-printed.php?jobNumber=" . urlencode($row['jobNumber']) . "&proformaNumber=" . urlencode($row['proformaNumber']) . "' class='link-primary'>" . htmlspecialchars($row['jobNumber']) . "</a></td>
	<td>" . htmlspecialchars($row['proformaNumber']) . "</td>
	<td>" . htmlspecialchars($jobDate) . "</td>
	<td>" . htmlspecialchars($row['customerName']) . "</td>
	<td>" . htmlspecialchars($netTotal) . "</td>
	<td>" . htmlspecialchars($totalPaid) . "</td>
	<td>" . htmlspecialchars($remaining) . "</td>
	<td><span class='badge {$statusClass} d-inline-flex align-items-center'>
		<i class='ti ti-circle-filled fs-5 me-1'></i>" . ucfirst(htmlspecialchars($row['paymentStatus'])) . "</span></td>
</tr>";
													}
												} else {
													echo "<tr><td colspan='9' class='text-center'>No invoice records found.</td></tr>";
												}
												?>
											</tbody>
										</table>
									</div>
								</div>
							</div>
							<!-- /product list -->
						</div>

					</div>
				</div>

				<!-- Invoices -->
			</div>
		</div>
		<!-- /Invoices -->
	</div>
	<!-- /Main Wrapper -->
	<div class="customizer-links" id="setdata">
		<ul class="sticky-sidebar">
			<li class="sidebar-icons">
				<a href="#" class="navigation-add" data-bs-toggle="tooltip" data-bs-placement="left" data-bs-original-title="Theme">
					<i data-feather="settings" class="feather-five"></i>
				</a>
			</li>
		</ul>
	</div>


	<!-- jQuery -->
	<script src="js/jquery-3.7.1.min.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>

	<!-- Feather Icon JS -->
	<script src="js/feather.min.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>

	<!-- Slimscroll JS -->
	<script src="js/jquery.slimscroll.min.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>

	<!-- Datatable JS -->
	<script src="js/jquery.dataTables.min.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>
	<script src="js/dataTables.bootstrap5.min.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>

	<!-- Datetimepicker JS -->
	<script src="js/moment.min.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>
	<script src="js/bootstrap-datetimepicker.min.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>

	<!-- Daterangepikcer JS -->
	<script src="js/moment.min.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>
	<script src="js/daterangepicker.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>

	<!-- Bootstrap Core JS -->
	<script src="js/bootstrap.bundle.min.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>

	<!-- Summernote JS -->
	<script src="js/summernote-bs4.min.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>

	<!-- Select2 JS -->
	<script src="js/select2.min.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>

	<!-- Sweetalert 2 -->
	<script src="js/sweetalert2.all.min.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>
	<script src="js/sweetalerts.min.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>

	<!-- Custom JS -->
	<script src="js/script.js" type="d4e35c9ac780cf7a658c1972-text/javascript"></script>


	<script src="js/rocket-loader.min.js" data-cf-settings="d4e35c9ac780cf7a658c1972-|49" defer=""></script>

	<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
</body>

</html>