<?php
// admin/dashboard-content.php - محتوای داشبورد مدیریت با نمودار
?>

<!-- ============================================================
     کارت‌های آمار مدرسین
     ============================================================ -->
<div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(200px,1fr));gap:16px;margin-bottom:24px;">
    <div class="stat-card" style="background:linear-gradient(135deg, #355C9C, #4A7ABF);color:white;border-radius:14px;padding:18px 22px;box-shadow:0 4px 16px rgba(53,92,156,0.25);transition:transform 0.3s ease;">
        <div style="display:flex;justify-content:space-between;align-items:center;">
            <div>
                <div style="font-size:13px;opacity:0.8;">تعداد کل مدرسین</div>
                <div style="font-size:26px;font-weight:800;margin-top:4px;"><?= persianNumber(number_format($totalTeachers)) ?></div>
                <div style="font-size:11px;opacity:0.6;">همه اساتید ثبت‌نام شده</div>
            </div>
            <div style="width:44px;height:44px;background:rgba(255,255,255,0.15);border-radius:12px;display:flex;align-items:center;justify-content:center;font-size:20px;">
                <i class="fas fa-users"></i>
            </div>
        </div>
    </div>

    <a href="teachers.php?status=with_sheba" class="stat-card" style="display:block;background:linear-gradient(135deg, #059669, #10B981);color:white;border-radius:14px;padding:18px 22px;box-shadow:0 4px 16px rgba(5,150,105,0.25);transition:transform 0.3s ease;text-decoration:none;cursor:pointer;">
        <div style="display:flex;justify-content:space-between;align-items:center;">
            <div>
                <div style="font-size:13px;opacity:0.8;">✅ دارای شماره شبا</div>
                <div style="font-size:26px;font-weight:800;margin-top:4px;"><?= persianNumber(number_format($teachersWithSheba)) ?></div>
                <div style="font-size:11px;opacity:0.6;"><?= round(($teachersWithSheba / max($totalTeachers, 1)) * 100) ?>% از کل</div>
            </div>
            <div style="width:44px;height:44px;background:rgba(255,255,255,0.15);border-radius:12px;display:flex;align-items:center;justify-content:center;font-size:20px;">
                <i class="fas fa-check-circle"></i>
            </div>
        </div>
    </a>

    <a href="teachers.php?status=without_sheba" class="stat-card" style="display:block;background:linear-gradient(135deg, #D97706, #F59E0B);color:white;border-radius:14px;padding:18px 22px;box-shadow:0 4px 16px rgba(217,119,6,0.25);transition:transform 0.3s ease;text-decoration:none;cursor:pointer;">
        <div style="display:flex;justify-content:space-between;align-items:center;">
            <div>
                <div style="font-size:13px;opacity:0.8;">⚠️ بدون شماره شبا</div>
                <div style="font-size:26px;font-weight:800;margin-top:4px;"><?= persianNumber(number_format($teachersWithoutSheba)) ?></div>
                <div style="font-size:11px;opacity:0.6;"><?= round(($teachersWithoutSheba / max($totalTeachers, 1)) * 100) ?>% از کل</div>
            </div>
            <div style="width:44px;height:44px;background:rgba(255,255,255,0.15);border-radius:12px;display:flex;align-items:center;justify-content:center;font-size:20px;">
                <i class="fas fa-exclamation-triangle"></i>
            </div>
        </div>
    </a>

    <div class="stat-card" style="background:linear-gradient(135deg, #DC2626, #EF4444);color:white;border-radius:14px;padding:18px 22px;box-shadow:0 4px 16px rgba(220,38,38,0.25);transition:transform 0.3s ease;">
        <div style="display:flex;justify-content:space-between;align-items:center;">
            <div>
                <div style="font-size:13px;opacity:0.8;">⏳ در انتظار تأیید</div>
                <div style="font-size:26px;font-weight:800;margin-top:4px;"><?= persianNumber(number_format($pendingSheba)) ?></div>
                <div style="font-size:11px;opacity:0.6;">شماره شباهای در انتظار تأیید</div>
            </div>
            <div style="width:44px;height:44px;background:rgba(255,255,255,0.15);border-radius:12px;display:flex;align-items:center;justify-content:center;font-size:20px;">
                <i class="fas fa-clock"></i>
            </div>
        </div>
    </div>
</div>

<!-- ============================================================
     دو ستون: نمودار پرداخت‌ها + اقدامات سریع
     ============================================================ -->
