<?php
// admin/certificate-requests-content.php - محتوای مدیریت درخواست‌ها
?>

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

<!-- ============================================================
     لیست درخواست‌ها
     ============================================================ -->
<div class="card">
    <h4 style="font-size:17px;font-weight:700;color:#1f2937;margin-bottom:16px;display:flex;align-items:center;gap:8px;">
        <i class="fas fa-list" style="color:#1a237e;"></i>
        لیست درخواست‌های گواهی
        <span style="font-size:13px;color:#9ca3af;font-weight:400;">(<?= count($requests) ?> درخواست)</span>
    </h4>
    
    <div class="table-responsive">
        <table style="width:100%;border-collapse:collapse;font-size:13px;">
            <thead>
                <tr style="background:#f8fafc;border-bottom:2px solid #e5e7eb;">
                    <th style="padding:8px 12px;text-align:right;color:#6b7280;font-weight:600;">#</th>
                    <th style="padding:8px 12px;text-align:right;color:#6b7280;font-weight:600;">استاد</th>
                    <th style="padding:8px 12px;text-align:right;color:#6b7280;font-weight:600;">ترم</th>
                    <th style="padding:8px 12px;text-align:right;color:#6b7280;font-weight:600;">عنوان</th>
                    <th style="padding:8px 12px;text-align:center;color:#6b7280;font-weight:600;">وضعیت</th>
                    <th style="padding:8px 12px;text-align:center;color:#6b7280;font-weight:600;">تاریخ درخواست</th>
                    <th style="padding:8px 12px;text-align:center;color:#6b7280;font-weight:600;min-width:200px;">عملیات</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($requests)): ?>
                    <tr><td colspan="7" style="padding:30px;text-align:center;color:#9ca3af;">هیچ درخواستی ثبت نشده است.</td></tr>
                <?php else: ?>
                    <?php $i = 1; foreach ($requests as $req): ?>
                    <tr style="border-bottom:1px solid #f3f4f6;">
                        <td style="padding:8px 12px;"><?= $i++ ?></td>
                        <td style="padding:8px 12px;">
                            <strong><?= htmlspecialchars($req['teacher_name'] . ' ' . $req['teacher_family']) ?></strong>
                            <div style="font-size:11px;color:#9ca3af;"><?= $req['national_code'] ?></div>
                        </td>
                        <td style="padding:8px 12px;"><?= htmlspecialchars($req['term_name'] ?? '-') ?></td>
                        <td style="padding:8px 12px;max-width:150px;"><?= htmlspecialchars($req['title'] ?? '-') ?></td>
                        <td style="padding:8px 12px;text-align:center;">
                            <?php if ($req['status'] == 'pending'): ?>
                                <span style="background:#fef3c7;color:#92400e;padding:2px 12px;border-radius:20px;font-size:12px;font-weight:500;">⏳ در انتظار</span>
                            <?php elseif ($req['status'] == 'approved'): ?>
                                <span style="background:#d1fae5;color:#065f46;padding:2px 12px;border-radius:20px;font-size:12px;font-weight:500;">✅ تأیید شده</span>
                            <?php else: ?>
                                <span style="background:#fecaca;color:#991b1b;padding:2px 12px;border-radius:20px;font-size:12px;font-weight:500;">❌ رد شده</span>
                            <?php endif; ?>
                        </td>
                        <td style="padding:8px 12px;text-align:center;"><?= jalaliDate($req['request_date']) ?></td>
                        <td style="padding:8px 12px;text-align:center;">
                            <?php if ($req['status'] == 'pending'): ?>
                                <!-- ===== فرم تأیید ===== -->
                                <form method="POST" style="display:inline-block;" onsubmit="return confirm('آیا از تأیید این گواهی مطمئن هستید؟')">
                                    <input type="hidden" name="action" value="approve">
                                    <input type="hidden" name="request_id" value="<?= $req['id'] ?>">
                                    <div style="display:flex;flex-wrap:wrap;gap:4px;align-items:center;justify-content:center;">
                                        <input type="text" name="letter_number" placeholder="شماره نامه" 
                                               style="width:100px;padding:4px 8px;border:1px solid #e5e7eb;border-radius:4px;font-size:12px;" required>
                                        <input type="text" name="approval_date" placeholder="تاریخ (1404/07/01)" 
                                               style="width:100px;padding:4px 8px;border:1px solid #e5e7eb;border-radius:4px;font-size:12px;" required>
                                        <button type="submit" class="btn btn-success btn-sm" style="padding:4px 10px;font-size:12px;border-radius:4px;border:none;cursor:pointer;background:#10b981;color:white;">
                                            <i class="fas fa-check"></i>
                                        </button>
                                    </div>
                                </form>
                                <!-- ===== دکمه رد ===== -->
                                <form method="POST" style="display:inline-block;" onsubmit="return confirm('آیا از رد این گواهی مطمئن هستید؟')">
                                    <input type="hidden" name="action" value="reject">
                                    <input type="hidden" name="request_id" value="<?= $req['id'] ?>">
                                    <button type="submit" class="btn btn-danger btn-sm" style="padding:4px 10px;font-size:12px;border-radius:4px;border:none;cursor:pointer;background:#dc2626;color:white;">
                                        <i class="fas fa-times"></i>
                                    </button>
                                </form>
                            <?php elseif ($req['status'] == 'approved'): ?>
                                <span style="font-size:12px;color:#6b7280;">
                                    شماره: <?= htmlspecialchars($req['letter_number']) ?>
                                    <br>
                                    تاریخ: <?= jalaliDate($req['approval_date']) ?>
                                    <br>
                                    تأیید: <?= htmlspecialchars($req['approver_name'] . ' ' . $req['approver_family']) ?>
                                </span>
                            <?php else: ?>
                                <span style="font-size:12px;color:#9ca3af;">رد شده</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<style>
.alert {
    padding:12px 16px;
    border-radius:8px;
    margin-bottom:16px;
}
.alert-danger {
    background:#fecaca;
    color:#991b1b;
    border:1px solid #fca5a5;
}
.alert-success {
    background:#d1fae5;
    color:#065f46;
    border:1px solid #a7f3d0;
}
</style>