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

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

/* =========================
   INPUTS
========================= */
$teacher_id = (int)$_POST['teacher_id'];
$month_name = $_POST['pay_month'];
$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'];

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

/* =========================
   MONTH CONVERSION
========================= */
$month_num = date('m', strtotime($month_name));

/* =========================
   WORKING DAYS
========================= */
function getWorkingDays($month, $year) {
    $days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
    $count = 0;

    for ($i = 1; $i <= $days; $i++) {
        $date = sprintf("%04d-%02d-%02d", $year, $month, $i);
        if (date('N', strtotime($date)) < 6) {
            $count++;
        }
    }

    return max($count, 1);
}

$total_days = getWorkingDays($month_num, $year);

/* =========================
   ABSENT DAYS
========================= */
$stmt = $conn->prepare("
SELECT COUNT(*) AS absent_days
FROM attendance
WHERE teacher_id = ?
AND MONTH(date) = ?
AND YEAR(date) = ?
AND status = 'Absent'
");
$stmt->bind_param("iii", $teacher_id, $month_num, $year);
$stmt->execute();
$absent_days = ($stmt->get_result()->fetch_assoc()['absent_days']) ?? 0;

/* =========================
   LATENESS COUNT (NEW)
========================= */
$stmt = $conn->prepare("
SELECT COUNT(*) AS late_days
FROM attendance
WHERE teacher_id = ?
AND MONTH(date) = ?
AND YEAR(date) = ?
AND status = 'Late'
");
$stmt->bind_param("iii", $teacher_id, $month_num, $year);
$stmt->execute();
$late_days = ($stmt->get_result()->fetch_assoc()['late_days']) ?? 0;

/* =========================
   TIERED LATENESS DEDUCTION
========================= */
$late_deduction = 0;
$warning_flag = 0;

if ($late_days <= 3) {
    $late_deduction = $late_days * 500;

} elseif ($late_days <= 7) {
    $late_deduction = $late_days * 1000;

} else {
    $late_deduction = $late_days * 2000;
    $warning_flag = 1;
}

/* =========================
   SALARY CALCULATION
========================= */
$gross = $basic + $allowances;

$daily_rate = $basic / $total_days;
$absent_deduction = $absent_days * $daily_rate;

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

$net_salary = $gross - ($tax + $pension + $absent_deduction + $late_deduction);

/* =========================
   INSERT PAYROLL
========================= */
$stmt = $conn->prepare("
INSERT INTO payrolls 
(
teacher_id, pay_month, pay_year,
basic_salary, allowances,
tax, pension,
deduction,
absent_days, late_days, late_deduction,
net_salary,
payment_date, payment_method,
warning_flag
)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
");

$stmt->bind_param(
"issdddddiidsssi",
$teacher_id,
$month_name,
$year,
$basic,
$allowances,
$tax,
$pension,
$absent_deduction,
$absent_days,
$late_days,
$late_deduction,
$net_salary,
$date,
$method,
$warning_flag
);

$stmt->execute();

header("Location: view_payroll.php");
exit;
?>