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

payrollRequireAdmin($auth);

$payroll = new Payroll($conn);
$employeeId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$employee = $employeeId > 0 ? $payroll->getEmployeeById($employeeId) : null;

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

$bankSummary = $employee['bank_details'] ?? '';
if (!empty($employee['bank_account_number'])) {
    $bankSummary = trim(($employee['bank_name'] ?? '') . ' ' . ($employee['bank_account_number'] ?? '') . ' ' . ($employee['bank_branch'] ?? ''));
}

payrollModuleStyles();
?>

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

    <div class="payroll-page-head">
        <div>
            <h1><?php echo payrollText($employee['employee_fullname']); ?></h1>
            <p><?php echo payrollText($employee['employee_code'] ?? ('EMP-' . $employeeId)); ?> · <?php echo payrollText($employee['employee_position'] ?? ''); ?> · <?php echo payrollStatusBadge($employee['employment_status'] ?? 'active'); ?></p>
        </div>
        <div class="no-print">
            <a href="employee_list.php" class="btn btn-outline-secondary"><i class="fas fa-arrow-left"></i> Back to Employees</a>
            <button type="button" onclick="window.print()" class="btn btn-outline-secondary"><i class="fas fa-print"></i> Print</button>
        </div>
    </div>

    <div class="payroll-metrics">
        <div class="payroll-metric"><strong>Basic Salary</strong><span><?php echo payrollMoney($employee['basic_salary'] ?? 0); ?></span></div>
        <div class="payroll-metric"><strong>Pay Frequency</strong><span><?php echo payrollText($employee['payment_frequency'] ?? ''); ?></span></div>
        <div class="payroll-metric"><strong>Start Date</strong><span><?php echo payrollText($employee['contract_start'] ?? ''); ?></span></div>
        <div class="payroll-metric"><strong>Branch</strong><span><?php echo payrollText($employee['branch_name'] ?? $employee['branch'] ?? ''); ?></span></div>
    </div>

    <div class="row g-3">
        <div class="col-lg-6">
            <div class="payroll-panel h-100">
                <div class="payroll-panel-title"><h2>Personal Details</h2></div>
                <div class="row g-3">
                    <div class="col-md-6"><strong>Legal Name</strong><br><?php echo payrollText($employee['employee_fullname']); ?></div>
                    <div class="col-md-6"><strong>Preferred Name</strong><br><?php echo payrollText($employee['preferred_name'] ?? ''); ?></div>
                    <div class="col-md-6"><strong>Date of Birth</strong><br><?php echo payrollText($employee['date_of_birth'] ?? ''); ?></div>
                    <div class="col-md-6"><strong>Gender</strong><br><?php echo payrollText($employee['gender'] ?? ''); ?></div>
                    <div class="col-md-6"><strong>Phone</strong><br><?php echo payrollText($employee['employee_phone'] ?? ''); ?></div>
                    <div class="col-md-6"><strong>Email</strong><br><?php echo payrollText($employee['email'] ?? ''); ?></div>
                    <div class="col-md-6"><strong>National ID</strong><br><?php echo payrollText($employee['national_id'] ?? ''); ?></div>
                    <div class="col-md-6"><strong>Tax/Pension ID</strong><br><?php echo payrollText(($employee['tax_identification_number'] ?? '') ?: ($employee['social_security_number'] ?? '')); ?></div>
                    <div class="col-12"><strong>Residence</strong><br><?php echo payrollText($employee['place_of_residence'] ?? ''); ?></div>
                </div>
            </div>
        </div>

        <div class="col-lg-6">
            <div class="payroll-panel h-100">
                <div class="payroll-panel-title"><h2>Employment</h2></div>
                <div class="row g-3">
                    <div class="col-md-6"><strong>Branch</strong><br><?php echo payrollText($employee['branch_name'] ?? $employee['branch'] ?? ''); ?></div>
                    <div class="col-md-6"><strong>Department</strong><br><?php echo payrollText($employee['department_name'] ?? ''); ?></div>
                    <div class="col-md-6"><strong>Position</strong><br><?php echo payrollText($employee['employee_position'] ?? ''); ?></div>
                    <div class="col-md-6"><strong>Employment Type</strong><br><?php echo payrollText($employee['employment_type'] ?? ''); ?></div>
                    <div class="col-md-6"><strong>Contract Start</strong><br><?php echo payrollText($employee['contract_start'] ?? ''); ?></div>
                    <div class="col-md-6"><strong>Contract End</strong><br><?php echo payrollText($employee['contract_ends'] ?? 'Permanent/Open'); ?></div>
                    <div class="col-md-6"><strong>Salary Grade</strong><br><?php echo payrollText($employee['grade_title'] ?? ''); ?></div>
                    <div class="col-md-6"><strong>Currency</strong><br><?php echo payrollText($employee['currency_code'] ?? 'MWK'); ?></div>
                </div>
            </div>
        </div>

        <div class="col-lg-6">
            <div class="payroll-panel h-100">
                <div class="payroll-panel-title"><h2>Emergency Contact</h2></div>
                <div class="row g-3">
                    <div class="col-md-6"><strong>Name</strong><br><?php echo payrollText($employee['next_of_kin_fullname'] ?? ''); ?></div>
                    <div class="col-md-6"><strong>Phone</strong><br><?php echo payrollText($employee['next_of_kin_phone'] ?? ''); ?></div>
                    <div class="col-12"><strong>Address</strong><br><?php echo payrollText($employee['next_of_kin_address'] ?? ''); ?></div>
                </div>
            </div>
        </div>

        <div class="col-lg-6">
            <div class="payroll-panel h-100">
                <div class="payroll-panel-title"><h2>Payment Details</h2></div>
                <div class="row g-3">
                    <div class="col-md-6"><strong>Bank</strong><br><?php echo payrollText($employee['bank_name'] ?? ''); ?></div>
                    <div class="col-md-6"><strong>Account Name</strong><br><?php echo payrollText($employee['bank_account_name'] ?? ''); ?></div>
                    <div class="col-md-6"><strong>Account Number</strong><br><?php echo payrollText($employee['bank_account_number'] ?? ''); ?></div>
                    <div class="col-md-6"><strong>Bank Branch</strong><br><?php echo payrollText($employee['bank_branch'] ?? ''); ?></div>
                    <div class="col-12"><strong>Legacy Bank Details</strong><br><?php echo payrollText($bankSummary); ?></div>
                </div>
            </div>
        </div>
    </div>

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

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