<?php
// admin/terms-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 class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 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(count($terms)) ?></h3>
            </div>
            <div class="w-12 h-12 bg-white/20 rounded-xl flex items-center justify-center">
                <i class="fas fa-calendar-alt 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(count(array_filter($terms, function($t) { return $t['is_active'] == 1; }))) ?></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(count(array_filter($terms, function($t) { return $t['is_active'] == 0; }))) ?></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>

<!-- ===== دو ستون ===== -->
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
    
    <!-- ===== ستون چپ: فرم افزودن ترم جدید ===== -->
    <div class="card" style="padding:20px 24px;background:#fff;border-radius:12px;border:1px solid #e5e7eb;">
        <h4 class="font-bold text-gray-700 mb-4 flex items-center gap-2" style="font-size:16px;">
            <i class="fas fa-plus-circle text-green-500"></i>
            افزودن ترم جدید
        </h4>
        <form method="POST" action="">
            <input type="hidden" name="action" value="add">
            
            <div class="form-group" style="margin-bottom:16px;">
                <label style="display:block;font-weight:600;color:#374151;margin-bottom:5px;font-size:14px;">نام ترم</label>
                <input type="text" name="name" placeholder="مثال: ترم مهر ۱۴۰۴" 
                       class="form-control" 
                       style="width:100%;padding:10px 14px;border:2px solid #e5e7eb;border-radius:8px;font-size:14px;" required>
            </div>
            
            <div class="form-group" style="margin-bottom:16px;">
                <label style="display:block;font-weight:600;color:#374151;margin-bottom:5px;font-size:14px;">کد ترم</label>
                <input type="text" name="code" placeholder="مثال: 4041" 
                       class="form-control" 
                       style="width:100%;padding:10px 14px;border:2px solid #e5e7eb;border-radius:8px;font-size:14px;" required>
                <small style="color:#9ca3af;font-size:12px;display:block;margin-top:4px;">کد ترم باید یکتا باشد</small>
            </div>
            
            <div style="display:grid;grid-template-columns:1fr 1fr;gap:12px;">
                <div class="form-group" style="margin-bottom:16px;">
                    <label style="display:block;font-weight:600;color:#374151;margin-bottom:5px;font-size:14px;">تاریخ شروع (شمسی)</label>
                    <input type="text" name="start_date_shamsi" placeholder="مثال: 1404/07/01" 
                           class="form-control" 
                           style="width:100%;padding:10px 14px;border:2px solid #e5e7eb;border-radius:8px;font-size:14px;" 
                           id="start_date_shamsi">
                    <small style="color:#9ca3af;font-size:12px;display:block;margin-top:4px;">فرمت: 1404/07/01</small>
                </div>
                
                <div class="form-group" style="margin-bottom:16px;">
                    <label style="display:block;font-weight:600;color:#374151;margin-bottom:5px;font-size:14px;">تاریخ پایان (شمسی)</label>
                    <input type="text" name="end_date_shamsi" placeholder="مثال: 1404/12/29" 
                           class="form-control" 
                           style="width:100%;padding:10px 14px;border:2px solid #e5e7eb;border-radius:8px;font-size:14px;" 
                           id="end_date_shamsi">
                    <small style="color:#9ca3af;font-size:12px;display:block;margin-top:4px;">فرمت: 1404/12/29</small>
                </div>
            </div>
            
            <div class="form-group" style="margin-bottom:16px;">
                <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-success" 
                    style="width:100%;padding:10px 24px;border:none;border-radius:8px;font-size:14px;font-weight:600;cursor:pointer;background:#10b981;color:white;transition:all 0.3s;display:inline-flex;align-items:center;justify-content:center;gap:8px;">
                <i class="fas fa-save"></i> افزودن ترم
            </button>
        </form>
    </div>
    
    <!-- ===== ستون راست: لیست ترم‌ها ===== -->
    <div class="card" style="padding:20px 24px;background:#fff;border-radius:12px;border:1px solid #e5e7eb;">
        <h4 class="font-bold text-gray-700 mb-4 flex items-center gap-2" style="font-size:16px;">
            <i class="fas fa-list text-blue-500"></i>
            لیست ترم‌ها
            <span class="text-sm text-gray-400 mr-2">(<?= persianNumber(count($terms)) ?> ترم)</span>
        </h4>
        
        <div class="space-y-3 max-h-[500px] overflow-y-auto">
            <?php if (empty($terms)): ?>
            <p class="text-gray-400 text-center py-8">هیچ ترمی ثبت نشده است</p>
            <?php else: ?>
            <?php foreach ($terms as $term): ?>
            <div class="term-item" style="background:#f8fafc;border-radius:10px;padding:14px 16px;border:1px solid <?= $term['is_active'] ? '#bfdbfe' : '#e5e7eb' ?>;transition:all 0.2s;">
                <form method="POST" action="" class="flex flex-wrap items-center gap-3">
                    <input type="hidden" name="action" value="edit">
                    <input type="hidden" name="id" value="<?= $term['id'] ?>">
                    
                    <div class="flex-1 min-w-[150px]">
                        <input type="text" name="name" value="<?= htmlspecialchars($term['name']) ?>" 
                               class="form-control" 
                               style="width:100%;padding:8px 12px;border:2px solid #e5e7eb;border-radius:6px;font-size:13px;" required>
                    </div>
                    
                    <div style="width:100px;">
                        <input type="text" name="code" value="<?= htmlspecialchars($term['code']) ?>" 
                               class="form-control" 
                               style="width:100%;padding:8px 12px;border:2px solid #e5e7eb;border-radius:6px;font-size:13px;text-align:center;" required>
                    </div>
                    
                    <div style="width:90px;">
                        <label style="display:flex;align-items:center;gap:6px;font-size:13px;cursor:pointer;color:#374151;">
                            <input type="checkbox" name="is_active" value="1" <?= $term['is_active'] ? 'checked' : '' ?>>
                            فعال
                        </label>
                    </div>
                    
                    <div class="action-buttons">
                        <button type="submit" class="btn-action btn-action-edit" title="ویرایش">
                            <i class="fas fa-save"></i>
                        </button>
                        
                        <button type="submit" class="btn-action btn-action-delete" title="حذف" 
                                onclick="this.form.action.value='delete'; return confirm('آیا از حذف ترم «<?= htmlspecialchars($term['name']) ?>» مطمئن هستید؟')">
                            <i class="fas fa-trash"></i>
                        </button>
                    </div>
                </form>
                
                <div style="font-size:11px;color:#9ca3af;margin-top:8px;padding-top:6px;border-top:1px solid #f1f5f9;display:flex;flex-wrap:wrap;gap:8px;align-items:center;">
                    <span>شناسه: <?= $term['id'] ?></span>
                    <span>|</span>
                    <span>کد: <?= htmlspecialchars($term['code']) ?></span>
                    <?php if ($term['start_date']): ?>
                    <span>|</span>
                    <span>شروع: <?= jalaliDate($term['start_date']) ?></span>
                    <?php endif; ?>
                    <?php if ($term['end_date']): ?>
                    <span>|</span>
                    <span>پایان: <?= jalaliDate($term['end_date']) ?></span>
                    <?php endif; ?>
                    <?php if ($term['is_active']): ?>
                    <span style="background:#d1fae5;color:#065f46;padding:2px 10px;border-radius:20px;font-size:10px;font-weight:600;">فعال</span>
                    <?php else: ?>
                    <span style="background:#fecaca;color:#991b1b;padding:2px 10px;border-radius:20px;font-size:10px;font-weight:600;">غیرفعال</span>
                    <?php endif; ?>
                </div>
            </div>
            <?php endforeach; ?>
            <?php endif; ?>
        </div>
    </div>
</div>