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

/* ===============================
   FETCH TEACHERS
=============================== */
$teachers = [];
$stmt = $conn->prepare("SELECT teacher_id, name FROM teachers ORDER BY name ASC");
$stmt->execute();
$res = $stmt->get_result();

while ($row = $res->fetch_assoc()) {
    $teachers[] = $row;
}
$stmt->close();

/* ===============================
   GENERATE PAYROLL REF
=============================== */
$payroll_ref = "PAY-" . date("Y") . "-" . strtoupper(bin2hex(random_bytes(3)));
?>

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

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

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

.container { width:50%; margin:30px auto; }

.card {
    background:white;
    padding:25px;
    border-radius:10px;
    box-shadow:0 5px 20px rgba(0,0,0,0.1);
}

label { font-weight:bold; margin-top:10px; display:block; }

input, select {
    width:100%;
    padding:10px;
    margin-top:5px;
    border:1px solid #ccc;
    border-radius:5px;
}

button {
    margin-top:20px;
    padding:12px;
    width:100%;
    background:#16a34a;
    color:white;
    border:none;
    border-radius:5px;
}

button:hover { background:#15803d; }
</style>
</head>

<body>

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

<div class="container">
<div class="card">

<h3>Add Payroll</h3>

<form method="POST" action="process_add_payroll.php">

<input type="hidden" name="payroll_ref" value="<?= $payroll_ref ?>">

<label>Teacher</label>
<select name="teacher_id" required>
<option value="">Select</option>
<?php foreach ($teachers as $t): ?>
<option value="<?= $t['teacher_id'] ?>"><?= htmlspecialchars($t['name']) ?></option>
<?php endforeach; ?>
</select>

<label>Month</label>
<select name="pay_month" required>
<option>January</option><option>February</option><option>March</option>
<option>April</option><option>May</option><option>June</option>
<option>July</option><option>August</option><option>September</option>
<option>October</option><option>November</option><option>December</option>
</select>

<label>Year</label>
<input type="number" name="pay_year" required>

<label>Basic Salary</label>
<input type="number" name="basic_salary" required>

<label>Allowances (₦)</label>
<input type="number" name="allowances" value="0">

<label>Tax (%)</label>
<input type="number" name="tax_percent" value="10">

<label>Pension (%)</label>
<input type="number" name="pension_percent" value="8">

<label>Payment Date</label>
<input type="date" name="payment_date" required>

<label>Payment Method</label>
<select name="payment_method">
<option>Cash</option>
<option>Bank Transfer</option>
<option>POS</option>
</select>

<button type="submit">Process Payroll</button>

</form>

</div>
</div>

</body>
</html>