<?php
// admin/cetak_id_panitia.php
include_once 'init.php'; // Termasuk koneksi DB

// --- 1. DETEKSI MODE CETAK ---
$mode = $_GET['mode'] ?? 'satuan'; // Mode default: satuan
$id_panitia = (int)($_GET['id'] ?? 0);

// --- 2. LOGIKA PENGAMBILAN DATA ---
$panitia_list = [];

if (!$id_tahun_aktif) {
    die("Tidak ada tahun pemilihan aktif untuk mencetak.");
}

$tahun_aktif = $tahun_aktif_display ?? "Tahun Pemilihan Aktif";

// --- AMBIL LOGO DARI DATABASE ---
$logo_osim = null;
$logo_kegiatan = null;
try {
    $stmt_pengaturan = $pdo->query("SELECT logo_osim, logo_kegiatan FROM pengaturan_aplikasi WHERE id = 1");
    $pengaturan = $stmt_pengaturan->fetch(PDO::FETCH_ASSOC);
    if ($pengaturan) {
        if ($pengaturan['logo_osim']) {
            $logo_osim = '../assets/img/logo/' . htmlspecialchars($pengaturan['logo_osim']);
        }
        if ($pengaturan['logo_kegiatan']) {
            $logo_kegiatan = '../assets/img/logo/' . htmlspecialchars($pengaturan['logo_kegiatan']);
        }
    }
} catch (PDOException $e) {
    // Jika gagal ambil logo, lanjutkan tanpa logo
}

if ($mode === 'massal') {
    // Mode Massal: Ambil semua panitia untuk tahun aktif
    $query_panitia = "
        SELECT * FROM panitia 
        WHERE id_tahun_fk = ? 
        ORDER BY 
            FIELD(jabatan_panitia, 'Pelindung', 'Penanggung Jawab', 'Pembina', 'Ketua Panitia', 'Sekretaris', 'Anggota'), 
            nama_panitia ASC
    ";
    $stmt = $pdo->prepare($query_panitia);
    $stmt->execute([$id_tahun_aktif]);
    $panitia_list = $stmt->fetchAll(PDO::FETCH_ASSOC);

    if (empty($panitia_list)) {
        die("Tidak ada data panitia untuk dicetak.");
    }

} else {
    // Mode Satuan: Ambil data satu panitia berdasarkan ID
    if (!$id_panitia) {
        die("ID Panitia tidak ditemukan.");
    }
    
    $stmt = $pdo->prepare("SELECT * FROM panitia WHERE id_panitia = ? AND id_tahun_fk = ?");
    $stmt->execute([$id_panitia, $id_tahun_aktif]);
    $panitia = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$panitia) {
        die("Data Panitia tidak ditemukan.");
    }
    // Untuk mode satuan, masukkan satu panitia ke dalam list untuk loop di HTML
    $panitia_list[] = $panitia; 
}

// Tentukan Judul Halaman
$page_title = ($mode === 'massal') 
    ? "Cetak Massal ID Card Panitia - {$tahun_aktif}" 
    : "Cetak ID Card Panitia - " . htmlspecialchars($panitia_list[0]['nama_panitia']);

