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

$user = current_user();

if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'change_template') {
    verify_csrf();
    $tpl = $_POST['ui_template'] ?? 'modern';
    if (array_key_exists($tpl, ui_templates())) {
        $stmt = $pdo->prepare("UPDATE users SET ui_template = ? WHERE id = ?");
        $stmt->execute([$tpl, $user['id']]);
        $_SESSION['user']['ui_template'] = $tpl;
        $_SESSION['flash_success'] = 'قالب ظاهری تغییر کرد.';
    }
    header('Location: profile.php');
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'change_color') {
    verify_csrf();
    $clr = $_POST['ui_color'] ?? 'teal';
    if (array_key_exists($clr, ui_colors())) {
        $stmt = $pdo->prepare("UPDATE users SET ui_color = ? WHERE id = ?");
        $stmt->execute([$clr, $user['id']]);
        $_SESSION['user']['ui_color'] = $clr;
        $_SESSION['flash_success'] = 'رنگ سامانه تغییر کرد.';
    }
    header('Location: profile.php');
    exit;
}

$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$user['id']]);
$u = $stmt->fetch();
$currentTemplate = $u['ui_template'] ?? 'modern';
$currentColor = $u['ui_color'] ?? 'teal';

$dependents_enabled = get_setting($pdo, 'dependents_enabled', '1') === '1';

$page_title = 'پروفایل من';
include __DIR__ . '/../includes/header.php';
?>

<h4 class="mb-4">پروفایل من</h4>

<div class="card p-4 mb-3">
  <h5 class="mb-1">قالب ظاهری</h5>
  <p class="text-muted small mb-3">چیدمان و حس بصری سامانه را برای خودتان انتخاب کنید. تغییر بلافاصله اعمال می‌شود.</p>
  <form method="post" id="templateForm">
    <?= csrf_field() ?>
    <input type="hidden" name="action" value="change_template">
    <div class="tpl-grid">
      <?php foreach (ui_templates() as $key => $t): ?>
        <label class="tpl-option<?= $key === $currentTemplate ? ' selected' : '' ?>">
          <input type="radio" name="ui_template" value="<?= htmlspecialchars($key) ?>" <?= $key === $currentTemplate ? 'checked' : '' ?> onchange="this.form.submit()">
          <span class="tpl-preview tpl-preview-<?= htmlspecialchars($key) ?>">
            <span class="tpl-preview-bar"></span>
            <span class="tpl-preview-body"><span></span><span></span></span>
          </span>
          <span class="tpl-name"><?= htmlspecialchars($t['label']) ?></span>
          <span class="tpl-desc"><?= htmlspecialchars($t['desc']) ?></span>
        </label>
      <?php endforeach; ?>
    </div>
  </form>
</div>

<div class="card p-4 mb-3">
  <h5 class="mb-1">رنگ سامانه</h5>
  <p class="text-muted small mb-3">رنگ اصلی دکمه‌ها، لینک‌ها و منوی کناری را انتخاب کنید.</p>
  <form method="post" id="colorForm">
    <?= csrf_field() ?>
    <input type="hidden" name="action" value="change_color">
    <div class="color-grid">
      <?php foreach (ui_colors() as $key => $c): ?>
        <label class="color-option<?= $key === $currentColor ? ' selected' : '' ?>">
          <input type="radio" name="ui_color" value="<?= htmlspecialchars($key) ?>" <?= $key === $currentColor ? 'checked' : '' ?> onchange="this.form.submit()">
          <span class="color-swatch" style="background:<?= htmlspecialchars($c['swatch']) ?>"></span>
          <span class="color-name"><?= htmlspecialchars($c['label']) ?></span>
        </label>
      <?php endforeach; ?>
    </div>
  </form>
</div>

<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['full_name']) ?></span></div></div>
    <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['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['agreement_accepted_at']): ?>
        <span class="badge bg-success">تایید شده</span>
      <?php else: ?>
        <span class="badge bg-warning text-dark">تایید نشده</span>
      <?php endif; ?>
    </span></div></div>

    <div class="col-md-6"><div class="info-item"><span class="info-label">آدرس</span><span class="info-value"><?= htmlspecialchars($u['address'] ?? '-') ?></span></div></div>
    <div class="col-md-6"><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>
</div>

<div class="d-flex gap-2 flex-wrap">
  <a href="complete_profile.php" class="btn btn-outline-secondary"><?= icon('edit') ?> ویرایش اطلاعات شخصی</a>
  <a href="agreement.php" class="btn btn-outline-primary"><?= icon('file-check') ?> مشاهده متن قرارداد و تعهدنامه</a>
  <?php if ($dependents_enabled): ?>
    <a href="dependents.php" class="btn btn-outline-secondary"><?= icon('family') ?> مدیریت افراد تبعی</a>
  <?php endif; ?>
</div>

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