<?php
// admin/teachers-content.php - محتوای لیست مدرسین با دکمه حذف (فقط مدیر)
?>

<?php if ($message): ?>
<div class="alert alert-<?= strpos($message, '❌') !== false ? 'alert-danger' : 'alert-success' ?>">
    <?= $message ?>
</div>
<?php endif; ?>

<!-- ===== کارت‌های آمار ===== -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4 mb-6">
    <div class="bg-blue-500 text-white rounded-2xl p-5 shadow-lg">
        <div class="flex items-center justify-between">
            <div>
                <p class="text-blue-100 text-sm">تعداد کل مدرسین</p>
                <h3 class="text-2xl font-bold mt-1"><?= persianNumber(number_format($totalTeachers)) ?></h3>
            </div>
            <div class="w-12 h-12 bg-white/20 rounded-xl flex items-center justify-center">
                <i class="fas fa-users text-xl"></i>
            </div>
        </div>
    </div>
    
    <div class="bg-green-500 text-white rounded-2xl p-5 shadow-lg">
        <div class="flex items-center justify-between">
            <div>
                <p class="text-green-100 text-sm">✅ فعال</p>
                <h3 class="text-2xl font-bold mt-1"><?= persianNumber(number_format($activeTeachers)) ?></h3>
            </div>
            <div class="w-12 h-12 bg-white/20 rounded-xl flex items-center justify-center">
                <i class="fas fa-check-circle text-xl"></i>
            </div>
        </div>
    </div>
    
    <div class="bg-red-500 text-white rounded-2xl p-5 shadow-lg">
        <div class="flex items-center justify-between">
            <div>
                <p class="text-red-100 text-sm">❌ غیرفعال</p>
                <h3 class="text-2xl font-bold mt-1"><?= persianNumber(number_format($inactiveTeachers)) ?></h3>
            </div>
            <div class="w-12 h-12 bg-white/20 rounded-xl flex items-center justify-center">
                <i class="fas fa-times-circle text-xl"></i>
            </div>
        </div>
    </div>
    
    <div class="bg-purple-500 text-white rounded-2xl p-5 shadow-lg">
        <div class="flex items-center justify-between">
            <div>
                <p class="text-purple-100 text-sm">🏦 دارای شماره شبا</p>
                <h3 class="text-2xl font-bold mt-1"><?= persianNumber(number_format($teachersWithSheba)) ?></h3>
            </div>
            <div class="w-12 h-12 bg-white/20 rounded-xl flex items-center justify-center">
                <i class="fas fa-university text-xl"></i>
            </div>
        </div>
    </div>
</div>