<div style="display:grid;grid-template-columns:2fr 1fr;gap:20px;margin-bottom:24px;">
    
    <!-- ===== ستون راست: نمودار پرداخت‌های ماهانه ===== -->
    <div class="card" style="padding:20px 24px;background:#fff;border-radius:12px;border:1px solid #e5e7eb;">
        <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px;">
            <h4 style="font-size:16px;font-weight:700;color:#1f2937;margin:0;display:flex;align-items:center;gap:8px;">
                <i class="fas fa-chart-bar" style="color:#355C9C;"></i>
                روند پرداخت‌های ماهانه
            </h4>
            <span style="font-size:12px;color:#9ca3af;">۶ ماه اخیر</span>
        </div>
        <div style="height:250px;position:relative;">
            <canvas id="monthlyChart"></canvas>
        </div>
    </div>
    
    <!-- ===== ستون چپ: اقدامات سریع ===== -->
    <div class="card" style="padding:20px 24px;background:#fff;border-radius:12px;border:1px solid #e5e7eb;">
        <h4 style="font-size:16px;font-weight:700;color:#1f2937;margin-bottom:16px;display:flex;align-items:center;gap:8px;">
            <i class="fas fa-bolt" style="color:#f59e0b;"></i>
            اقدامات سریع
        </h4>
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:10px;">
            <a href="teacher-edit.php?action=create" class="quick-action-btn" style="display:flex;flex-direction:column;align-items:center;justify-content:center;padding:16px 12px;background:#f8fafc;border-radius:10px;border:1px solid #e5e7eb;text-decoration:none;color:#1f2937;transition:all 0.3s;gap:8px;">
                <i class="fas fa-user-plus" style="font-size:24px;color:#355C9C;"></i>
                <span style="font-size:13px;font-weight:600;">ثبت مدرس</span>
            </a>
            <a href="transactions.php" class="quick-action-btn" style="display:flex;flex-direction:column;align-items:center;justify-content:center;padding:16px 12px;background:#f8fafc;border-radius:10px;border:1px solid #e5e7eb;text-decoration:none;color:#1f2937;transition:all 0.3s;gap:8px;">
                <i class="fas fa-coins" style="font-size:24px;color:#059669;"></i>
                <span style="font-size:13px;font-weight:600;">ثبت تراکنش</span>
            </a>
            <a href="courses.php" class="quick-action-btn" style="display:flex;flex-direction:column;align-items:center;justify-content:center;padding:16px 12px;background:#f8fafc;border-radius:10px;border:1px solid #e5e7eb;text-decoration:none;color:#1f2937;transition:all 0.3s;gap:8px;">
                <i class="fas fa-book" style="font-size:24px;color:#7c3aed;"></i>
                <span style="font-size:13px;font-weight:600;">مدیریت دروس</span>
            </a>
            <a href="report-payment.php" class="quick-action-btn" style="display:flex;flex-direction:column;align-items:center;justify-content:center;padding:16px 12px;background:#f8fafc;border-radius:10px;border:1px solid #e5e7eb;text-decoration:none;color:#1f2937;transition:all 0.3s;gap:8px;">
                <i class="fas fa-file-invoice" style="font-size:24px;color:#dc2626;"></i>
                <span style="font-size:13px;font-weight:600;">گزارش پرداخت</span>
            </a>
        </div>
    </div>
</div>

<!-- ============================================================
     آمار مالی ۳ ترم گذشته
     ============================================================ -->
<?php if (!empty($financialStats)): ?>
<div style="margin-bottom:24px;">
    <h4 style="font-size:16px;font-weight:700;color:#1f2937;margin-bottom:12px;display:flex;align-items:center;gap:8px;">
        <i class="fas fa-chart-line" style="color:#355C9C;"></i>
        آمار مالی ۳ ترم گذشته
    </h4>
    <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(280px,1fr));gap:16px;">
        <?php foreach ($financialStats as $termId => $term): ?>
        <div class="stat-card" style="padding:16px 20px;background:#fff;border-radius:12px;border:1px solid #e5e7eb;transition:all 0.3s;">
            <div style="font-weight:700;color:#1f2937;font-size:15px;border-bottom:2px solid #355C9C;padding-bottom:8px;margin-bottom:10px;display:flex;justify-content:space-between;align-items:center;">
                <span><?= htmlspecialchars($term['name']) ?></span>
                <span style="font-size:12px;color:#9ca3af;font-weight:400;">(<?= htmlspecialchars($term['code']) ?>)</span>
            </div>
            <div style="display:grid;grid-template-columns:1fr 1fr;gap:4px 12px;font-size:13px;">
                <span style="color:#6b7280;">تعداد دروس:</span>
                <span style="font-weight:600;color:#1f2937;"><?= persianNumber($term['course_count']) ?></span>
                
                <span style="color:#6b7280;">حق التدریس:</span>
                <span style="font-weight:600;color:#355C9C;"><?= formatPrice($term['total_amount']) ?></span>
                
                <span style="color:#6b7280;">مالیات:</span>
                <span style="font-weight:600;color:#dc2626;"><?= formatPrice($term['total_tax']) ?></span>
                
                <span style="color:#6b7280;">بیمه:</span>
                <span style="font-weight:600;color:#d97706;"><?= formatPrice($term['total_insurance']) ?></span>
                
                <span style="color:#6b7280;font-weight:600;">قابل پرداخت:</span>
                <span style="font-weight:700;color:#059669;"><?= formatPrice($term['total_net']) ?></span>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
