<?php
include('db_connection.php');

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
error_reporting(E_ALL);
ini_set('display_errors', 1);

/* =========================
   SUMMARY
========================= */
$total_paid = $conn->query("SELECT SUM(net_salary) AS total FROM payrolls")->fetch_assoc()['total'] ?? 0;
$total_count = $conn->query("SELECT COUNT(*) AS total FROM payrolls")->fetch_assoc()['total'] ?? 0;

/* =========================
   FETCH PAYROLL
========================= */
$sql = "
SELECT 
    p.*,
    t.name AS teacher_name
FROM payrolls p
JOIN teachers t ON t.teacher_id = p.teacher_id
ORDER BY p.pay_year DESC, p.pay_month DESC
";

$res = $conn->query($sql);
?>

<!DOCTYPE html>
<html>
<head>
<title>Payroll Dashboard | ERP</title>

<style>
body { font-family: Arial; background:#eef2f7; margin:0; }

.header {
    background:#1e293b;
    color:white;
    text-align:center;
    padding:18px;
}

.container { width:90%; margin:20px auto; }

.summary {
    display:flex;
    gap:20px;
    margin-bottom:20px;
}

.box {
    flex:1;
    background:#1e293b;
    color:white;
    padding:15px;
    border-radius:10px;
    text-align:center;
}

.card {
    background:white;
    padding:20px;
    border-radius:10px;
}

table {
    width:100%;
    border-collapse:collapse;
}

th {
    background:#1e293b;
    color:white;
    padding:10px;
    font-size:13px;
}

td {
    padding:10px;
    border-bottom:1px solid #ddd;
    font-size:13px;
}

a {
    background:#2563eb;
    color:white;
    padding:6px 10px;
    text-decoration:none;
    border-radius:5px;
}

a:hover { background:#1d4ed8; }

.badge {
    font-size:12px;
    padding:4px 8px;
    border-radius:5px;
    color:white;
}

.good { background:green; }
.warn { background:orange; }
.bad { background:red; }
</style>
</head>

<body>

<div class="header">
    <h2>AGID EDUCATIONAL INSTITUTE</h2>
    <p>Payroll Management Dashboard (ERP)</p>
</div>

<div class="container">

<!-- SUMMARY -->
<div class="summary">
    <div class="box">
        Total Paid<br>
        <b>₦<?= number_format($total_paid,2) ?></b>
    </div>

    <div class="box">
        Payroll Records<br>
        <b><?= $total_count ?></b>
    </div>
</div>

<div class="card">

<table>
<tr>
    <th>Teacher</th>
    <th>Month</th>
    <th>Year</th>
    <th>Gross</th>
    <th>Absent Deduction</th>
    <th>Late Deduction</th>
    <th>Net Salary</th>
    <th>Status</th>
    <th>Action</th>
</tr>

<?php while ($row = $res->fetch_assoc()): ?>

<?php
$status = "GOOD";
$class = "good";

if ($row['warning_flag'] == 1) {
    $status = "WARNING";
    $class = "warn";
}
?>

<tr>
    <td><?= htmlspecialchars($row['teacher_name']) ?></td>
    <td><?= $row['pay_month'] ?></td>
    <td><?= $row['pay_year'] ?></td>

    <td>₦<?= number_format($row['basic_salary'] + $row['allowances'],2) ?></td>

    <td>₦<?= number_format($row['deduction'],2) ?></td>

    <td>₦<?= number_format($row['late_deduction'],2) ?></td>

    <td><b>₦<?= number_format($row['net_salary'],2) ?></b></td>

    <td>
        <span class="badge <?= $class ?>"><?= $status ?></span>
    </td>

    <td>
        <a href="generate_payslip.php?id=<?= $row['id'] ?>">Payslip</a>
    </td>
</tr>

<?php endwhile; ?>

</table>

</div>
</div>

</body>
</html>