<!-- ===== فیلترها و جستجو ===== -->
<div class="flex flex-wrap items-center justify-between gap-3 mb-4">
    <div class="flex flex-wrap gap-2">
        <a href="?status=all&term_filter=<?= $term_filter ?>&search=<?= urlencode($search) ?>" class="btn <?= $status_filter == 'all' ? 'btn-primary' : 'bg-gray-200 text-gray-700' ?> btn-sm">همه</a>
        <a href="?status=active&term_filter=<?= $term_filter ?>&search=<?= urlencode($search) ?>" class="btn <?= $status_filter == 'active' ? 'btn-success' : 'bg-gray-200 text-gray-700' ?> btn-sm">فعال</a>
        <a href="?status=inactive&term_filter=<?= $term_filter ?>&search=<?= urlencode($search) ?>" class="btn <?= $status_filter == 'inactive' ? 'btn-danger' : 'bg-gray-200 text-gray-700' ?> btn-sm">غیرفعال</a>
        <a href="?status=with_sheba&term_filter=<?= $term_filter ?>&search=<?= urlencode($search) ?>" class="btn <?= $status_filter == 'with_sheba' ? 'btn-success' : 'bg-gray-200 text-gray-700' ?> btn-sm">دارای شبا</a>
        <a href="?status=without_sheba&term_filter=<?= $term_filter ?>&search=<?= urlencode($search) ?>" class="btn <?= $status_filter == 'without_sheba' ? 'btn-warning' : 'bg-gray-200 text-gray-700' ?> btn-sm">بدون شبا</a>
        <a href="?status=pending_sheba&term_filter=<?= $term_filter ?>&search=<?= urlencode($search) ?>" class="btn <?= $status_filter == 'pending_sheba' ? 'btn-danger' : 'bg-gray-200 text-gray-700' ?> btn-sm">در انتظار تأیید</a>
    </div>
    
    <div class="flex flex-wrap gap-2">
        <form method="GET" class="flex gap-2">
            <input type="hidden" name="status" value="<?= $status_filter ?>">
            <input type="hidden" name="term_filter" value="<?= $term_filter ?>">
            <input type="text" name="search" placeholder="جستجوی نام، کد ملی، موبایل..." 
                   value="<?= htmlspecialchars($search) ?>" 
                   class="px-3 py-2 border rounded-lg text-sm w-48">
            <button type="submit" class="btn btn-primary btn-sm">
                <i class="fas fa-search"></i>
            </button>
            <?php if (!empty($search)): ?>
            <a href="?status=<?= $status_filter ?>&term_filter=<?= $term_filter ?>" class="btn btn-danger btn-sm">
                <i class="fas fa-times"></i>
            </a>
            <?php endif; ?>
        </form>
        <select name="term_filter" form="filterForm" class="px-3 py-2 border rounded-lg text-sm" onchange="this.form.submit()">
            <option value="0">همه ترم‌ها</option>
            <?php foreach ($allTerms as $term): ?>
            <option value="<?= $term['id'] ?>" <?= $term_filter == $term['id'] ? 'selected' : '' ?>>
                <?= htmlspecialchars($term['name']) ?>
            </option>
            <?php endforeach; ?>
        </select>
        <a href="teacher-edit.php?action=create" class="btn btn-primary btn-sm">
            <i class="fas fa-plus"></i> افزودن مدرس
        </a>
    </div>
</div>

<!-- ===== فرم مخفی برای فیلتر ترم ===== -->
<form id="filterForm" method="GET" style="display:none;">
    <input type="hidden" name="status" value="<?= $status_filter ?>">
    <input type="hidden" name="search" value="<?= htmlspecialchars($search) ?>">
</form>

