<?php
// admin/export-pdf.php - خروجی PDF (مستقل)

session_start();
require_once '../config/database.php';
require_once '../includes/db.php';
require_once '../includes/auth.php';
require_once '../includes/functions.php';

if (!isLoggedIn()) {
    die('ابتدا وارد شوید.');
}

$user = getCurrentUser();
if (!in_array($user['role_level'], [1,2,5])) {
    die('دسترسی غیرمجاز.');
}

$term_id = $_GET['term_id'] ?? 0;
$status = $_GET['status'] ?? 'all';

// دریافت لیست مدرسین
$whereConditions = ["u.role_id = (SELECT id FROM roles WHERE level = 6)"];
$params = [];

if ($status == 'with_sheba') {
    $whereConditions[] = "u.sheba_number IS NOT NULL AND u.sheba_number != '' AND u.is_sheba_confirmed = 1";
} elseif ($status == 'without_sheba') {
    $whereConditions[] = "(u.sheba_number IS NULL OR u.sheba_number = '')";
}

if ($term_id > 0) {
    $whereConditions[] = "EXISTS (SELECT 1 FROM courses c WHERE c.teacher_id = u.id AND c.term_id = ?)";
    $params[] = $term_id;
}

$whereSQL = implode(' AND ', $whereConditions);

$teachers = fetchAll(
    "SELECT u.*, r.name as role_name 
     FROM users u 
     LEFT JOIN roles r ON u.role_id = r.id 
     WHERE $whereSQL 
     ORDER BY u.name ASC",
    $params
);

// =============================================
// خروجی HTML برای پرینت
// =============================================
?>
<!DOCTYPE html>
<html dir="rtl">
<head>
    <meta charset="UTF-8">
    <title>لیست مدرسین</title>
    <style>
        @media print {
            .no-print { display: none; }
            body { padding: 20px; }
        }
        body { font-family: 'Vazirmatn', 'Tahoma', sans-serif; padding: 30px; }
        h1 { text-align: center; color: #1a237e; }
        .info { text-align: center; color: #666; margin-bottom: 20px; }
        table { width: 100%; border-collapse: collapse; margin-top: 20px; }
        th { background: #1a237e; color: white; padding: 10px; text-align: right; }
        td { padding: 8px 10px; border-bottom: 1px solid #ddd; }
        .footer { text-align: center; margin-top: 30px; color: #999; font-size: 12px; border-top: 1px solid #ddd; padding-top: 20px; }
        .btn-print { padding: 10px 30px; background: #1a237e; color: white; border: none; border-radius: 8px; font-size: 16px; cursor: pointer; }
        .btn-print:hover { background: #0d1445; }
    </style>
</head>
<body>
    <div class="no-print" style="text-align:center;margin-bottom:20px;">
        <button onclick="window.print()" class="btn-print">🖨️ چاپ / پرینت</button>
        <button onclick="window.close()" class="btn-print" style="background:#666;margin-right:10px;">✖ بستن</button>
    </div>
    
    <h1>📋 لیست مدرسین</h1>
    <div class="info">تاریخ: <?= jalaliDate(date('Y-m-d')) ?> | تعداد: <?= count($teachers) ?> نفر</div>
    
    <table>
        <tr><th>#</th><th>نام</th><th>کد ملی</th><th>موبایل</th><th>شماره شبا</th><th>وضعیت</th></tr>
        <?php if (empty($teachers)): ?>
        <tr><td colspan="6" style="text-align:center;">هیچ مدرسی یافت نشد</td></tr>
        <?php else: ?>
        <?php $i = 1; foreach ($teachers as $teacher): ?>
        <tr>
            <td><?= $i++ ?></td>
            <td><?= htmlspecialchars($teacher['name'] . ' ' . $teacher['family']) ?></td>
            <td><?= $teacher['national_code'] ?></td>
            <td><?= $teacher['mobile'] ?></td>
            <td><?= $teacher['sheba_number'] ?? 'ثبت نشده' ?></td>
            <td><?= $teacher['is_active'] ? 'فعال' : 'غیرفعال' ?></td>
        </tr>
        <?php endforeach; ?>
        <?php endif; ?>
    </table>
    
    <div class="footer">سامانه مدیریت اساتید - دانشگاه علوم و فناوری</div>
</body>
</html>
<?php
exit;
?>