?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title><?= $page_title ?></title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f5f5f5;
            -webkit-print-color-adjust: exact;
            color-adjust: exact;
            print-color-adjust: exact;
        }

        /* === STYLE UMUM ID CARD === */
        .id-card {
            background: linear-gradient(145deg, #ffffff 0%, #f8f9fa 100%);
            border-radius: 20px;
            overflow: hidden;
            position: relative;
            border: 3px solid #fff;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
            display: flex;
            flex-direction: column;
        }

        /* Header dengan logo */
        .card-header {
            background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
            text-align: center;
            position: relative;
            overflow: hidden;
        }

        .card-header::before {
            content: '';
            position: absolute;
            top: -50%;
            left: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
        }

        .logo-container {
            position: relative;
            z-index: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }

        .logo {
            background: white;
            border-radius: 50%;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
        }

        .logo img {
            width: 100%;
            height: 100%;
            object-fit: contain;
            padding: 5px;
        }

        /* Fallback jika logo tidak ada */
        .logo-fallback {
            width: 100%;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            font-weight: bold;
        }

        .header-text {
            color: white;
            position: relative;
            z-index: 1;
        }

        .header-text h3 {
            font-weight: 600;
            letter-spacing: 0.5px;
            text-transform: uppercase;
        }

        .header-text h2 {
            font-weight: 700;
            letter-spacing: 1px;
        }

        .header-text p {
            opacity: 0.9;
            font-weight: 500;
        }

        /* Body Card */
        .card-body {
            text-align: center;
            position: relative;
            flex-grow: 1;
        }

        .decorative-line {
            height: 3px;
            background: linear-gradient(90deg, #1e3c72, #2a5298);
            margin: 0 auto;
            border-radius: 2px;
        }

        .foto-container {
            margin: 0 auto;
            position: relative;
        }

        .foto-frame {
            width: 100%;
            height: 100%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 15px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: 600;
            box-shadow: 0 8px 20px rgba(102, 126, 234, 0.4);
            position: relative;
            overflow: hidden;
        }

        .foto-icon {
            opacity: 0.7;
        }

        .nama-panitia {
            font-weight: 700;
            color: #1e3c72;
            line-height: 1.2;
        }

        .jabatan-badge {
            display: inline-block;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 20px;
            font-weight: 600;
            box-shadow: 0 4px 10px rgba(102, 126, 234, 0.3);
        }

        .info-box {
            background: #f8f9fa;
            border-radius: 10px;
            border-left: 3px solid #2a5298;
        }

        .info-label {
            color: #6c757d;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .info-value {
            color: #1e3c72;
            font-weight: 700;
        }

        /* Footer */
        .card-footer {
            background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
            text-align: center;
            color: white;
            font-weight: 600;
            letter-spacing: 0.5px;
        }

        /* Watermark pattern */
        .card-body::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-image: 
                repeating-linear-gradient(45deg, transparent, transparent 35px, rgba(102, 126, 234, 0.02) 35px, rgba(102, 126, 234, 0.02) 70px);
            pointer-events: none;
        }

        /* === MODE SATUAN === */
        <?php if ($mode === 'satuan'): ?>
        
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 20px;
        }

        .print-container {
            display: flex;
            justify-content: center;
        }

        .id-card {
            width: 320px;
            height: 500px;
        }

        .card-header {
            padding: 15px;
        }

        .logo {
            width: 60px;
            height: 60px;
            margin-bottom: 8px;
        }

        .logo-fallback {
            font-size: 24px;
        }

        .header-text h3 {
            font-size: 11px;
            margin-bottom: 2px;
        }

        .header-text h2 {
            font-size: 16px;
            margin-bottom: 3px;
        }

        .header-text p {
            font-size: 10px;
        }

        .card-body {
            padding: 25px 20px;
        }

        .decorative-line {
            width: 60px;
            margin-bottom: 20px;
        }

        .foto-container {
            width: 140px;
            height: 170px;
            margin-bottom: 20px;
        }

        .foto-frame {
            font-size: 12px;
        }

        .foto-icon {
            font-size: 48px;
        }

        .nama-panitia {
            font-size: 22px;
            margin-bottom: 8px;
        }

        .jabatan-badge {
            padding: 6px 20px;
            font-size: 13px;
            margin-bottom: 15px;
        }

        .info-box {
            padding: 12px;
            margin-top: 15px;
        }

        .info-label {
            font-size: 10px;
            margin-bottom: 4px;
        }

        .info-value {
            font-size: 13px;
        }

        .card-footer {
            padding: 12px;
            font-size: 11px;
        }

        @media print {
            body {
                background: white;
                display: block;
                padding: 0;
            }
            .id-card {
                box-shadow: none;
                border: 2px solid #333;
                margin: 0 auto;
            }
        }

        /* === MODE MASSAL === */
        <?php else: ?>

        @page {
            size: A4 portrait;
            margin: 10mm;
        }

        body {
            margin: 0;
            padding: 0;
            background: white;
        }

        .print-container {
            width: 100%;
            display: flex;
            flex-wrap: wrap;
            justify-content: flex-start;
        }

        .card-wrapper {
            width: 50%;
            height: 140mm;
            padding: 5mm;
            box-sizing: border-box;
            page-break-inside: avoid;
        }

        .card-wrapper:nth-child(4n) {
            page-break-after: always;
        }

        .id-card {
            width: 100%;
            height: 130mm;
        }

        .card-header {
            padding: 8px;
        }

        .logo {
            width: 45px;
            height: 45px;
            margin-bottom: 6px;
        }

        .logo-fallback {
            font-size: 18px;
        }

        .header-text h3 {
            font-size: 8pt;
            margin-bottom: 2px;
        }

        .header-text h2 {
            font-size: 11pt;
            margin-bottom: 2px;
        }

        .header-text p {
            font-size: 7pt;
        }

        .card-body {
            padding: 12px 10px;
        }

        .decorative-line {
            width: 40px;
            margin-bottom: 12px;
        }

        .foto-container {
            width: 100px;
            height: 150px;
            margin-bottom: 12px;
        }

        .foto-frame {
            font-size: 9pt;
        }

        .foto-icon {
            font-size: 32px;
        }

        .nama-panitia {
            font-size: 14pt;
            margin-bottom: 6px;
        }

        .jabatan-badge {
            padding: 4px 14px;
            font-size: 10pt;
            margin-bottom: 10px;
        }

        .info-box {
            padding: 8px;
            margin-top: 10px;
        }

        .info-label {
            font-size: 7pt;
            margin-bottom: 3px;
        }

        .info-value {
            font-size: 9pt;
        }

        .card-footer {
            padding: 8px;
            font-size: 8pt;
        }

        @media print {
            .id-card {
                box-shadow: none;
                border: 1px solid #333;
            }
        }

        <?php endif; ?>
    </style>
