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

payrollRequireAdmin($auth);

$payroll = new Payroll($conn);
$error = '';

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);
    $overtime = (float)($_POST['overtime'] ?? 0);
    $other = (float)($_POST['other'] ?? 0);

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

    $error = 'Failed to create salary grade. Please check the values and try again.';
}

payrollModuleStyles();
?>

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

    <div class="payroll-page-head">
        <div>
            <h1>Add Salary Grade</h1>
            <p>Create a pay band with standard pension, allowance, and benefit settings.</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" 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" required>
                </div>
                <div class="col-md-3">
                    <label for="pension" class="form-label">Employee Pension (%)</label>
                    <input type="number" step="0.01" min="0" class="form-control" id="pension" name="pension" value="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="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="0" required>
                </div>
                <div class="col-md-3">
                    <label for="overtime" class="form-label">Overtime Rate</label>
                    <input type="number" step="0.01" min="0" class="form-control" id="overtime" name="overtime" value="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="0" required>
                </div>
            </div>

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

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

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