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

payrollRequireAdmin($auth);

$payroll = new Payroll($conn);
$employees = $payroll->getAllEmployees();
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $employee_id = (int)($_POST['employee_id'] ?? 0);
    $loan_type = trim($_POST['loan_type'] ?? '');
    $loan_amount = (float)($_POST['loan_amount'] ?? 0);
    $interest_rate = (float)($_POST['interest_rate'] ?? 0);
    $loan_months = (int)($_POST['loan_months'] ?? 0);
    $loans_starts = trim($_POST['loans_starts'] ?? '');
    $loan_status = trim($_POST['loan_status'] ?? 'pending');

    if ($employee_id > 0 && $loan_amount > 0 && $loan_months > 0 && $payroll->newLoan($employee_id, $loan_type, $loan_amount, $interest_rate, $loan_months, $loans_starts, $loan_status)) {
        header('Location: loans.php');
        exit();
    }

    $error = 'Failed to save the loan. Please check the employee, amount, and repayment period.';
}

payrollModuleStyles();
?>

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

    <div class="payroll-page-head">
        <div>
            <h1>New Employee Loan</h1>
            <p>Record a staff loan or salary advance for payroll deduction tracking.</p>
        </div>
        <a href="loans.php" class="btn btn-outline-secondary no-print"><i class="fas fa-arrow-left"></i> Back to Loans</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="employee_id" class="form-label">Employee</label>
                    <select class="form-control" id="employee_id" name="employee_id" required>
                        <option value="">Select employee</option>
                        <?php foreach ($employees as $employee): ?>
                            <option value="<?php echo (int)$employee['employee_id']; ?>">
                                <?php echo payrollText($employee['employee_fullname'] ?? ''); ?> - <?php echo payrollText($employee['employee_position'] ?? ''); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-6">
                    <label for="loan_type" class="form-label">Loan Type</label>
                    <select class="form-control" id="loan_type" name="loan_type" required>
                        <option value="salary_advance">Salary Advance</option>
                        <option value="emergency_loan">Emergency Loan</option>
                        <option value="personal_loan">Personal Loan</option>
                        <option value="educational_loan">Educational Loan</option>
                    </select>
                </div>
                <div class="col-md-4">
                    <label for="loan_amount" class="form-label">Loan Amount</label>
                    <input type="number" step="0.01" min="0.01" class="form-control" id="loan_amount" name="loan_amount" required>
                </div>
                <div class="col-md-4">
                    <label for="interest_rate" class="form-label">Interest Rate (%)</label>
                    <input type="number" step="0.01" min="0" class="form-control" id="interest_rate" name="interest_rate" value="0" required>
                </div>
                <div class="col-md-4">
                    <label for="loan_months" class="form-label">Repayment Months</label>
                    <input type="number" min="1" class="form-control" id="loan_months" name="loan_months" required>
                </div>
                <div class="col-md-6">
                    <label for="loans_starts" class="form-label">Deduction Starts</label>
                    <input type="date" class="form-control" id="loans_starts" name="loans_starts" required>
                </div>
                <div class="col-md-6">
                    <label for="loan_status" class="form-label">Status</label>
                    <select class="form-control" id="loan_status" name="loan_status" required>
                        <option value="pending">Pending</option>
                        <option value="approved">Approved</option>
                    </select>
                </div>
            </div>

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

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

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