<?php
include '../../includes/header.php';
require_once __DIR__ . '/_module.php';

payrollRequireAdmin($auth);

$gradeId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$payroll = new Payroll($conn);
$grade = $gradeId > 0 ? $payroll->getGradeById($gradeId) : null;
$error = '';

if (!$grade) {
    echo '<div class="container mt-4"><div class="alert alert-danger">Salary grade not found.</div></div>';
    include '../../includes/footer.php';
    exit();
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $gradeTitle = trim($_POST['grade_title'] ?? '');
    $basicSalary = (float)($_POST['basic_salary'] ?? 0);
    $pension = (float)($_POST['pension'] ?? 0);
    $healthInsurance = (float)($_POST['health_insurance'] ?? 0);
    $houseAllowance = (float)($_POST['house_allowance'] ?? 0);
    $other = (float)($_POST['other'] ?? 0);

    if ($gradeTitle !== '' && $basicSalary >= 0 && $payroll->updateGrade($gradeId, $gradeTitle, $basicSalary, $pension, $healthInsurance, $houseAllowance, $other)) {
        header('Location: salaries.php');
        exit();
    }

    $error = 'Failed to update the salary grade. Please check the values and try again.';
    $grade = array_merge($grade, [
        'grade_title' => $gradeTitle,
        'basic_salary' => $basicSalary,
        'pension' => $pension,
        'health_insurance' => $healthInsurance,
        'house_allowance' => $houseAllowance,
        'other' => $other,
    ]);
}

payrollModuleStyles();
?>

<div class="container-fluid px-4">
    <?php payrollModuleNav('salaries'); ?>

    <div class="payroll-page-head">
        <div>
            <h1>Edit Salary Grade</h1>
            <p>Update salary band, deductions, and allowance percentages.</p>
        </div>
        <a href="salaries.php" class="btn btn-outline-secondary no-print"><i class="fas fa-arrow-left"></i> Back to Salary Grades</a>
    </div>

    <?php if ($error): ?>
        <div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
    <?php endif; ?>

    <div class="payroll-panel">
        <form method="POST">
            <div class="row g-3">
                <div class="col-md-6">
                    <label for="grade_title" class="form-label">Grade Title</label>
                    <input type="text" class="form-control" id="grade_title" name="grade_title" value="<?php echo payrollText($grade['grade_title'] ?? ''); ?>" required>
                </div>
                <div class="col-md-6">
                    <label for="basic_salary" class="form-label">Basic Salary</label>
                    <input type="number" step="0.01" min="0" class="form-control" id="basic_salary" name="basic_salary" value="<?php echo htmlspecialchars($grade['basic_salary'] ?? '0'); ?>" required>
                </div>
                <div class="col-md-3">
                    <label for="pension" class="form-label">Pension (%)</label>
                    <input type="number" step="0.01" min="0" class="form-control" id="pension" name="pension" value="<?php echo htmlspecialchars($grade['pension'] ?? '0'); ?>" required>
                </div>
                <div class="col-md-3">
                    <label for="health_insurance" class="form-label">Health Insurance (%)</label>
                    <input type="number" step="0.01" min="0" class="form-control" id="health_insurance" name="health_insurance" value="<?php echo htmlspecialchars($grade['health_insurance'] ?? '0'); ?>" required>
                </div>
                <div class="col-md-3">
                    <label for="house_allowance" class="form-label">House Allowance (%)</label>
                    <input type="number" step="0.01" min="0" class="form-control" id="house_allowance" name="house_allowance" value="<?php echo htmlspecialchars($grade['house_allowance'] ?? '0'); ?>" required>
                </div>
                <div class="col-md-3">
                    <label for="other" class="form-label">Other Benefits (%)</label>
                    <input type="number" step="0.01" min="0" class="form-control" id="other" name="other" value="<?php echo htmlspecialchars($grade['other'] ?? '0'); ?>" required>
                </div>
            </div>

            <div class="mt-4">
                <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Update Grade</button>
                <a href="salaries.php" class="btn btn-outline-secondary">Cancel</a>
            </div>
        </form>
    </div>

    <?php payrollModuleEnd(); ?>
</div>

<?php include '../../includes/footer.php'; ?>
