<?php
// admin/availability-review-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="filter-section" style="margin-bottom:20px;padding:16px 20px;background:#fff;border-radius:12px;border:1px solid #e5e7eb;">
    <form method="GET" style="display:flex;flex-wrap:wrap;gap:12px;align-items:center;">
        <div>
            <label style="font-size:13px;font-weight:600;display:block;margin-bottom:4px;color:#6b7280;">وضعیت</label>
            <select name="status" class="form-control" style="padding:8px 12px;border-radius:8px;border:2px solid #e5e7eb;background:white;">
                <option value="all" <?= $statusFilter == 'all' ? 'selected' : '' ?>>همه</option>
                <option value="pending" <?= $statusFilter == 'pending' ? 'selected' : '' ?>>در انتظار</option>
                <option value="approved" <?= $statusFilter == 'approved' ? 'selected' : '' ?>>تأیید شده</option>
                <option value="rejected" <?= $statusFilter == 'rejected' ? 'selected' : '' ?>>رد شده</option>
                <option value="needs_revision" <?= $statusFilter == 'needs_revision' ? 'selected' : '' ?>>نیاز به اصلاح</option>
            </select>
        </div>
        <div>
            <label style="font-size:13px;font-weight:600;display:block;margin-bottom:4px;color:#6b7280;">ترم</label>
            <select name="term_id" class="form-control" style="padding:8px 12px;border-radius:8px;border:2px solid #e5e7eb;background:white;">
                <option value="0">همه ترم‌ها</option>
                <?php foreach ($allTerms as $t): ?>
                    <option value="<?= $t['id'] ?>" <?= $termFilter == $t['id'] ? 'selected' : '' ?>>
                        <?= htmlspecialchars($t['name']) ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </div>
        <div>
            <label style="font-size:13px;font-weight:600;display:block;margin-bottom:4px;color:#6b7280;">استاد</label>
            <select name="teacher_id" class="form-control" style="padding:8px 12px;border-radius:8px;border:2px solid #e5e7eb;background:white;">
                <option value="0">همه اساتید</option>
                <?php foreach ($allTeachers as $t): ?>
                    <option value="<?= $t['id'] ?>" <?= $teacherFilter == $t['id'] ? 'selected' : '' ?>>
                        <?= htmlspecialchars($t['name'] . ' ' . $t['family']) ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="padding-top:22px;display:flex;gap:8px;">
            <button type="submit" class="btn btn-primary" style="padding:8px 20px;border:none;border-radius:8px;font-size:14px;font-weight:600;cursor:pointer;background:#1a237e;color:white;transition:all 0.3s;">
                <i class="fas fa-filter"></i> اعمال فیلتر
            </button>
            <a href="availability-review.php" class="btn btn-outline" style="padding:8px 20px;border-radius:8px;font-size:14px;font-weight:600;cursor:pointer;text-decoration:none;background:transparent;color:#6b7280;border:2px solid #e5e7eb;transition:all 0.3s;display:inline-flex;align-items:center;gap:6px;">
                <i class="fas fa-redo"></i> بازنشانی
            </a>
            <a href="availability-export.php<?= $teacherFilter > 0 ? '?teacher_id=' . $teacherFilter : '' ?>" 
               class="btn btn-success" target="_blank" style="padding:8px 20px;border:none;border-radius:8px;font-size:14px;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-file-excel"></i> خروجی اکسل
            </a>
            <a href="availability-export.php<?= $teacherFilter > 0 ? '?teacher_id=' . $teacherFilter . '&print=1' : '?print=1' ?>" 
               class="btn btn-primary" target="_blank" style="padding:8px 20px;border:none;border-radius:8px;font-size:14px;font-weight:600;cursor:pointer;background:#1a237e;color:white;transition:all 0.3s;text-decoration:none;display:inline-flex;align-items:center;gap:6px;">
                <i class="fas fa-print"></i> پرینت
            </a>
        </div>
    </form>
</div>

<!-- ============================================================
     لیست درخواست‌ها
     ============================================================ -->
<?php if (empty($groupedRequests)): ?>
<div class="card" style="text-align:center;padding:60px 20px;">
    <i class="fas fa-inbox" style="font-size:48px;color:#9ca3af;display:block;margin-bottom:16px;opacity:0.3;"></i>
    <p style="font-size:18px;color:#6b7280;">هیچ درخواستی برای نمایش وجود ندارد.</p>
