<?php
// admin/users-content.php - محتوای مدیریت کاربران
?>

<!-- ============================================================
     پیام‌ها
     ============================================================ -->
<?php if ($message): ?>
<div class="alert alert-<?= strpos($message, 'خطا') !== false ? 'alert-danger' : 'alert-success' ?>">
    <i class="fas <?= strpos($message, 'خطا') !== false ? 'fa-exclamation-circle' : 'fa-check-circle' ?>"></i>
    <?= $message ?>
</div>
<?php endif; ?>

<!-- ============================================================
     هدر و دکمه افزودن
     ============================================================ -->
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px;flex-wrap:wrap;gap:12px;">
    <h3 style="font-size:16px;font-weight:700;color:#1f2937;margin:0;display:flex;align-items:center;gap:8px;">
        <i class="fas fa-users-cog" style="color:#355C9C;"></i>
        لیست کاربران (مدیران، کارشناسان، ...)
    </h3>
    <a href="teacher-edit.php?action=create" class="btn btn-success" style="padding:8px 20px;border:none;border-radius:8px;font-size:13px;font-weight:600;cursor:pointer;background:#10b981;color:white;transition:all 0.3s;text-decoration:none;display:inline-flex;align-items:center;gap:6px;">
        <i class="fas fa-plus-circle"></i> افزودن کاربر جدید
    </a>
</div>

<!-- ============================================================
     جدول کاربران
     ============================================================ -->
<div class="card" style="padding:0;overflow:hidden;background:#fff;border-radius:12px;border:1px solid #e5e7eb;">
    <div class="table-responsive" style="overflow-x:auto;">
        <table style="width:100%;border-collapse:collapse;font-size:14px;">
            <thead>
                <tr style="background:#f8fafc;border-bottom:2px solid #e5e7eb;">
                    <th style="padding:12px 16px;text-align:center;color:#6b7280;font-weight:600;font-size:13px;width:50px;">#</th>
                    <th style="padding:12px 16px;text-align:right;color:#6b7280;font-weight:600;font-size:13px;">نام</th>
                    <th style="padding:12px 16px;text-align:center;color:#6b7280;font-weight:600;font-size:13px;">کد ملی</th>
                    <th style="padding:12px 16px;text-align:center;color:#6b7280;font-weight:600;font-size:13px;">موبایل</th>
                    <th style="padding:12px 16px;text-align:center;color:#6b7280;font-weight:600;font-size:13px;">نقش</th>
                    <th style="padding:12px 16px;text-align:center;color:#6b7280;font-weight:600;font-size:13px;width:100px;">وضعیت</th>
                    <th style="padding:12px 16px;text-align:center;color:#6b7280;font-weight:600;font-size:13px;width:120px;">عملیات</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($users)): ?>
                <tr>
                    <td colspan="7" style="padding:40px;text-align:center;color:#9ca3af;">هیچ کاربری یافت نشد</td>
                </tr>
                <?php else: ?>
                <?php $i = 1; foreach ($users as $user): ?>
                <tr style="border-bottom:1px solid #f3f4f6;" class="table-row-hover">
                    <td style="padding:10px 16px;text-align:center;"><?= $i++ ?></td>
                    
                    <td style="padding:10px 16px;">
                        <div style="display:flex;align-items:center;gap:10px;">
                            <div style="width:32px;height:32px;border-radius:50%;background:linear-gradient(135deg, #355C9C, #2FA7A3);display:flex;align-items:center;justify-content:center;color:white;font-weight:600;font-size:13px;">
                                <?= mb_substr($user['name'], 0, 1, 'UTF-8') ?>
                            </div>
                            <span style="font-weight:500;color:#1f2937;"><?= htmlspecialchars($user['name'] . ' ' . $user['family']) ?></span>
                        </div>
                    </td>
                    
                    <td style="padding:10px 16px;text-align:center;color:#6b7280;"><?= $user['national_code'] ?></td>
                    <td style="padding:10px 16px;text-align:center;color:#6b7280;"><?= $user['mobile'] ?></td>
                    
                    <td style="padding:10px 16px;text-align:center;">
                        <span style="background:#dbeafe;color:#1e40af;padding:2px 12px;border-radius:4px;font-size:12px;font-weight:500;">
                            <?= htmlspecialchars($user['role_name']) ?>
                        </span>
                    </td>
                    
                    <td style="padding:10px 16px;text-align:center;">
                        <?php if ($user['is_active']): ?>
                            <span class="status-badge status-active">فعال</span>
                        <?php else: ?>
                            <span class="status-badge status-inactive">غیرفعال</span>
                        <?php endif; ?>
                    </td>
                    
                    <td style="padding:10px 16px;text-align:center;">
                        <div class="action-buttons">
                            <!-- ===== دکمه ویرایش ===== -->
                            <a href="teacher-edit.php?id=<?= $user['id'] ?>" class="btn-action btn-action-edit" title="ویرایش">
                                <i class="fas fa-edit"></i>
                            </a>
                            
                            <!-- ===== دکمه فعال/غیرفعال ===== -->
                            <form method="POST" style="display:inline;" onsubmit="return confirm('آیا از تغییر وضعیت کاربر مطمئن هستید؟')">
                                <input type="hidden" name="action" value="toggle_status">
                                <input type="hidden" name="user_id" value="<?= $user['id'] ?>">
                                <input type="hidden" name="current_status" value="<?= $user['is_active'] ?>">
                                <button type="submit" class="btn-action <?= $user['is_active'] ? 'btn-action-disable' : 'btn-action-enable' ?>" title="<?= $user['is_active'] ? 'غیرفعال کردن' : 'فعال کردن' ?>">
                                    <i class="fas <?= $user['is_active'] ? 'fa-ban' : 'fa-check' ?>"></i>
                                </button>
                            </form>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>