<?php
$isSelf = Auth::id() === (int)$user['id'];
$isSuperAdmin = !empty($user['is_super_admin']);
?>
<div class="mb-3">
  <a href="<?= url('/admin/users') ?>" class="btn btn-outline-secondary btn-sm py-1 px-3">
    <i class="bi bi-arrow-left me-1"></i>กลับรายการผู้ใช้งาน
  </a>
</div>

<!-- ── Detail Summary Header ───────────────────────────── -->
<div class="admin-detail-summary-header">
  <div class="d-flex align-items-center gap-3">
    <div class="rounded-circle bg-primary bg-opacity-10 text-primary d-flex align-items-center justify-content-center fw-bold fs-3" style="width: 60px; height: 60px;">
      <?= strtoupper(mb_substr($user['first_name'] ?? 'U', 0, 1)) ?>
    </div>
    <div>
      <div class="d-flex align-items-center gap-2 flex-wrap">
        <h5 class="fw-bold mb-0 text-dark"><?= e($user['first_name'] . ' ' . $user['last_name']) ?></h5>
        <?php if ($isSuperAdmin): ?>
          <span class="badge bg-warning text-dark"><i class="bi bi-shield-shaded me-1"></i>Super Admin</span>
        <?php endif; ?>
        <?php if ($user['status'] === 'active'): ?>
          <span class="status-pill status-active">ปกติ</span>
        <?php elseif ($user['status'] === 'suspended'): ?>
          <span class="status-pill status-suspended">ระงับการใช้งาน</span>
        <?php else: ?>
          <span class="status-pill status-secondary"><?= e($user['status']) ?></span>
        <?php endif; ?>
      </div>
      <div class="text-muted small mt-1">
        <i class="bi bi-person me-1"></i>@<?= e($user['username']) ?> &bull;
        <i class="bi bi-envelope me-1"></i><?= e($user['email']) ?>
      </div>
    </div>
  </div>

  <div class="d-flex align-items-center gap-2">
    <?php if (!$isSelf && !$isSuperAdmin && AdminMiddleware::can('user.suspend')): ?>
      <?php if ($user['status'] === 'active'): ?>
        <button type="button" class="btn btn-outline-danger btn-sm px-3" data-bs-toggle="modal" data-bs-target="#suspendUserModal">
          <i class="bi bi-slash-circle me-1"></i>ระงับบัญชีผู้ใช้
        </button>
      <?php elseif ($user['status'] === 'suspended'): ?>
        <form action="<?= url('/admin/users/' . (int)$user['id'] . '/reactivate') ?>" method="POST" class="d-inline">
          <?= Security::csrfField() ?>
          <button type="submit" class="btn btn-success btn-sm px-3" onclick="return confirm('ยืนยันเปิดใช้งานบัญชีนี้อีกครั้ง?')">
            <i class="bi bi-check-circle me-1"></i>เปิดใช้บัญชีอีกครั้ง
          </button>
        </form>
      <?php endif; ?>
    <?php endif; ?>
  </div>
</div>

<?php if (!empty($user['pending_delete_request'])): ?>
<div class="alert alert-danger border-danger d-flex flex-column flex-md-row justify-content-between align-items-md-center gap-3 p-3 mb-4 rounded-3 shadow-sm">
  <div>
    <div class="fw-bold text-danger mb-1">
      <i class="bi bi-exclamation-octagon-fill me-2"></i>ผู้ใช้งานนี้มีคำขอลบบัญชีที่รอดำเนินการ (Pending Deletion Request)
    </div>
    <div class="small text-dark">
      <strong>เหตุผล:</strong> <?= !empty($user['pending_delete_request']['reason']) ? e($user['pending_delete_request']['reason']) : 'ไม่ได้ระบุเหตุผล' ?>
      &bull; <span class="text-muted">ส่งเมื่อ <?= date('d/m/Y H:i', strtotime($user['pending_delete_request']['created_at'])) ?></span>
    </div>
  </div>
  <div class="d-flex gap-2">
    <a href="<?= url('/admin/users/deletion-requests?status=pending&q=' . urlencode($user['username'])) ?>" class="btn btn-danger btn-sm px-3 text-nowrap">
      <i class="bi bi-person-x me-1"></i>ไปที่หน้าพิจารณาคำขอ
    </a>
  </div>
</div>
<?php endif; ?>