</div>
<?php else: ?>
    <?php foreach ($groupedRequests as $key => $item): ?>
    <div class="card" style="margin-bottom:20px;padding:0;overflow:hidden;border:1px solid <?= 
        $item['status'] == 'approved' ? '#86efac' : 
        ($item['status'] == 'pending' ? '#fcd34d' : 
        ($item['status'] == 'needs_revision' ? '#fcd34d' : '#fca5a5')) ?>;">
        
        <!-- ============================================================
             هدر اطلاعات استاد
             ============================================================ -->
        <div style="padding:14px 20px;background:<?= 
            $item['status'] == 'approved' ? '#d1fae5' : 
            ($item['status'] == 'pending' ? '#fef3c7' : 
            ($item['status'] == 'needs_revision' ? '#fef3c7' : '#fecaca')) ?>;border-bottom:2px solid <?= 
            $item['status'] == 'approved' ? '#86efac' : 
            ($item['status'] == 'pending' ? '#fcd34d' : 
            ($item['status'] == 'needs_revision' ? '#fcd34d' : '#fca5a5')) ?>;">
            
            <div style="display:flex;flex-wrap:wrap;justify-content:space-between;align-items:center;gap:8px;">
                <div>
                    <span style="font-weight:700;font-size:16px;color:#1f2937;">
                        <?= htmlspecialchars($item['teacher_name'] . ' ' . $item['teacher_family']) ?>
                    </span>
                    <span style="font-size:13px;color:#6b7280;margin-right:12px;">
                        کد ملی: <?= $item['national_code'] ?>
                    </span>
                    <?php if ($item['field_of_study']): ?>
                        <span style="font-size:13px;color:#6b7280;margin-right:12px;">
                            رشته: <?= htmlspecialchars($item['field_of_study']) ?>
                        </span>
                    <?php endif; ?>
                    <?php if ($item['degree']): ?>
                        <span style="font-size:13px;color:#6b7280;margin-right:12px;">
                            مدرک: <?= htmlspecialchars($item['degree']) ?>
                    <?php endif; ?>
                    <span style="font-size:13px;color:#6b7280;margin-right:12px;">
                        ترم: <?= htmlspecialchars($item['term_name']) ?>
                    </span>
                </div>
                <div style="display:flex;align-items:center;gap:8px;">
                    <?php if ($item['status'] == 'approved'): ?>
                        <span style="background:#d1fae5;color:#065f46;padding:2px 14px;border-radius:20px;font-size:12px;font-weight:500;">✅ تأیید شده</span>
                    <?php elseif ($item['status'] == 'pending'): ?>
                        <span style="background:#fef3c7;color:#92400e;padding:2px 14px;border-radius:20px;font-size:12px;font-weight:500;">⏳ در انتظار</span>
                    <?php elseif ($item['status'] == 'needs_revision'): ?>
                        <span style="background:#fef3c7;color:#92400e;padding:2px 14px;border-radius:20px;font-size:12px;font-weight:500;">✏️ نیاز به اصلاح</span>
                    <?php else: ?>
                        <span style="background:#fecaca;color:#991b1b;padding:2px 14px;border-radius:20px;font-size:12px;font-weight:500;">❌ رد شده</span>
                    <?php endif; ?>
                    
                    <!-- ===== دکمه حذف همه زمان‌های استاد ===== -->
                    <form method="POST" style="display:inline-block;" onsubmit="return confirm('آیا از حذف همه زمان‌های این استاد مطمئن هستید؟')">
                        <input type="hidden" name="action" value="delete_all">
                        <input type="hidden" name="teacher_id" value="<?= $item['teacher_id'] ?>">
                        <input type="hidden" name="term_id" value="<?= $item['term_id'] ?>">
                        <button type="submit" class="btn btn-danger btn-sm" style="padding:2px 10px;font-size:11px;border-radius:4px;border:none;cursor:pointer;background:#dc2626;color:white;">
                            <i class="fas fa-trash-alt"></i> حذف همه
                        </button>
                    </form>
                </div>
            </div>
            
            <!-- ===== دروس مورد علاقه ===== -->
            <?php if ($item['preferred_courses']): ?>
            <div style="margin-top:6px;font-size:13px;color:#4b5563;">
                <span style="font-weight:600;">دروس مورد علاقه:</span>
                <?= htmlspecialchars($item['preferred_courses']) ?>
            </div>
            <?php endif; ?>
        </div>
        
        <!-- ============================================================
             جدول زمان‌ها
             ============================================================ -->
        <div style="padding:16px 20px;">
            <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: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;">۱۲-۱۴</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;">۱۸-۲۰</th>
                            <th style="padding:8px 12px;text-align:center;color:#6b7280;font-weight:600;width:70px;">عملیات</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php 
                        $days = [
                            'saturday' => 'شنبه',
                            'sunday' => 'یک‌شنبه',
                            'monday' => 'دوشنبه',
                            'tuesday' => 'سه‌شنبه',
                            'wednesday' => 'چهارشنبه',
                            'thursday' => 'پنج‌شنبه'
                        ];
                        $slots = ['08-10', '10-12', '12-14', '14-16', '16-18', '18-20'];
                        
                        $slotMap = [];
                        $slotIds = [];
                        foreach ($item['slots'] as $slot) {
                            $slotMap[$slot['day_of_week']][$slot['time_slot']] = true;
                            $slotIds[$slot['day_of_week']][$slot['time_slot']] = $slot['id'];
                        }
                        
                        foreach ($days as $dayKey => $dayLabel):
                        ?>
                        <tr style="border-bottom:1px solid #f3f4f6;">
                            <td style="padding:8px 12px;font-weight:600;color:#1f2937;"><?= $dayLabel ?></td>
                            <?php foreach ($slots as $slot): ?>
                            <td style="padding:8px 12px;text-align:center;">
                                <?php if (isset($slotMap[$dayKey][$slot])): ?>
                                    <span style="color:#059669;">✅</span>
                                <?php else: ?>
                                    <span style="color:#d1d5db;">—</span>
                                <?php endif; ?>
                            </td>
                            <?php endforeach; ?>
                            <td style="padding:8px 12px;text-align:center;">
                                <?php if (isset($slotMap[$dayKey])): ?>
                                    <form method="POST" style="display:inline-block;" onsubmit="return confirm('آیا از حذف این روز مطمئن هستید؟')">
                                        <input type="hidden" name="action" value="delete_slot">
                                        <input type="hidden" name="slot_id" value="<?= reset($slotIds[$dayKey]) ?>">
                                        <button type="submit" class="btn btn-danger btn-sm" style="padding:2px 6px;font-size:10px;border-radius:4px;border:none;cursor:pointer;background:#dc2626;color:white;">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                    </form>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            
            <!-- ===== نظر مدیر ===== -->
            <?php if ($item['review_comment']): ?>
                <div style="margin-top:8px;padding:8px 12px;background:#f8fafc;border-radius:6px;border:1px solid #e5e7eb;">
                    <span style="font-weight:600;color:#6b7280;">📝 نظر مدیر:</span>
                    <span style="color:#1f2937;"><?= htmlspecialchars($item['review_comment']) ?></span>
                    <?php if ($item['reviewed_at']): ?>
                        <span style="font-size:11px;color:#9ca3af;margin-right:8px;">
                            (<?= jalaliDate($item['reviewed_at']) ?>)
                        </span>
                    <?php endif; ?>
                </div>
            <?php endif; ?>
            
            <!-- ============================================================
                 دکمه‌های عملیات
                 ============================================================ -->
            <?php if ($item['status'] == 'pending' || $item['status'] == 'needs_revision'): ?>
            <div style="margin-top:12px;display:flex;flex-wrap:wrap;gap:8px;padding-top:12px;border-top:1px solid #e5e7eb;">
                <form method="POST" style="display:inline-block;" onsubmit="return confirm('آیا از تأیید این زمان‌ها مطمئن هستید؟')">
                    <input type="hidden" name="action" value="approve">
                    <input type="hidden" name="teacher_id" value="<?= $item['teacher_id'] ?>">
                    <input type="hidden" name="term_id" value="<?= $item['term_id'] ?>">
                    <input type="text" name="comment" placeholder="نظر (اختیاری)" 
                           style="padding:4px 10px;border:1px solid #e5e7eb;border-radius:4px;font-size:12px;width:150px;">
                    <button type="submit" class="btn btn-success btn-sm" style="padding:4px 14px;font-size:12px;border-radius:4px;border:none;cursor:pointer;background:#10b981;color:white;">
                        <i class="fas fa-check"></i> تأیید
                    </button>
                </form>
                
                <form method="POST" style="display:inline-block;" onsubmit="return confirm('آیا از رد این زمان‌ها مطمئن هستید؟')">
                    <input type="hidden" name="action" value="reject">
                    <input type="hidden" name="teacher_id" value="<?= $item['teacher_id'] ?>">
                    <input type="hidden" name="term_id" value="<?= $item['term_id'] ?>">
                    <input type="text" name="comment" placeholder="دلیل رد" 
                           style="padding:4px 10px;border:1px solid #e5e7eb;border-radius:4px;font-size:12px;width:150px;" required>
                    <button type="submit" class="btn btn-danger btn-sm" style="padding:4px 14px;font-size:12px;border-radius:4px;border:none;cursor:pointer;background:#dc2626;color:white;">
                        <i class="fas fa-times"></i> رد
                    </button>
                </form>
                
                <form method="POST" style="display:inline-block;" onsubmit="return confirm('آیا از ارسال درخواست اصلاح مطمئن هستید؟')">
                    <input type="hidden" name="action" value="needs_revision">
                    <input type="hidden" name="teacher_id" value="<?= $item['teacher_id'] ?>">
                    <input type="hidden" name="term_id" value="<?= $item['term_id'] ?>">
                    <input type="text" name="comment" placeholder="توضیح اصلاحات" 
                           style="padding:4px 10px;border:1px solid #e5e7eb;border-radius:4px;font-size:12px;width:150px;" required>
                    <button type="submit" class="btn btn-warning btn-sm" style="padding:4px 14px;font-size:12px;border-radius:4px;border:none;cursor:pointer;background:#d97706;color:white;">
                        <i class="fas fa-edit"></i> درخواست اصلاح
                    </button>
                </form>
            </div>
            <?php endif; ?>
            
        </div>
    </div>
    <?php endforeach; ?>
<?php endif; ?>

<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;
}
.btn-warning {
    background:#d97706;
    color:white;
}
.btn-warning:hover {
    background:#b45309;
}
.form-control:focus {
    border-color: #1a237e !important;
    box-shadow: 0 0 0 3px rgba(26,35,126,0.1) !important;
    outline: none;
}
</style>