<?php
/**
 * Enterprise HRM ERP System - View Employee
 */

require_once __DIR__ . '/../../includes/init.php';
requireLogin();
requirePermission('EMPLOYEE_VIEW');

$pageTitle = 'View Employee';
$currentModule = 'employee';

$employeeId = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($employeeId <= 0) {
    redirect(BASE_URL . '/modules/employee/list.php', 'Invalid employee ID.', 'error');
}

// Check data isolation - can user access this employee?
requireEmployeeAccess($employeeId);

// Get employee data
try {
    $db = getDB();
    
    // Build query based on user role
    if (isEmployee()) {
        // Employee can only see own data
        $stmt = $db->prepare("
            SELECT 
                e.*,
                d.department_name,
                des.designation_name,
                CONCAT(e.first_name, ' ', COALESCE(e.middle_name, ''), ' ', e.last_name) as full_name
            FROM employees e
            LEFT JOIN departments d ON e.department_id = d.department_id
            LEFT JOIN designations des ON e.designation_id = des.designation_id
            WHERE e.employee_id = ? AND e.email = ?
        ");
        $stmt->execute([$employeeId, $_SESSION['email'] ?? '']);
    } elseif (isSuperAdmin()) {
        // Super Admin can see all
        $stmt = $db->prepare("
            SELECT 
                e.*,
                d.department_name,
                des.designation_name,
                CONCAT(e.first_name, ' ', COALESCE(e.middle_name, ''), ' ', e.last_name) as full_name
            FROM employees e
            LEFT JOIN departments d ON e.department_id = d.department_id
            LEFT JOIN designations des ON e.designation_id = des.designation_id
            WHERE e.employee_id = ?
        ");
        $stmt->execute([$employeeId]);
    } else {
        // HR Admin - only their site
        $siteId = getCurrentSiteId();
        $stmt = $db->prepare("
            SELECT 
                e.*,
                d.department_name,
                des.designation_name,
                CONCAT(e.first_name, ' ', COALESCE(e.middle_name, ''), ' ', e.last_name) as full_name
            FROM employees e
            LEFT JOIN departments d ON e.department_id = d.department_id
            LEFT JOIN designations des ON e.designation_id = des.designation_id
            WHERE e.employee_id = ? AND e.site_id = ?
        ");
        $stmt->execute([$employeeId, $siteId]);
    }
    
    $employee = $stmt->fetch();
    
    if (!$employee) {
        redirect(BASE_URL . '/modules/employee/list.php', 'Employee not found or you do not have access.', 'error');
    }
    
} catch (Exception $e) {
    error_log("Error loading employee: " . $e->getMessage());
    redirect(BASE_URL . '/modules/employee/list.php', 'Error loading employee data.', 'error');
}

include __DIR__ . '/../../includes/header.php';
?>

<div class="card">
    <div class="card-header">
        <h2 class="card-title">Employee Details: <?php echo htmlspecialchars($employee['employee_code']); ?></h2>
        <div>
            <?php if (hasPermission('EMPLOYEE_EDIT')): ?>
            <a href="<?php echo BASE_URL; ?>/modules/employee/edit.php?id=<?php echo $employeeId; ?>" class="btn btn-primary">Edit</a>
            <?php endif; ?>
            <a href="<?php echo BASE_URL; ?>/modules/employee/list.php" class="btn btn-secondary">Back to List</a>
        </div>
    </div>
    
    <div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 30px;">
        <!-- Basic Information -->
        <div>
            <h3 style="margin-bottom: 15px; padding-bottom: 10px; border-bottom: 1px solid var(--border-color);">Basic Information</h3>
            
            <table style="width: 100%; border-collapse: collapse;">
                <tr>
                    <td style="padding: 8px 0; font-weight: 600; width: 40%;">Employee Code:</td>
                    <td style="padding: 8px 0;"><?php echo htmlspecialchars($employee['employee_code']); ?></td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Full Name:</td>
                    <td style="padding: 8px 0;"><?php echo htmlspecialchars($employee['full_name']); ?></td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Email:</td>
                    <td style="padding: 8px 0;"><?php echo htmlspecialchars($employee['email'] ?? '-'); ?></td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Phone:</td>
                    <td style="padding: 8px 0;"><?php echo htmlspecialchars($employee['phone'] ?? '-'); ?></td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Date of Birth:</td>
                    <td style="padding: 8px 0;"><?php echo formatDate($employee['date_of_birth']); ?></td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Gender:</td>
                    <td style="padding: 8px 0;"><?php echo ucfirst($employee['gender'] ?? '-'); ?></td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Marital Status:</td>
                    <td style="padding: 8px 0;"><?php echo ucfirst($employee['marital_status'] ?? '-'); ?></td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">National ID:</td>
                    <td style="padding: 8px 0;"><?php echo htmlspecialchars($employee['national_id'] ?? '-'); ?></td>
                </tr>
            </table>
        </div>
        
        <!-- Employment Information -->
        <div>
            <h3 style="margin-bottom: 15px; padding-bottom: 10px; border-bottom: 1px solid var(--border-color);">Employment Information</h3>
            
            <table style="width: 100%; border-collapse: collapse;">
                <tr>
                    <td style="padding: 8px 0; font-weight: 600; width: 40%;">Department:</td>
                    <td style="padding: 8px 0;"><?php echo htmlspecialchars($employee['department_name'] ?? '-'); ?></td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Designation:</td>
                    <td style="padding: 8px 0;"><?php echo htmlspecialchars($employee['designation_name'] ?? '-'); ?></td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Date of Joining:</td>
                    <td style="padding: 8px 0;"><?php echo formatDate($employee['date_of_joining']); ?></td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Employment Type:</td>
                    <td style="padding: 8px 0;"><?php echo ucfirst(str_replace('_', ' ', $employee['employment_type'] ?? '-')); ?></td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Employment Status:</td>
                    <td style="padding: 8px 0;">
                        <span class="status-badge status-<?php echo htmlspecialchars($employee['employment_status']); ?>">
                            <?php echo ucfirst($employee['employment_status']); ?>
                        </span>
                    </td>
                </tr>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Salary:</td>
                    <td style="padding: 8px 0;"><?php echo formatCurrency($employee['salary'] ?? 0); ?></td>
                </tr>
            </table>
        </div>
        
        <!-- Address Information -->
        <?php if ($employee['address_line1'] || $employee['city']): ?>
        <div>
            <h3 style="margin-bottom: 15px; padding-bottom: 10px; border-bottom: 1px solid var(--border-color);">Address Information</h3>
            
            <table style="width: 100%; border-collapse: collapse;">
                <?php if ($employee['address_line1']): ?>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600; width: 40%;">Address Line 1:</td>
                    <td style="padding: 8px 0;"><?php echo htmlspecialchars($employee['address_line1']); ?></td>
                </tr>
                <?php endif; ?>
                <?php if ($employee['address_line2']): ?>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Address Line 2:</td>
                    <td style="padding: 8px 0;"><?php echo htmlspecialchars($employee['address_line2']); ?></td>
                </tr>
                <?php endif; ?>
                <?php if ($employee['city']): ?>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">City:</td>
                    <td style="padding: 8px 0;"><?php echo htmlspecialchars($employee['city']); ?></td>
                </tr>
                <?php endif; ?>
                <?php if ($employee['state']): ?>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">State:</td>
                    <td style="padding: 8px 0;"><?php echo htmlspecialchars($employee['state']); ?></td>
                </tr>
                <?php endif; ?>
                <?php if ($employee['postal_code']): ?>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Postal Code:</td>
                    <td style="padding: 8px 0;"><?php echo htmlspecialchars($employee['postal_code']); ?></td>
                </tr>
                <?php endif; ?>
                <?php if ($employee['country']): ?>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Country:</td>
                    <td style="padding: 8px 0;"><?php echo htmlspecialchars($employee['country']); ?></td>
                </tr>
                <?php endif; ?>
            </table>
        </div>
        <?php endif; ?>
        
        <!-- Bank & Emergency Contact -->
        <div>
            <h3 style="margin-bottom: 15px; padding-bottom: 10px; border-bottom: 1px solid var(--border-color);">Bank & Emergency Contact</h3>
            
            <table style="width: 100%; border-collapse: collapse;">
                <?php if ($employee['bank_name']): ?>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600; width: 40%;">Bank Name:</td>
                    <td style="padding: 8px 0;"><?php echo htmlspecialchars($employee['bank_name']); ?></td>
                </tr>
                <?php endif; ?>
                <?php if ($employee['bank_account_number']): ?>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Account Number:</td>
                    <td style="padding: 8px 0;"><?php echo htmlspecialchars($employee['bank_account_number']); ?></td>
                </tr>
                <?php endif; ?>
                <?php if ($employee['bank_ifsc']): ?>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">IFSC Code:</td>
                    <td style="padding: 8px 0;"><?php echo htmlspecialchars($employee['bank_ifsc']); ?></td>
                </tr>
                <?php endif; ?>
                <?php if ($employee['emergency_contact_name']): ?>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Emergency Contact:</td>
                    <td style="padding: 8px 0;">
                        <?php echo htmlspecialchars($employee['emergency_contact_name']); ?>
                        <?php if ($employee['emergency_contact_relation']): ?>
                            (<?php echo htmlspecialchars($employee['emergency_contact_relation']); ?>)
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endif; ?>
                <?php if ($employee['emergency_contact_phone']): ?>
                <tr>
                    <td style="padding: 8px 0; font-weight: 600;">Emergency Phone:</td>
                    <td style="padding: 8px 0;"><?php echo htmlspecialchars($employee['emergency_contact_phone']); ?></td>
                </tr>
                <?php endif; ?>
            </table>
        </div>
    </div>
</div>

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

