<?php
// admin/view-employee.php
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/functions.php';

if (!isAdmin()) {
    header('Location: login.php');
    exit;
}

$id = intval($_GET['id'] ?? 0);
if (!$id) {
    header('Location: employees.php');
    exit;
}

// Get employee details
$stmt = $pdo->prepare("SELECT e.*, d.name as department_name, des.name as designation_name
                       FROM employees e
                       LEFT JOIN departments d ON e.department = d.name
                       LEFT JOIN designations des ON e.designation = des.name
                       WHERE e.id = ?");
$stmt->execute([$id]);
$employee = $stmt->fetch();

if (!$employee) {
    header('Location: employees.php');
    exit;
}

include 'header.php';
?>

<style>
.employee-profile {
    background: #fff;
    border-radius: 12px;
    padding: 25px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    margin-bottom: 20px;
}

.profile-header {
    display: flex;
    align-items: center;
    gap: 20px;
    padding-bottom: 20px;
    border-bottom: 2px solid #e2e8f0;
    flex-wrap: wrap;
}

.profile-avatar {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background: #047dc3;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 32px;
    font-weight: 700;
    flex-shrink: 0;
}

.profile-info h2 {
    font-size: 24px;
    font-weight: 700;
    margin: 0;
}

.profile-info .employee-code {
    color: #94a3b8;
    font-size: 14px;
}

.profile-info .employee-status {
    margin-top: 5px;
}

.status-badge {
    padding: 4px 14px;
    border-radius: 20px;
    font-size: 13px;
    font-weight: 500;
}

.status-badge.active { background: #86efac; color: #166534; }
.status-badge.inactive { background: #fca5a5; color: #991b1b; }
.status-badge.full_time { background: #93c5fd; color: #1e40af; }
.status-badge.part_time { background: #fcd34d; color: #92400e; }
.status-badge.contract { background: #f59e0b; color: #92400e; }
.status-badge.intern { background: #86efac; color: #166534; }

.info-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 15px;
    margin-top: 20px;
}

.info-card {
    background: #f8fafc;
    border-radius: 10px;
    padding: 15px 20px;
}

.info-card h6 {
    font-weight: 600;
    color: #64748b;
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 8px;
    padding-bottom: 6px;
    border-bottom: 1px solid #e2e8f0;
}

.info-card p {
    margin-bottom: 3px;
    font-size: 14px;
}

.info-card .label {
    color: #64748b;
    font-weight: 500;
}

.action-buttons {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    margin-top: 20px;
}
</style>

<div class="admin-content">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><i class="fa-solid fa-user-tie me-2"></i> Employee Profile</h2>
        <div>
            <a href="edit-employee.php?id=<?php echo $employee['id']; ?>" class="btn btn-warning">
                <i class="fa-solid fa-edit"></i> Edit
            </a>
            <a href="employees.php" class="btn btn-secondary">
                <i class="fa-solid fa-arrow-left"></i> Back
            </a>
        </div>
    </div>

    <div class="employee-profile">
        <div class="profile-header">
            <div class="profile-avatar">
                <?php echo strtoupper(substr($employee['first_name'], 0, 1)); ?>
            </div>
            <div class="profile-info">
                <h2><?php echo htmlspecialchars($employee['first_name'] . ' ' . $employee['last_name']); ?></h2>
                <div class="employee-code"><?php echo $employee['employee_code']; ?></div>
                <div class="employee-status">
                    <span class="status-badge <?php echo $employee['status'] ? 'active' : 'inactive'; ?>">
                        <?php echo $employee['status'] ? 'Active' : 'Inactive'; ?>
                    </span>
                    <span class="status-badge <?php echo $employee['employee_type']; ?>">
                        <?php echo ucfirst(str_replace('_', ' ', $employee['employee_type'])); ?>
                    </span>
                </div>
            </div>
        </div>

        <div class="info-grid">
            <div class="info-card">
                <h6><i class="fa-solid fa-phone me-2"></i> Contact</h6>
                <p><span class="label">Phone:</span> <?php echo htmlspecialchars($employee['phone']); ?></p>
                <p><span class="label">Email:</span> <?php echo htmlspecialchars($employee['email'] ?? 'N/A'); ?></p>
                <p><span class="label">Alternative:</span> <?php echo htmlspecialchars($employee['alternative_phone'] ?? 'N/A'); ?></p>
            </div>

            <div class="info-card">
                <h6><i class="fa-solid fa-briefcase me-2"></i> Employment</h6>
                <p><span class="label">Department:</span> <?php echo htmlspecialchars($employee['department_name'] ?? 'N/A'); ?></p>
                <p><span class="label">Designation:</span> <?php echo htmlspecialchars($employee['designation_name'] ?? 'N/A'); ?></p>
                <p><span class="label">Joining:</span> <?php echo date('d M Y', strtotime($employee['joining_date'])); ?></p>
                <p><span class="label">Type:</span> <?php echo ucfirst(str_replace('_', ' ', $employee['employee_type'])); ?></p>
            </div>

            <div class="info-card">
                <h6><i class="fa-solid fa-money-bill me-2"></i> Salary</h6>
                <p><span class="label">Base Salary:</span> <?php echo number_format($employee['base_salary']); ?> TK</p>
                <p><span class="label">Allowance:</span> <?php echo number_format($employee['allowance']); ?> TK</p>
                <p><span class="label">Bonus:</span> <?php echo number_format($employee['bonus']); ?> TK</p>
                <p><span class="label">Total:</span> <?php echo number_format($employee['base_salary'] + $employee['allowance'] + $employee['bonus']); ?> TK</p>
            </div>

            <div class="info-card">
                <h6><i class="fa-solid fa-location-dot me-2"></i> Address</h6>
                <p><?php echo htmlspecialchars($employee['address'] ?? 'N/A'); ?></p>
                <?php if ($employee['city']): ?>
                <p><span class="label">City:</span> <?php echo htmlspecialchars($employee['city']); ?></p>
                <?php endif; ?>
            </div>

            <div class="info-card">
                <h6><i class="fa-solid fa-bank me-2"></i> Bank Details</h6>
                <p><span class="label">Bank:</span> <?php echo htmlspecialchars($employee['bank_name'] ?? 'N/A'); ?></p>
                <p><span class="label">Account No:</span> <?php echo htmlspecialchars($employee['bank_account_no'] ?? 'N/A'); ?></p>
            </div>

            <div class="info-card">
                <h6><i class="fa-solid fa-phone-volume me-2"></i> Emergency Contact</h6>
                <p><span class="label">Name:</span> <?php echo htmlspecialchars($employee['emergency_contact_name'] ?? 'N/A'); ?></p>
                <p><span class="label">Phone:</span> <?php echo htmlspecialchars($employee['emergency_contact_phone'] ?? 'N/A'); ?></p>
                <p><span class="label">Relation:</span> <?php echo htmlspecialchars($employee['emergency_contact_relation'] ?? 'N/A'); ?></p>
            </div>

            <div class="info-card">
                <h6><i class="fa-solid fa-calendar me-2"></i> Additional</h6>
                <p><span class="label">DOB:</span> <?php echo $employee['date_of_birth'] ? date('d M Y', strtotime($employee['date_of_birth'])) : 'N/A'; ?></p>
                <p><span class="label">Gender:</span> <?php echo ucfirst($employee['gender']); ?></p>
                <p><span class="label">Joined:</span> <?php echo date('d M Y, h:i A', strtotime($employee['created_at'])); ?></p>
            </div>
        </div>

        <div class="action-buttons">
            <a href="edit-employee.php?id=<?php echo $employee['id']; ?>" class="btn btn-warning">
                <i class="fa-solid fa-edit"></i> Edit
            </a>
            <a href="employee-attendance.php?emp=<?php echo $employee['id']; ?>" class="btn btn-info">
                <i class="fa-solid fa-clock"></i> Attendance
            </a>
            <a href="employee-leaves.php?emp=<?php echo $employee['id']; ?>" class="btn btn-primary">
                <i class="fa-solid fa-calendar-check"></i> Leaves
            </a>
            <button onclick="window.print()" class="btn btn-secondary">
                <i class="fa-solid fa-print"></i> Print Profile
            </button>
        </div>
    </div>
</div>

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