<div class="row g-4">
  <!-- Left Information Column -->
  <div class="col-lg-5">
    <!-- Account Information Card -->
    <div class="admin-detail-card mb-4">
      <div class="admin-detail-card-header">
        <span><i class="bi bi-info-circle text-primary me-2"></i>ข้อมูลบัญชีทั่วไป</span>
      </div>
      <div class="admin-detail-card-body">
        <dl class="row mb-0 small">
          <dt class="col-5 text-muted mb-2">User ID</dt>
          <dd class="col-7 font-monospace mb-2">#<?= (int)$user['id'] ?></dd>

          <dt class="col-5 text-muted mb-2">ชื่อ - นามสกุล</dt>
          <dd class="col-7 mb-2"><?= e($user['first_name'] . ' ' . $user['last_name']) ?></dd>

          <dt class="col-5 text-muted mb-2">อีเมล (Email)</dt>
          <dd class="col-7 mb-2"><?= e($user['email']) ?></dd>

          <dt class="col-5 text-muted mb-2">เบอร์โทรศัพท์</dt>
          <dd class="col-7 mb-2"><?= e($user['phone'] ?? '—') ?></dd>

          <dt class="col-5 text-muted mb-2">Roles ในระบบ</dt>
          <dd class="col-7 mb-2">
            <?php foreach (explode(',', $user['roles'] ?? '') as $r): if ($r): ?>
              <span class="badge bg-light text-dark border me-1 mb-1" style="font-size: 0.72rem;"><?= e($r) ?></span>
            <?php endif; endforeach; ?>
          </dd>

          <dt class="col-5 text-muted mb-2">วันที่ลงทะเบียน</dt>
          <dd class="col-7 mb-2"><?= date('d M Y H:i', strtotime($user['created_at'])) ?></dd>

          <dt class="col-5 text-muted mb-0">เข้าสู่ระบบล่าสุด</dt>
          <dd class="col-7 mb-0"><?= $user['last_login_at'] ? date('d M Y H:i', strtotime($user['last_login_at'])) : '—' ?></dd>
        </dl>
      </div>
    </div>
  </div>

  <!-- Right History & Activity Column -->
  <div class="col-lg-7">
    <!-- Login History Card -->
    <div class="admin-table-container">
      <div class="admin-table-header">
        <div class="d-flex align-items-center gap-2">
          <i class="bi bi-clock-history text-primary"></i>
          <span class="fw-bold text-dark" style="font-size: 0.9rem;">ประวัติการเข้าสู่ระบบล่าสุด (Login History)</span>
        </div>
      </div>
      <div class="table-responsive">
        <table class="admin-table">
          <thead>
            <tr>
              <th>เวลาที่เข้าใช้งาน</th>
              <th>IP Address</th>
              <th>อุปกรณ์ / เบราว์เซอร์</th>
              <th>ผลลัพธ์</th>
            </tr>
          </thead>
          <tbody>
            <?php if (empty($user['login_history'])): ?>
            <tr>
              <td colspan="4" class="text-center text-muted py-3">ไม่มีบันทึกประวัติการเข้าสู่ระบบ</td>
            </tr>
            <?php else: foreach ($user['login_history'] as $h): ?>
            <tr>
              <td class="text-muted small"><?= date('d M Y H:i', strtotime($h['created_at'])) ?></td>
              <td class="font-monospace small"><?= e($h['ip_address'] ?? '—') ?></td>
              <td class="text-muted small" style="max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">
                <?= e($h['user_agent'] ?? '—') ?>
              </td>
              <td>
                <?php if (!empty($h['success'])): ?>
                  <span class="status-pill status-active">สำเร็จ</span>
                <?php else: ?>
                  <span class="status-pill status-danger">ล้มเหลว</span>
                <?php endif; ?>
              </td>
            </tr>
            <?php endforeach; endif; ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>

<!-- ── Suspend User Confirmation Modal ─────────────────── -->
<?php if (!$isSelf && !$isSuperAdmin && AdminMiddleware::can('user.suspend')): ?>
<div class="modal fade" id="suspendUserModal" tabindex="-1" aria-labelledby="suspendUserModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <form action="<?= url('/admin/users/' . (int)$user['id'] . '/suspend') ?>" method="POST">
        <?= Security::csrfField() ?>
        <div class="modal-header bg-danger text-white py-3">
          <h6 class="modal-title fw-bold" id="suspendUserModalLabel">
            <i class="bi bi-exclamation-triangle-fill me-2"></i>ยืนยันการระงับบัญชีผู้ใช้
          </h6>
          <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body p-4 small">
          <p class="mb-3 text-dark">
            คุณกำลังจะระงับบัญชีผู้ใช้งาน <strong><?= e($user['first_name'] . ' ' . $user['last_name']) ?> (@<?= e($user['username']) ?>)</strong>
          </p>
          <div class="alert alert-warning border-warning small mb-0">
            <strong>ผลกระทบของการระงับ:</strong>
            <ul class="mb-0 ps-3 mt-1 text-dark">
              <li>ผู้ใช้จะไม่สามารถเข้าสู่ระบบหรือทำธุรกรรมใด ๆ บนแพลตฟอร์มได้</li>
              <li>หากผู้ใช้มีร้านค้า ร้านค้าและสินค้าจะถูกระงับการแสดงผลชั่วคราว</li>
            </ul>
          </div>
        </div>
        <div class="modal-footer py-2 bg-light">
          <button type="button" class="btn btn-sm btn-secondary" data-bs-dismiss="modal">ยกเลิก</button>
          <button type="submit" class="btn btn-sm btn-danger px-3">
            <i class="bi bi-slash-circle me-1"></i>ยืนยันระงับบัญชี
          </button>
        </div>
      </form>
    </div>
  </div>
</div>
<?php endif; ?>

