<?php
require_once __DIR__ . '/../includes/functions.php';
require_role('admin');

$uid = (int)($_GET['id'] ?? 0);
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ? AND role = 'employee'");
$stmt->execute([$uid]);
$u = $stmt->fetch();

if (!$u) {
    $_SESSION['flash_error'] = 'کارمند یافت نشد.';
    header('Location: ' . base_url('admin/users.php'));
    exit;
}

$doc_stmt = $pdo->prepare("SELECT * FROM user_documents WHERE user_id = ?");
$doc_stmt->execute([$uid]);
$docs = $doc_stmt->fetchAll();

$docLabels = required_document_types() + optional_document_types();

$page_title = 'پروفایل ' . $u['full_name'];
include __DIR__ . '/../includes/header.php';
?>

<h4 class="mb-4">پروفایل و مدارک: <?= htmlspecialchars($u['full_name']) ?></h4>

<div class="card p-4 mb-3">
  <div class="row g-3">
    <div class="col-md-3"><div class="info-item"><span class="info-label">نام کاربری</span><span class="info-value"><?= htmlspecialchars($u['username']) ?></span></div></div>
    <div class="col-md-3"><div class="info-item"><span class="info-label">کد پرسنلی</span><span class="info-value"><?= htmlspecialchars($u['personnel_code'] ?? '-') ?></span></div></div>
    <div class="col-md-3"><div class="info-item"><span class="info-label">تاریخ استخدام</span><span class="info-value"><?= $u['hire_date'] ? to_jalali_string($u['hire_date']) : '-' ?></span></div></div>
    <div class="col-md-3"><div class="info-item"><span class="info-label">آزمایش بدو استخدام</span><span class="info-value"><?= $u['pre_employment_test_required'] ? 'بله - ' . to_jalali_string($u['pre_employment_test_date']) : 'خیر' ?></span></div></div>

    <div class="col-md-3"><div class="info-item"><span class="info-label">جنسیت</span><span class="info-value"><?= $u['gender'] === 'male' ? 'مرد' : ($u['gender'] === 'female' ? 'زن' : '-') ?></span></div></div>
    <div class="col-md-3"><div class="info-item"><span class="info-label">کد ملی</span><span class="info-value"><?= htmlspecialchars($u['national_code'] ?? '-') ?></span></div></div>
    <div class="col-md-3"><div class="info-item"><span class="info-label">شماره شناسنامه</span><span class="info-value"><?= htmlspecialchars($u['id_number'] ?? '-') ?></span></div></div>
    <div class="col-md-3"><div class="info-item"><span class="info-label">تاریخ تولد</span><span class="info-value"><?= $u['birth_date'] ? to_jalali_string($u['birth_date']) : '-' ?></span></div></div>

    <div class="col-md-3"><div class="info-item"><span class="info-label">موبایل</span><span class="info-value"><?= htmlspecialchars($u['mobile'] ?? '-') ?></span></div></div>
    <div class="col-md-3"><div class="info-item"><span class="info-label">تلفن ثابت</span><span class="info-value"><?= htmlspecialchars($u['phone'] ?? '-') ?></span></div></div>
    <div class="col-md-3"><div class="info-item"><span class="info-label">ایمیل</span><span class="info-value"><?= htmlspecialchars($u['email'] ?? '-') ?></span></div></div>
    <div class="col-md-3"><div class="info-item"><span class="info-label">سابقه بیماری خاص</span><span class="info-value">
      <?php if ($u['has_medical_condition'] === null): ?> - <?php elseif ((int)$u['has_medical_condition'] === 1): ?>
        بله: <?= htmlspecialchars($u['medical_history'] ?? '') ?>
      <?php else: ?> خیر <?php endif; ?>
    </span></div></div>

    <div class="col-12"><div class="info-item"><span class="info-label">آدرس</span><span class="info-value"><?= htmlspecialchars($u['address'] ?? '-') ?></span></div></div>
  </div>
</div>

<div class="card p-4">
  <h6 class="mb-3">مدارک</h6>
  <?php if (!$docs): ?>
    <p class="text-muted">مدرکی آپلود نشده است.</p>
  <?php else: ?>
    <div class="row g-3">
      <?php foreach ($docs as $d): ?>
        <?php $url = base_url('download_user_doc.php?id=' . $d['id']); ?>
        <div class="col-6 col-md-4">
          <a href="<?= $url ?>" target="_blank" class="text-decoration-none text-body">
            <div class="small fw-medium"><?= htmlspecialchars($docLabels[$d['doc_type']] ?? $d['doc_type']) ?></div>
            <?php if (is_image_ext($d['original_name'])): ?>
              <img src="<?= $url ?>" class="doc-thumb" alt="">
            <?php else: ?>
              <div class="doc-thumb d-flex align-items-center justify-content-center bg-light text-muted small">📄 PDF</div>
            <?php endif; ?>
          </a>
        </div>
      <?php endforeach; ?>
    </div>
  <?php endif; ?>
</div>

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