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

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

/* =========================
   GET PAYROLL ID
========================= */
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($id <= 0) {
    die("Invalid payroll ID");
}

/* =========================
   FETCH PAYROLL RECORD
========================= */
$stmt = $conn->prepare("
SELECT p.*, t.name AS teacher_name
FROM payrolls p
JOIN teachers t ON t.teacher_id = p.teacher_id
WHERE p.id = ?
");
$stmt->bind_param("i", $id);
$stmt->execute();
$data = $stmt->get_result()->fetch_assoc();

if (!$data) {
    die("Payroll record not found");
}
$stmt->close();

/* =========================
   FETCH TEACHERS
========================= */
$teachers = [];
$res = $conn->query("SELECT teacher_id, name FROM teachers ORDER BY name ASC");
while ($row = $res->fetch_assoc()) {
    $teachers[] = $row;
}

/* =========================
   UPDATE PROCESS
========================= */
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $teacher_id = (int)$_POST['teacher_id'];
    $pay_month = $_POST['pay_month'];
    $pay_year = (int)$_POST['pay_year'];

    $basic = (float)$_POST['basic_salary'];
    $allowances = (float)$_POST['allowances'];
    $tax_percent = (float)$_POST['tax_percent'];
    $pension_percent = (float)$_POST['pension_percent'];

    $absent_deduction = (float)$_POST['absent_deduction'];
    $late_deduction = (float)$_POST['late_deduction'];

    $payment_date = $_POST['payment_date'];
    $payment_method = $_POST['payment_method'];

    /* =========================
       CALCULATIONS
    ========================= */
    $gross = $basic + $allowances;

    $tax = ($tax_percent / 100) * $gross;
    $pension = ($pension_percent / 100) * $gross;

    $total_deduction = $tax + $pension + $absent_deduction + $late_deduction;

    $net_salary = $gross - $total_deduction;

    /* =========================
       UPDATE DB
    ========================= */
    $update = $conn->prepare("
        UPDATE payrolls SET
            teacher_id = ?,
            pay_month = ?,
            pay_year = ?,
            basic_salary = ?,
            allowances = ?,
            tax = ?,
            pension = ?,
            deduction = ?,
            late_deduction = ?,
            net_salary = ?,
            payment_date = ?,
            payment_method = ?
        WHERE id = ?
    ");

    $update->bind_param(
        "isidddddddssi",
        $teacher_id,
        $pay_month,
        $pay_year,
        $basic,
        $allowances,
        $tax,
        $pension,
        $absent_deduction,
        $late_deduction,
        $net_salary,
        $payment_date,
        $payment_method,
        $id
    );

    $update->execute();

    header("Location: view_payroll.php?updated=1");
    exit;
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Edit Payroll | ERP System</title>

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

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

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

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

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

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

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

button:hover {
    background: #15803d;
}

.badge {
    background: #1e293b;
    color: white;
    padding: 6px 10px;
    display: inline-block;
    border-radius: 5px;
    margin-bottom: 10px;
}
</style>
</head>

<body>

<div class="header">
    <h2>AGID EDUCATIONAL INSTITUTE</h2>
    <p>Edit Payroll Record</p>
</div>

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

<span class="badge">Payroll ID: <?= $id ?></span>

<form method="POST">

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

<!-- MONTH -->
<label>Month</label>
<input type="text" name="pay_month" value="<?= $data['pay_month'] ?>" required>

<!-- YEAR -->
<label>Year</label>
<input type="number" name="pay_year" value="<?= $data['pay_year'] ?>" required>

<!-- SALARY -->
<label>Basic Salary</label>
<input type="number" name="basic_salary" value="<?= $data['basic_salary'] ?>" required>

<!-- ALLOWANCES -->
<label>Allowances</label>
<input type="number" name="allowances" value="<?= $data['allowances'] ?>">

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

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

<!-- ABSENT DEDUCTION -->
<label>Absent Deduction (₦)</label>
<input type="number" name="absent_deduction" value="<?= $data['absent_deduction'] ?? 0 ?>">

<!-- LATE DEDUCTION -->
<label>Late Deduction (₦)</label>
<input type="number" name="late_deduction" value="<?= $data['late_deduction'] ?? 0 ?>">

<!-- PAYMENT DATE -->
<label>Payment Date</label>
<input type="date" name="payment_date" value="<?= $data['payment_date'] ?>">

<!-- PAYMENT METHOD -->
<label>Payment Method</label>
<select name="payment_method">
<option <?= $data['payment_method']=='Cash'?'selected':'' ?>>Cash</option>
<option <?= $data['payment_method']=='Bank Transfer'?'selected':'' ?>>Bank Transfer</option>
<option <?= $data['payment_method']=='POS'?'selected':'' ?>>POS</option>
</select>

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

</form>

</div>
</div>

</body>
</html>