<?php
// view_employee.php

ini_set('display_errors', 0);
error_reporting(E_ALL);
ini_set('log_errors', 1);
ini_set('error_log', 'php_errors.log');

session_start();
require_once("../config/config.php"); // $conn = mysqli connection

$employee_id = $_GET['id'] ?? '';
if (empty($employee_id)) {
    die("Invalid employee ID.");
}

$stmt = $conn->prepare("SELECT * FROM employee_master WHERE employee_id = ? AND status = 'active'");
$stmt->bind_param("s", $employee_id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    die("Employee not found or has been removed.");
}

$emp = $result->fetch_assoc();
$stmt->close();

// Helper to display value or fallback
function val($value, $fallback = '-') {
    return $value !== '' && $value !== null ? htmlspecialchars($value) : $fallback;
}

// Format date
function formatDate($date) {
    return $date ? date('d-m-Y', strtotime($date)) : '-';
}

// Document path helper
function docPath($path) {
    return $path ? "../" . $path : null;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>View Employee - <?= val($emp['employee_id']) ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <style>
        :root { --primary: #8b5cf6; --primary-light: #a78bfa; --text: #e0e7ff; --bg: rgba(255,255,255,0.1); }
        * { margin:0; padding:0; box-sizing:border-box; }
        body { font-family:'Segoe UI',sans-serif; min-height:100vh; background:linear-gradient(rgba(0,0,0,0.6),rgba(0,0,0,0.6)),url('../background.jpeg') center/cover no-repeat fixed; color:var(--text); padding:20px; }
        .container { max-width:1200px; margin:0 auto; background:var(--bg); backdrop-filter:blur(16px); border-radius:24px; border:1px solid rgba(255,255,255,0.2); box-shadow:0 20px 50px rgba(0,0,0,0.4); overflow:hidden; }
        .header { background:rgba(255,255,255,0.15); padding:40px; text-align:center; position:relative; }
        .header h1 { font-size:2.5rem; margin-bottom:10px; }
        .back-btn { position:absolute; top:30px; left:40px; padding:12px 20px; background:rgba(139,92,246,0.3); border:1px solid var(--primary-light); border-radius:14px; color:white; text-decoration:none; font-weight:600; transition:.3s; }
        .back-btn:hover { background:rgba(139,92,246,0.5); transform:translateY(-3px); }
        .info-grid { display:grid; grid-template-columns:repeat(auto-fit, minmax(300px, 1fr)); gap:30px; padding:40px; }
        .section { background:rgba(255,255,255,0.08); border-radius:16px; padding:25px; border:1px solid rgba(255,255,255,0.15); }
        .section-title { font-size:1.6rem; margin-bottom:20px; padding-bottom:10px; border-bottom:1px solid rgba(255,255,255,0.3); display:flex; align-items:center; gap:10px; }
        .detail-row { display:flex; flex-wrap:wrap; margin-bottom:16px; gap:10px; }
        .label { font-weight:600; min-width:180px; color:rgba(255,255,255,0.9); }
        .value { flex:1; word-break:break-word; }
        .doc-grid { display:grid; grid-template-columns:repeat(auto-fit, minmax(200px, 1fr)); gap:20px; margin-top:20px; }
        .doc-item { background:rgba(255,255,255,0.1); border-radius:12px; padding:15px; text-align:center; border:1px solid rgba(255,255,255,0.2); }
        .doc-item i { font-size:2.5rem; margin-bottom:10px; color:var(--primary-light); }
        .doc-name { font-size:0.95rem; margin-bottom:12px; }
        .doc-actions a { color:var(--primary-light); text-decoration:none; margin:0 8px; font-size:0.9rem; }
        .doc-actions a:hover { text-decoration:underline; }
        .no-doc { color:rgba(255,255,255,0.6); font-style:italic; }
        @media (max-width:768px) { .info-grid { grid-template-columns:1fr; } .back-btn { position:static; display:inline-block; margin-bottom:20px; } }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <a href="employee-management.php" class="back-btn">
                <i class="fas fa-arrow-left"></i> Back to List
            </a>
            <h1>Employee Details</h1>
            <p>Complete profile information for <strong><?= val($emp['full_name']) ?></strong></p>
        </div>

        <div class="info-grid">

            <!-- Company Information -->
            <div class="section">
                <h2 class="section-title"><i class="fas fa-building"></i> Company Information</h2>
                <div class="detail-row"><span class="label">Employee ID:</span><span class="value"><strong><?= val($emp['employee_id']) ?></strong></span></div>
                <div class="detail-row"><span class="label">Date of Joining:</span><span class="value"><?= formatDate($emp['date_of_joining']) ?></span></div>
                <div class="detail-row"><span class="label">Date of Registration:</span><span class="value"><?= formatDate($emp['date_of_registration']) ?></span></div>
                <div class="detail-row"><span class="label">Working Hours:</span><span class="value"><?= val($emp['working_hours'], '0') ?> hours</span></div>
                <div class="detail-row"><span class="label">Designation:</span><span class="value"><?= val($emp['designation']) ?></span></div>
                <div class="detail-row"><span class="label">Place of Work:</span><span class="value"><?= val($emp['place_of_work']) ?></span></div>
                <div class="detail-row"><span class="label">Unit Head:</span><span class="value"><?= val($emp['unit_head_name']) ?></span></div>
                <div class="detail-row"><span class="label">Reference:</span><span class="value"><?= val($emp['reference']) ?></span></div>
                <div class="detail-row"><span class="label">Wages (Per Hour):</span><span class="value">₹<?= number_format((float)$emp['wages'], 2) ?></span></div>
                <div class="detail-row"><span class="label">Decided Salary:</span><span class="value">₹<?= number_format((float)$emp['decided_salary'], 2) ?></span></div>
            </div>

            <!-- Personal Information -->
            <div class="section">
                <h2 class="section-title"><i class="fas fa-user"></i> Personal Information</h2>
                <div class="detail-row"><span class="label">Full Name:</span><span class="value"><?= val($emp['full_name']) ?></span></div>
                <div class="detail-row"><span class="label">First Name:</span><span class="value"><?= val($emp['first_name']) ?></span></div>
                <div class="detail-row"><span class="label">Middle Name:</span><span class="value"><?= val($emp['middle_name']) ?></span></div>
                <div class="detail-row"><span class="label">Last Name:</span><span class="value"><?= val($emp['last_name']) ?></span></div>
                <div class="detail-row"><span class="label">Date of Birth:</span><span class="value"><?= formatDate($emp['date_of_birth']) ?></span></div>
                <div class="detail-row"><span class="label">Blood Group:</span><span class="value"><?= val($emp['blood_group']) ?></span></div>
                <div class="detail-row"><span class="label">Education:</span><span class="value"><?= val($emp['education']) ?></span></div>
                <div class="detail-row"><span class="label">Mother Tongue:</span><span class="value"><?= val($emp['mother_tongue']) ?></span></div>
                <div class="detail-row"><span class="label">Religion:</span><span class="value"><?= val($emp['religion']) ?></span></div>
                <div class="detail-row"><span class="label">Marital Status:</span><span class="value"><?= val($emp['marital_status']) ?></span></div>
            </div>

            <!-- Contact & Official IDs -->
            <div class="section">
                <h2 class="section-title"><i class="fas fa-id-card"></i> Contact & Official Details</h2>
                <div class="detail-row"><span class="label">Mobile Number:</span><span class="value"><?= val($emp['mobile_number']) ?></span></div>
                <div class="detail-row"><span class="label">Alternate Mobile:</span><span class="value"><?= val($emp['alternate_mobile_number']) ?></span></div>
                <div class="detail-row"><span class="label">Aadhar Number:</span><span class="value"><?= val($emp['aadhar_number']) ?></span></div>
                <div class="detail-row"><span class="label">PAN Number:</span><span class="value"><?= val($emp['pan_number']) ?></span></div>
                <div class="detail-row"><span class="label">EPF Number:</span><span class="value"><?= val($emp['epf_number']) ?></span></div>
                <div class="detail-row"><span class="label">UAN Number:</span><span class="value"><?= val($emp['uan_number']) ?></span></div>
                <div class="detail-row"><span class="label">ESIC Number:</span><span class="value"><?= val($emp['esic_number']) ?></span></div>
            </div>

            <!-- Bank Details -->
            <div class="section">
                <h2 class="section-title"><i class="fas fa-university"></i> Bank Details</h2>
                <div class="detail-row"><span class="label">Account Number:</span><span class="value"><?= val($emp['bank_account_number']) ?></span></div>
                <div class="detail-row"><span class="label">IFSC Code:</span><span class="value"><?= val($emp['ifsc_code']) ?></span></div>
                <div class="detail-row"><span class="label">Account Holder:</span><span class="value"><?= val($emp['bank_account_holder_name']) ?></span></div>
                <div class="detail-row"><span class="label">Branch Name:</span><span class="value"><?= val($emp['bank_branch_name']) ?></span></div>
                <div class="detail-row"><span class="label">Bank Name & Address:</span><span class="value"><?= val($emp['bank_name_and_address']) ?></span></div>
            </div>

            <!-- Permanent Address -->
            <div class="section">
                <h2 class="section-title"><i class="fas fa-home"></i> Permanent Address</h2>
                <div class="detail-row"><span class="label">Address:</span><span class="value"><?= val($emp['permanent_address']) ?></span></div>
                <div class="detail-row"><span class="label">Post / Taluka:</span><span class="value"><?= val($emp['post']) ?> / <?= val($emp['taluka']) ?></span></div>
                <div class="detail-row"><span class="label">City / Village:</span><span class="value"><?= val($emp['city_or_village']) ?></span></div>
                <div class="detail-row"><span class="label">District:</span><span class="value"><?= val($emp['district']) ?></span></div>
                <div class="detail-row"><span class="label">State:</span><span class="value"><?= val($emp['state']) ?></span></div>
                <div class="detail-row"><span class="label">Pincode:</span><span class="value"><?= val($emp['pincode']) ?></span></div>
            </div>

            <!-- Current Address -->
            <div class="section">
                <h2 class="section-title"><i class="fas fa-map-marker-alt"></i> Current Address</h2>
                <div class="detail-row"><span class="label">Address:</span><span class="value"><?= val($emp['current_address']) ?></span></div>
                <div class="detail-row"><span class="label">Post:</span><span class="value"><?= val($emp['current_post']) ?></span></div>
                <div class="detail-row"><span class="label">City / Village:</span><span class="value"><?= val($emp['current_city_or_village']) ?></span></div>
                <div class="detail-row"><span class="label">State:</span><span class="value"><?= val($emp['current_state']) ?></span></div>
                <div class="detail-row"><span class="label">Pincode:</span><span class="value"><?= val($emp['current_pincode']) ?></span></div>
            </div>

            <!-- Uploaded Documents -->
            <div class="section">
                <h2 class="section-title"><i class="fas fa-folder-open"></i> Uploaded Documents</h2>
                <div class="doc-grid">
                    <?php
                    $documents = [
                        'Aadhar Card' => $emp['aadhar_card_file'],
                        'PAN Card' => $emp['pan_card_file'],
                        'Passport' => $emp['passport_file'],
                        'Driving License' => $emp['driving_license_file'],
                        'Left Thumb' => $emp['left_hand_thumb_file'],
                        'Right Thumb' => $emp['right_hand_thumb_file'],
                        'Other Document' => $emp['other_document_file']
                    ];

                    foreach ($documents as $name => $path):
                        $fullPath = docPath($path);
                        $isImage = $fullPath && in_array(strtolower(pathinfo($fullPath, PATHINFO_EXTENSION)), ['jpg', 'jpeg', 'png']);
                    ?>
                        <div class="doc-item">
                            <i class="<?= $isImage ? 'fas fa-image' : 'fas fa-file-pdf' ?>"></i>
                            <div class="doc-name"><?= $name ?></div>
                            <?php if ($fullPath && file_exists($fullPath)): ?>
                                <div class="doc-actions">
                                    <a href="../<?= $path ?>" target="_blank">View</a>
                                    <a href="<?= $path ?>" download>Download</a>
                                </div>
                            <?php else: ?>
                                <div class="no-doc">Not uploaded</div>
                            <?php endif; ?>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>

        </div>
    </div>
</body>
</html>