</div>
<?php endif; ?>

<!-- ============================================================
     آمار تیکت‌ها
     ============================================================ -->
<div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(180px,1fr));gap:16px;margin-bottom:24px;">
    <div class="stat-card" style="text-align:center;padding:14px 18px;background:#fff;border-radius:12px;border:1px solid #e5e7eb;">
        <div style="font-size:13px;color:#6b7280;">📩 کل تیکت‌ها</div>
        <div style="font-size:22px;font-weight:800;color:#1f2937;margin-top:4px;"><?= persianNumber(number_format($ticketStats['total'])) ?></div>
        <div style="font-size:11px;color:#9ca3af;">همه تیکت‌های ثبت‌شده</div>
    </div>
    
    <a href="<?= $baseUrl ?>tickets.php?status=new" class="stat-card" style="display:block;text-align:center;padding:14px 18px;background:#fff;border-radius:12px;border:2px solid #fca5a5;text-decoration:none;cursor:pointer;">
        <div style="font-size:13px;color:#dc2626;">🆕 جدید</div>
        <div style="font-size:22px;font-weight:800;color:#dc2626;margin-top:4px;"><?= persianNumber(number_format($ticketStats['new'])) ?></div>
        <div style="font-size:11px;color:#9ca3af;">تیکت‌های خوانده‌نشده</div>
    </a>
    
    <a href="<?= $baseUrl ?>tickets.php?status=replied" class="stat-card" style="display:block;text-align:center;padding:14px 18px;background:#fff;border-radius:12px;border:2px solid #fcd34d;text-decoration:none;cursor:pointer;">
        <div style="font-size:13px;color:#d97706;">💬 منتظر پاسخ</div>
        <div style="font-size:22px;font-weight:800;color:#d97706;margin-top:4px;"><?= persianNumber(number_format($ticketStats['replied'])) ?></div>
        <div style="font-size:11px;color:#9ca3af;">پاسخ داده‌شده و در انتظار</div>
    </a>
    
    <a href="<?= $baseUrl ?>tickets.php?status=closed" class="stat-card" style="display:block;text-align:center;padding:14px 18px;background:#fff;border-radius:12px;border:2px solid #86efac;text-decoration:none;cursor:pointer;">
        <div style="font-size:13px;color:#059669;">✅ بسته شده</div>
        <div style="font-size:22px;font-weight:800;color:#059669;margin-top:4px;"><?= persianNumber(number_format($ticketStats['closed'])) ?></div>
        <div style="font-size:11px;color:#9ca3af;">تیکت‌های بسته‌شده</div>
    </a>
</div>

<!-- ============================================================
     آخرین تراکنش‌ها
     ============================================================ -->
<div class="card" style="padding:20px 24px;background:#fff;border-radius:12px;border:1px solid #e5e7eb;margin-bottom:24px;">
    <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px;">
        <h4 style="font-size:16px;font-weight:700;color:#1f2937;margin:0;display:flex;align-items:center;gap:8px;">
            <i class="fas fa-clock" style="color:#355C9C;"></i>
            آخرین تراکنش‌ها
        </h4>
        <a href="transactions.php" style="font-size:13px;color:#355C9C;text-decoration:none;">مشاهده همه →</a>
    </div>
    
    <div class="table-responsive" style="overflow-x:auto;">
        <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>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($recentTransactions)): ?>
                <tr><td colspan="4" style="padding:20px;text-align:center;color:#9ca3af;">هیچ تراکنشی ثبت نشده است</td></tr>
                <?php else: ?>
                <?php foreach ($recentTransactions as $tr): ?>
                <tr style="border-bottom:1px solid #f3f4f6;" class="table-row-hover">
                    <td style="padding:8px 12px;font-weight:500;color:#1f2937;"><?= htmlspecialchars($tr['name'] . ' ' . $tr['family']) ?></td>
                    <td style="padding:8px 12px;text-align:center;font-weight:600;color:#355C9C;"><?= formatPrice($tr['amount']) ?></td>
                    <td style="padding:8px 12px;text-align:center;color:#6b7280;"><?= jalaliDate($tr['payment_date']) ?></td>
                    <td style="padding:8px 12px;text-align:center;color:#6b7280;"><?= getTermName($tr['term_id']) ?></td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<!-- ============================================================
     آخرین مدرسین وارد شده
     ============================================================ -->