<!-- ===== لیست مدرسین ===== -->
<div class="overflow-x-auto">
    <table class="w-full border">
        <thead>
            <tr class="bg-gray-100">
                <th class="p-2 text-right border" style="width:50px;">#</th>
                <th class="p-2 text-right border">نام</th>
                <th class="p-2 text-right border">کد ملی</th>
                <th class="p-2 text-right border">موبایل</th>
                <th class="p-2 text-right border">شماره شبا</th>
                <th class="p-2 text-right border" style="width:130px;">وضعیت شبا</th>
                <th class="p-2 text-right border" style="width:100px;">وضعیت</th>
                <th class="p-2 text-right border" style="width:280px;">عملیات</th>
            </tr>
        </thead>
        <tbody>
            <?php if (empty($teachers)): ?>
            <tr>
                <td colspan="8" class="text-center p-4 text-gray-400">هیچ مدرسی یافت نشد</td>
            </tr>
            <?php else: ?>
            <?php $i = 1; foreach ($teachers as $teacher): ?>
            <tr class="border-b hover:bg-gray-50">
                <td class="p-2 border text-center"><?= $i++ ?></td>
                
                <!-- ===== لینک نام استاد به دروس ===== -->
                <td class="p-2 border">
                    <a href="courses.php?teacher_id=<?= $teacher['id'] ?>" 
                       class="teacher-name-link" 
                       title="مشاهده دروس این استاد">
                        <?= htmlspecialchars($teacher['name'] . ' ' . $teacher['family']) ?>
                        <i class="fas fa-arrow-left" style="font-size:10px;opacity:0.6;margin-right:4px;"></i>
                    </a>
                </td>
                
                <td class="p-2 border"><?= $teacher['national_code'] ?></td>
                <td class="p-2 border"><?= $teacher['mobile'] ?></td>
                <td class="p-2 border"><?= $teacher['sheba_number'] ?? 'ثبت نشده' ?></td>
                
                <!-- ===== وضعیت شبا با رنگ کامل ===== -->
                <td class="p-2 border text-center">
                    <?php if (empty($teacher['sheba_number'])): ?>
                        <span class="status-sheba status-sheba-empty">ثبت نشده</span>
                    <?php elseif ($teacher['is_sheba_confirmed']): ?>
                        <span class="status-sheba status-sheba-confirmed">✅ تأیید شده</span>
                    <?php else: ?>
                        <span class="status-sheba status-sheba-pending">⏳ در انتظار تأیید</span>
                    <?php endif; ?>
                </td>
                
                <!-- ===== وضعیت با رنگ کامل ===== -->
                <td class="p-2 border text-center">
                    <?php if ($teacher['is_active']): ?>
                        <span class="status-badge status-active">فعال</span>
                    <?php else: ?>
                        <span class="status-badge status-inactive">غیرفعال</span>
                    <?php endif; ?>
                </td>
                
                <!-- ===== عملیات با سایز یکسان ===== -->
                <td class="p-2 border">
                    <div class="action-buttons">
                        <!-- ===== دکمه ویرایش ===== -->
                        <a href="teacher-edit.php?id=<?= $teacher['id'] ?>" class="btn-action btn-action-edit" title="ویرایش">
                            <i class="fas fa-edit"></i>
                        </a>
                        
                        <!-- ===== دکمه تأیید شماره شبا ===== -->
                        <?php if (!empty($teacher['sheba_number']) && !$teacher['is_sheba_confirmed'] && in_array($user['role_level'], [1,2])): ?>
                        <form method="POST" style="display:inline" onsubmit="return confirm('آیا از تأیید شماره شبا مطمئن هستید؟')">
                            <input type="hidden" name="action" value="confirm_sheba">
                            <input type="hidden" name="teacher_id" value="<?= $teacher['id'] ?>">
                            <button type="submit" class="btn-action btn-action-confirm" title="تأیید شبا">
                                <i class="fas fa-check"></i>
                            </button>
                        </form>
                        <?php endif; ?>
                        
                        <!-- ===== دکمه فعال/غیرفعال ===== -->
                        <form method="POST" style="display:inline" onsubmit="return confirm('آیا از تغییر وضعیت مدرس مطمئن هستید؟')">
                            <input type="hidden" name="action" value="toggle_status">
                            <input type="hidden" name="teacher_id" value="<?= $teacher['id'] ?>">
                            <input type="hidden" name="current_status" value="<?= $teacher['is_active'] ?>">
                            <button type="submit" class="btn-action <?= $teacher['is_active'] ? 'btn-action-disable' : 'btn-action-enable' ?>" title="<?= $teacher['is_active'] ? 'غیرفعال کردن' : 'فعال کردن' ?>">
                                <i class="fas <?= $teacher['is_active'] ? 'fa-ban' : 'fa-check' ?>"></i>
                            </button>
                        </form>
                        
                        <!-- ===== دکمه حذف کامل (فقط مدیر سامانه - سطح 1) ===== -->
                        <?php if ($user['role_level'] == 1): ?>
                        <form method="POST" style="display:inline" 
                              onsubmit="return confirm('⚠️ هشدار! آیا از حذف کامل این مدرس و تمام اطلاعات مرتبط (دروس، جلسات، تراکنش‌ها) مطمئن هستید؟ این عمل قابل بازگشت نیست!')">
                            <input type="hidden" name="action" value="delete_teacher">
                            <input type="hidden" name="teacher_id" value="<?= $teacher['id'] ?>">
                            <button type="submit" class="btn-action btn-action-delete" title="حذف کامل">
                                <i class="fas fa-trash-alt"></i>
                            </button>
                        </form>
                        <?php endif; ?>
                    </div>
                </td>
            </tr>
            <?php endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>
</div>