<?php
// admin/announcement-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" style="margin-bottom:24px;">
    <h4 style="font-size:17px;font-weight:700;color:#1f2937;margin-bottom:16px;display:flex;align-items:center;gap:8px;">
        <i class="fas fa-plus-circle" style="color:#1a237e;"></i>
        افزودن اعلان جدید
    </h4>
    
    <form method="POST" action="">
        <input type="hidden" name="action" value="add">
        
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:16px;">
            <div class="form-group">
                <label style="display:block;font-weight:600;color:#374151;margin-bottom:5px;font-size:14px;">
                    ترم <span style="color:#dc2626;">*</span>
                </label>
                <select name="term_id" class="form-control" style="width:100%;padding:10px 14px;border:2px solid #e5e7eb;border-radius:8px;font-size:14px;background:white;" required>
                    <option value="">انتخاب ترم...</option>
                    <?php foreach ($allTerms as $t): ?>
                        <option value="<?= $t['id'] ?>"><?= htmlspecialchars($t['name']) ?> (<?= htmlspecialchars($t['code']) ?>)</option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group">
                <label style="display:block;font-weight:600;color:#374151;margin-bottom:5px;font-size:14px;">
                    عنوان (اختیاری)
                </label>
                <input type="text" name="title" class="form-control" 
                       placeholder="مثال: اطلاعیه مهم" 
                       style="width:100%;padding:10px 14px;border:2px solid #e5e7eb;border-radius:8px;font-size:14px;">
            </div>
        </div>
        
        <div class="form-group" style="margin-top:12px;">
            <label style="display:block;font-weight:600;color:#374151;margin-bottom:5px;font-size:14px;">
                متن اعلان <span style="color:#dc2626;">*</span>
            </label>
            <textarea name="content" class="form-control" rows="3" 
                      placeholder="متن اعلان را وارد کنید..." 
                      style="width:100%;padding:10px 14px;border:2px solid #e5e7eb;border-radius:8px;font-size:14px;resize:vertical;" required></textarea>
        </div>
        
        <div class="form-group" style="margin-top:8px;">
            <label style="display:flex;align-items:center;gap:8px;font-weight:600;color:#374151;font-size:14px;cursor:pointer;">
                <input type="checkbox" name="is_active" value="1" checked>
                فعال (نمایش داده شود)
            </label>
        </div>
        
        <button type="submit" class="btn btn-primary" style="padding:10px 28px;border:none;border-radius:8px;font-size:14px;font-weight:600;cursor:pointer;background:#1a237e;color:white;transition:all 0.3s;margin-top:8px;">
            <i class="fas fa-save"></i> ثبت اعلان
        </button>
    </form>
</div>

<!-- ============================================================
     لیست اعلان‌های موجود
     ============================================================ -->
<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($announcements) ?> اعلان)</span>
    </h4>
    
    <?php if (empty($announcements)): ?>
        <div style="text-align:center;padding:30px;color:#9ca3af;">
            <i class="fas fa-info-circle" style="font-size:24px;display:block;margin-bottom:8px;"></i>
            هیچ اعلانی ثبت نشده است.
        </div>
    <?php else: ?>
        <div class="table-responsive">
            <table style="width:100%;border-collapse:collapse;font-size:14px;">
                <thead>
                    <tr style="background:#f8fafc;border-bottom:2px solid #e5e7eb;">
                        <th style="padding:10px 12px;text-align:right;color:#6b7280;font-weight:600;">#</th>
                        <th style="padding:10px 12px;text-align:right;color:#6b7280;font-weight:600;">ترم</th>
                        <th style="padding:10px 12px;text-align:right;color:#6b7280;font-weight:600;">عنوان</th>
                        <th style="padding:10px 12px;text-align:right;color:#6b7280;font-weight:600;">متن</th>
                        <th style="padding:10px 12px;text-align:center;color:#6b7280;font-weight:600;">وضعیت</th>
                        <th style="padding:10px 12px;text-align:center;color:#6b7280;font-weight:600;">تاریخ</th>
                        <th style="padding:10px 12px;text-align:center;color:#6b7280;font-weight:600;">عملیات</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $i = 1; foreach ($announcements as $item): ?>
                    <tr style="border-bottom:1px solid #f3f4f6;">
                        <td style="padding:8px 12px;"><?= $i++ ?></td>
                        <td style="padding:8px 12px;"><?= htmlspecialchars($item['term_name'] ?? '-') ?></td>
                        <td style="padding:8px 12px;"><?= htmlspecialchars($item['title'] ?? '-') ?></td>
                        <td style="padding:8px 12px;max-width:200px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">
                            <?= htmlspecialchars($item['content']) ?>
                        </td>
                        <td style="padding:8px 12px;text-align:center;">
                            <?php if ($item['is_active']): ?>
                                <span style="background:#d1fae5;color:#065f46;padding:2px 12px;border-radius:20px;font-size:12px;font-weight:500;">✅ فعال</span>
                            <?php else: ?>
                                <span style="background:#f3f4f6;color:#6b7280;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($item['created_at']) ?></td>
                        <td style="padding:8px 12px;text-align:center;">
                            <!-- ===== دکمه فعال/غیرفعال ===== -->
                            <form method="POST" style="display:inline-block;">
                                <input type="hidden" name="action" value="toggle_status">
                                <input type="hidden" name="id" value="<?= $item['id'] ?>">
                                <input type="hidden" name="current_status" value="<?= $item['is_active'] ?>">
                                <button type="submit" class="btn btn-sm <?= $item['is_active'] ? 'btn-warning' : 'btn-success' ?>" 
                                        style="padding:2px 10px;font-size:11px;border-radius:4px;border:none;cursor:pointer;<?= $item['is_active'] ? 'background:#d97706;color:white;' : 'background:#10b981;color:white;' ?>">
                                    <i class="fas <?= $item['is_active'] ? 'fa-pause' : 'fa-play' ?>"></i>
                                    <?= $item['is_active'] ? 'غیرفعال' : 'فعال' ?>
                                </button>
                            </form>
                            <!-- ===== دکمه حذف ===== -->
                            <form method="POST" style="display:inline-block;" onsubmit="return confirm('آیا از حذف این اعلان مطمئن هستید؟')">
                                <input type="hidden" name="action" value="delete">
                                <input type="hidden" name="id" value="<?= $item['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"></i>
                                </button>
                            </form>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</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;
}
.form-control:focus {
    border-color: #1a237e !important;
    box-shadow: 0 0 0 3px rgba(26,35,126,0.1) !important;
    outline: none;
}
.btn-warning:hover {
    background:#b45309 !important;
}
.btn-success:hover {
    background:#059669 !important;
}
.btn-danger:hover {
    background:#b91c1c !important;
}
</style>