<?php

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL);

require_once __DIR__ . '/../../includes/auth.php';
requireLogin();
requirePermission('finance.manage');

require_once __DIR__ . '/../../config/database.php';

$company_id = $_SESSION['company_id'] ?? 0;

/* ===============================
TOTAL REVENUE
================================ */

$stmt = $pdo->prepare("
SELECT SUM(total) as total_revenue
FROM invoices
WHERE company_id = ?
");

$stmt->execute([$company_id]);

$totalRevenue = $stmt->fetch(PDO::FETCH_ASSOC)['total_revenue'] ?? 0;


/* ===============================
TOTAL PAID
================================ */

$stmt = $pdo->prepare("
SELECT SUM(paid_total) as paid_total
FROM invoices
WHERE company_id = ?
");

$stmt->execute([$company_id]);

$paidTotal = $stmt->fetch(PDO::FETCH_ASSOC)['paid_total'] ?? 0;


/* ===============================
OUTSTANDING BALANCE
================================ */

$stmt = $pdo->prepare("
SELECT SUM(balance) as outstanding
FROM invoices
WHERE company_id = ?
AND balance > 0
");

$stmt->execute([$company_id]);

$outstandingBalance = $stmt->fetch(PDO::FETCH_ASSOC)['outstanding'] ?? 0;


/* ===============================
TOTAL INVOICE COUNT
================================ */

$stmt = $pdo->prepare("
SELECT COUNT(*) as invoice_count
FROM invoices
WHERE company_id = ?
");

$stmt->execute([$company_id]);

$invoiceCount = $stmt->fetch(PDO::FETCH_ASSOC)['invoice_count'] ?? 0;


/* ===============================
TOTAL QUOTATIONS
================================ */

$stmt = $pdo->prepare("
SELECT COUNT(*) as total_quotes
FROM quotations
WHERE company_id = ?
");

$stmt->execute([$company_id]);

$totalQuotes = $stmt->fetch(PDO::FETCH_ASSOC)['total_quotes'] ?? 0;


/* ===============================
RECENT INVOICES
================================ */

$stmt = $pdo->prepare("
SELECT id, invoice_number, total, payment_status, created_at
FROM invoices
WHERE company_id = ?
ORDER BY created_at DESC
LIMIT 5
");

$stmt->execute([$company_id]);

$recentInvoices = $stmt->fetchAll(PDO::FETCH_ASSOC);


/* ===============================
LAYOUT
================================ */

$breadcrumb = "Finance Dashboard";

require_once __DIR__.'/../../includes/layout.php';

?>

<div class="container">

<div class="page-header">
<h2>Finance Dashboard</h2>
<p class="page-subtitle">
Financial overview for your company.
</p>
</div>


<div class="grid-cards">

<div class="stat-card">
<h3>Total Revenue</h3>
<p class="stat-number">
R <?= number_format((float)$totalRevenue,2) ?>
</p>
</div>


<div class="stat-card">
<h3>Total Paid</h3>
<p class="stat-number">
R <?= number_format((float)$paidTotal,2) ?>
</p>
</div>


<div class="stat-card">
<h3>Outstanding Balance</h3>
<p class="stat-number">
R <?= number_format((float)$outstandingBalance,2) ?>
</p>
</div>


<div class="stat-card">
<h3>Total Invoices</h3>
<p class="stat-number">
<?= (int)$invoiceCount ?>
</p>
</div>


<div class="stat-card">
<h3>Total Quotations</h3>
<p class="stat-number">
<?= (int)$totalQuotes ?>
</p>
</div>

</div>


<br>


<div class="table-wrap">

<h3>Recent Invoices</h3>

<table>

<thead>
<tr>
<th>Invoice</th>
<th>Total</th>
<th>Status</th>
<th>Date</th>
<th>Action</th>
</tr>
</thead>

<tbody>

<?php if(!$recentInvoices): ?>

<tr>
<td colspan="5">No invoices found.</td>
</tr>

<?php else: ?>

<?php foreach($recentInvoices as $inv): ?>

<tr>

<td>

<?= htmlspecialchars(
$inv['invoice_number']
?: 'INV-'.str_pad($inv['id'],5,"0",STR_PAD_LEFT)
) ?>

</td>

<td>
R <?= number_format((float)$inv['total'],2) ?>
</td>

<td>
<?= htmlspecialchars($inv['payment_status'] ?? 'UNPAID') ?>
</td>

<td>
<?= date("d M Y",strtotime($inv['created_at'])) ?>
</td>

<td>

<a class="btn-small view"
href="/core/modules/finance/invoices/view.php?id=<?= (int)$inv['id'] ?>">
View
</a>

</td>

</tr>

<?php endforeach; ?>

<?php endif; ?>

</tbody>

</table>

</div>

</div>

<?php require_once __DIR__.'/../../includes/layout_footer.php'; ?>