<?php
require_once '../config/database.php';
require_once '../includes/header.php';

// Query untuk mengambil semua data GTK
$query = "SELECT * FROM bind_gtk ORDER BY nama ASC";
$result = mysqli_query($conn, $query);

// Hitung total data
$total_data = mysqli_num_rows($result);

// Format tanggal Indonesia
function tanggalIndonesia($date) {
    if (!$date || $date == '0000-00-00') return '-';
    
    $bulan = array(
        1 => 'Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni',
        'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember'
    );
    
    $tanggal = date('j', strtotime($date));
    $bulan_angka = date('n', strtotime($date));
    $tahun = date('Y', strtotime($date));
    
    return $tanggal . ' ' . $bulan[$bulan_angka] . ' ' . $tahun;
}

// Format status aktif
function formatStatusAktif($status) {
    return $status == 1 ? 'Aktif' : 'Non-Aktif';
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cetak Data GTK</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .header {
            text-align: center;
            margin-bottom: 20px;
            border-bottom: 2px solid #333;
            padding-bottom: 10px;
        }
        .header h2 {
            margin: 0;
            font-size: 18px;
        }
        .header p {
            margin: 5px 0;
            font-size: 14px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            font-size: 12px;
            margin-bottom: 20px;
        }
        table, th, td {
            border: 1px solid #000;
        }
        th, td {
            padding: 6px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
            text-align: center;
        }
        .no {
            width: 30px;
            text-align: center;
        }
        .text-center {
            text-align: center;
        }
        .footer {
            margin-top: 30px;
            text-align: right;
            font-size: 12px;
        }
        .page-break {
            page-break-after: always;
        }
        @media print {
            body {
                margin: 0;
                padding: 15px;
            }
            .no-print {
                display: none;
            }
            .header {
                margin-top: 0;
            }
        }
    </style>
</head>
<body>
    <div class="no-print" style="margin-bottom: 20px;">
        <button onclick="window.print()" class="btn-print" style="padding: 10px 15px; background: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer;">
            Cetak / Print
        </button>
      <!--  <button onclick="window.history.back()" class="btn-back" style="padding: 10px 15px; background: #6c757d; color: white; border: none; border-radius: 5px; cursor: pointer; margin-left: 10px;">
            Kembali
        </button> -->
    </div>

    <div class="header">
        <h2>DATA GURU DAN TENAGA KEPENDIDIKAN</h2>
        <p>SEKOLAH MENENGAH PERTAMA (SMP)</p>
        <p>Periode: <?php echo date('d F Y'); ?></p>
    </div>

    <table>
        <thead>
            <tr>
                <th class="no">No</th>
                <th>NIP</th>
                <th>NUPTK</th>
                <th>Nama</th>
                <th>Jenis GTK</th>
                <th>Status Kepegawaian</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
            <?php
            if ($result && $total_data > 0) {
                $no = 1;
                while ($row = mysqli_fetch_assoc($result)) {
                    echo "<tr>
                        <td class='text-center'>{$no}</td>
                        <td>" . ($row['nip'] ? htmlspecialchars($row['nip']) : '-') . "</td>
                        <td>" . ($row['nuptk'] ? htmlspecialchars($row['nuptk']) : '-') . "</td>
                        <td>" . htmlspecialchars($row['nama']) . "</td>
                        <td>" . htmlspecialchars($row['jenis_gtk']) . "</td>
                        <td>" . htmlspecialchars($row['status_kepegawaian']) . "</td>
                        <td class='text-center'>" . formatStatusAktif($row['status_aktif']) . "</td>
                    </tr>";
                    $no++;
                }
            } else {
                echo "<tr>
                    <td colspan='7' class='text-center'>Tidak ada data GTK</td>
                </tr>";
            }
            ?>
        </tbody>
    </table>

    <div class="footer">
        <div style="float: left;">
            <p>Total Data: <strong><?php echo $total_data; ?></strong></p>
        </div>
        <div style="float: right; text-align: center;">
            <p>Mengetahui,</p>
            <br><br><br>
            <p>_________________________</p>
            <p>Kepala Sekolah</p>
        </div>
        <div style="clear: both;"></div>
    </div>

    <script>
        // Auto print jika diinginkan
        // window.print();
    </script>
</body>
</html>

<?php
require_once '../includes/footer.php';
?>