</head>
<body onload="window.print()">

    <div class="print-container">
        <?php foreach ($panitia_list as $p) : ?>
            <div class="<?= ($mode === 'massal') ? 'card-wrapper' : '' ?>">
                <div class="id-card">
                    <div class="card-header">
                        <div class="logo-container">
                            <?php if ($logo_osim): ?>
                                <div class="logo">
                                    <img src="<?= $logo_osim ?>" alt="Logo OSIM">
                                </div>
                            <?php endif; ?>
                            
                            <div class="logo">
                                <?php if ($logo_kegiatan): ?>
                                    <img src="<?= $logo_kegiatan ?>" alt="Logo Kegiatan">
                                <?php else: ?>
                                    <div class="logo-fallback">KPU</div>
                                <?php endif; ?>
                            </div>
                        </div>
                        <div class="header-text">
                            <h3>Organisasi Siswa Intra Madrasah (OSIM)</h3>
                            <h2>KOMISI PEMILIHAN OSIM</h2>
                            <p>MA Darul Ulum Palangka Raya</p>
                        </div>
                    </div>

                    <div class="card-body">
                        <br>
                        <div class="decorative-line"></div>
                        <br>
                        <div class="foto-container">
                            <div class="foto-frame">
                                <?php if (!empty($p['foto_panitia']) && file_exists('../assets/img/panitia/' . $p['foto_panitia'])): ?>
                                    <img src="../assets/img/panitia/<?= htmlspecialchars($p['foto_panitia']) ?>" 
                                         alt="<?= htmlspecialchars($p['nama_panitia']) ?>" 
                                         style="width: 100%; height: 100%; object-fit: cover; border-radius: 15px;">
                                <?php else: ?>
                                    <div class="foto-icon">📷</div>
                                <?php endif; ?>
                            </div>
                        </div>

                        <div class="nama-panitia">
                            <?= htmlspecialchars($p['nama_panitia']) ?>
                        </div>
                        
                        <div class="jabatan-badge">
                            <?= htmlspecialchars($p['jabatan_panitia']) ?>
                        </div>

                        <br>
                        <br>
                        <div class="decorative-line"></div>
                    </div>

                    <div class="card-footer">
                        TAHUN PEMILIHAN <?= htmlspecialchars($tahun_aktif) ?>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>

</body>
</html>