<div class="card" style="padding:20px 24px;background:#fff;border-radius:12px;border:1px solid #e5e7eb;">
    <div style="display:flex;align-items:center;gap:10px;margin-bottom:16px;">
        <i class="fas fa-history" style="color:#355C9C;"></i>
        <h4 style="font-size:16px;font-weight:700;color:#1f2937;margin:0;">آخرین مدرسین وارد شده</h4>
    </div>
    
    <div class="table-responsive" style="overflow-x:auto;">
        <table style="width:100%;border-collapse:collapse;font-size:14px;">
            <thead>
                <tr style="background:#f8fafc;border-bottom:2px solid #e5e7eb;">
                    <th style="padding:10px 14px;text-align:right;color:#6b7280;font-weight:600;font-size:13px;">نام</th>
                    <th style="padding:10px 14px;text-align:center;color:#6b7280;font-weight:600;font-size:13px;">کد ملی</th>
                    <th style="padding:10px 14px;text-align:center;color:#6b7280;font-weight:600;font-size:13px;">تاریخ ورود</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($recentLogins)): ?>
                <tr><td colspan="3" style="padding:30px;text-align:center;color:#9ca3af;">هیچ ورودی ثبت نشده است</td></tr>
                <?php else: ?>
                <?php foreach ($recentLogins as $teacher): ?>
                <tr style="border-bottom:1px solid #f3f4f6;" class="table-row-hover">
                    <td style="padding:10px 14px;">
                        <div style="display:flex;align-items:center;gap:10px;">
                            <div style="width:32px;height:32px;border-radius:50%;background:linear-gradient(135deg, #355C9C, #2FA7A3);display:flex;align-items:center;justify-content:center;color:white;font-weight:600;font-size:13px;">
                                <?= mb_substr($teacher['name'], 0, 1, 'UTF-8') ?>
                            </div>
                            <span style="font-weight:500;color:#1f2937;"><?= htmlspecialchars($teacher['name'] . ' ' . $teacher['family']) ?></span>
                        </div>
                    </td>
                    <td style="padding:10px 14px;text-align:center;color:#6b7280;"><?= persianNumber($teacher['national_code']) ?></td>
                    <td style="padding:10px 14px;text-align:center;color:#6b7280;font-size:13px;"><?= jalaliDate($teacher['last_login']) ?></td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<!-- ============================================================
     اسکریپت Chart.js
     ============================================================ -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

<script>
document.addEventListener('DOMContentLoaded', function() {
    // ===== نمودار پرداخت‌های ماهانه =====
    const ctx = document.getElementById('monthlyChart').getContext('2d');
    
    const monthlyLabels = <?= json_encode($monthLabels) ?>;
    const monthlyData = <?= json_encode($monthlyData) ?>;
    
    new Chart(ctx, {
        type: 'bar',
        data: {
            labels: monthlyLabels,
            datasets: [{
                label: 'مبلغ پرداختی (ریال)',
                data: monthlyData,
                backgroundColor: [
                    'rgba(53, 92, 156, 0.7)',
                    'rgba(47, 167, 163, 0.7)',
                    'rgba(245, 193, 108, 0.7)',
                    'rgba(53, 92, 156, 0.7)',
                    'rgba(47, 167, 163, 0.7)',
                    'rgba(245, 193, 108, 0.7)'
                ],
                borderColor: '#355C9C',
                borderWidth: 1,
                borderRadius: 4
            }]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            plugins: {
                legend: {
                    display: false
                }
            },
            scales: {
                y: {
                    beginAtZero: true,
                    ticks: {
                        callback: function(value) {
                            if (value >= 1000000) {
                                return (value / 1000000) + 'M';
                            } else if (value >= 1000) {
                                return (value / 1000) + 'K';
                            }
                            return value;
                        }
                    }
                }
            }
        }
    });
    
    // ===== هاور روی دکمه‌های اقدامات سریع =====
    document.querySelectorAll('.quick-action-btn').forEach(function(el) {
        el.addEventListener('mouseenter', function() {
            this.style.borderColor = '#355C9C';
            this.style.boxShadow = '0 4px 12px rgba(53,92,156,0.1)';
            this.style.transform = 'translateY(-2px)';
        });
        el.addEventListener('mouseleave', function() {
            this.style.borderColor = '#e5e7eb';
            this.style.boxShadow = 'none';
            this.style.transform = 'translateY(0)';
        });
    });
});
</script>