<?php 
include '../koneksi.php';
cek_akses(['Kepala', 'Super Admin']);

// Ambil parameter dari URL (dikirim dari supervisi_menu.php)
$guru_id = isset($_GET['guru_id']) ? (int)$_GET['guru_id'] : 0;
$guru_nama = isset($_GET['guru_nama']) ? urldecode($_GET['guru_nama']) : '';

// Jika ada guru_id tapi nama kosong, ambil dari database
if($guru_id > 0 && empty($guru_nama)) {
    $query_guru = mysqli_query($conn, "SELECT full_name FROM users WHERE id = $guru_id AND role = 'Guru'");
    if($query_guru && mysqli_num_rows($query_guru) > 0) {
        $guru = mysqli_fetch_assoc($query_guru);
        $guru_nama = $guru['full_name'];
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Formulir Permintaan Tindakan Perbaikan dan Pencegahan</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/admin-lte@3.2/dist/css/adminlte.min.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="../assets/css/theme-istiqlal-green.css">
    <style>
        .form-container { max-width: 1200px; margin: 0 auto; }
        .section-card { margin-bottom: 20px; border-radius: 12px; overflow: hidden; box-shadow: 0 2px 8px rgba(0,0,0,0.08); }
        .section-header { background: #0b5e4e; color: white; padding: 10px 15px; font-weight: 600; }
        .section-body { background: white; padding: 15px; }
        .info-row { display: flex; padding: 8px 0; border-bottom: 1px solid #e2e8f0; }
        .info-label { width: 200px; font-weight: 600; }
        .info-value { flex: 1; }
        .check-group { display: flex; gap: 20px; flex-wrap: wrap; margin-top: 10px; }
        .check-option { display: inline-flex; align-items: center; gap: 5px; }
        @media print { .no-print { display: none !important; } }
    </style>
</head>
<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">
    <?php include '../sidebar.php'; ?>
    
    <div class="content-wrapper">
        <div class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1 class="m-0" style="font-size: 18px;"><i class="fas fa-tools text-warning"></i> FORMULIR PERMINTAAN TINDAKAN PERBAIKAN DAN PENCEGAHAN</h1>
                        <small>F-024-KPM</small>
                    </div>
                    <div class="col-sm-6">
                        <ol class="breadcrumb float-sm-right">
                            <li class="breadcrumb-item"><a href="../dashboard.php">Home</a></li>
                            <li class="breadcrumb-item"><a href="supervisi_menu.php">Supervisi</a></li>
                            <li class="breadcrumb-item active">Tindakan Perbaikan</li>
                        </ol>
                    </div>
                </div>
            </div>
        </div>

        <div class="content">
            <div class="container-fluid form-container">
                <form method="POST" action="cetak_dokumen.php" target="_blank">
                    <input type="hidden" name="form_type" value="perbaikan">
                    
                    <!-- Data Guru -->
                    <div class="section-card">
                        <div class="section-header"><i class="fas fa-user"></i> Data Guru</div>
                        <div class="section-body">
                            <div class="row">
                                <div class="col-md-3 mb-2">
                                    <label class="small fw-bold">Nama Guru</label>
                                    <input type="text" name="nama_guru" class="form-control form-control-sm" value="<?= htmlspecialchars($guru_nama) ?>" required>
                                    <?php if($guru_nama): ?>
                                    <small class="text-success d-block">✓ Data otomatis dari jadwal supervisi</small>
                                    <?php endif; ?>
                                </div>
                                <div class="col-md-3 mb-2">
                                    <label class="small fw-bold">NIP/NUPTK</label>
                                    <input type="text" name="nip" class="form-control form-control-sm" placeholder="Nomor Induk">
                                </div>
                                <div class="col-md-3 mb-2">
                                    <label class="small fw-bold">Unit/Sekolah</label>
                                    <input type="text" name="unit" class="form-control form-control-sm" value="<?= $_SESSION['unit'] ?>" readonly>
                                </div>
                                <div class="col-md-3 mb-2">
                                    <label class="small fw-bold">Tanggal</label>
                                    <input type="date" name="tanggal" class="form-control form-control-sm" value="<?= date('Y-m-d') ?>">
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Header Form -->
                    <div class="section-card">
                        <div class="section-header"><i class="fas fa-file-alt"></i> Data Formulir</div>
                        <div class="section-body">
                            <div class="row">
                                <div class="col-md-4 mb-2">
                                    <label class="small fw-bold">No Formulir</label>
                                    <input type="text" name="no_formulir" class="form-control form-control-sm" placeholder="Contoh: 001/F/KPM/2024">
                                </div>
                                <div class="col-md-4 mb-2">
                                    <label class="small fw-bold">Satker/Satdik</label>
                                    <input type="text" name="satker" class="form-control form-control-sm" placeholder="Nama satuan kerja">
                                </div>
                                <div class="col-md-4 mb-2">
                                    <label class="small fw-bold">Lingkup Audit</label>
                                    <input type="text" name="lingkup_audit" class="form-control form-control-sm" placeholder="Lingkup audit">
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Jenis Ketidaksesuaian -->
                    <div class="section-card">
                        <div class="section-header"><i class="fas fa-exclamation-triangle text-warning"></i> Jenis Ketidaksesuaian</div>
                        <div class="section-body">
                            <div class="check-group">
                                <label class="check-option"><input type="checkbox" name="jenis[]" value="Temuan Audit Internal"> Temuan Audit Internal</label>
                                <label class="check-option"><input type="checkbox" name="jenis[]" value="Produk Tidak Sesuai"> Produk Tidak Sesuai</label>
                                <label class="check-option"><input type="checkbox" name="jenis[]" value="Keluhan Pihak berkepentingan"> Keluhan Pihak berkepentingan</label>
                                <label class="check-option"><input type="checkbox" name="jenis[]" value="Sasaran Mutu/KPI"> Sasaran Mutu/KPI</label>
                                <label class="check-option"><input type="checkbox" name="jenis[]" value="Observasi/Saran"> Observasi/Saran</label>
                            </div>
                            <div class="mt-2">
                                <label class="small fw-bold">Lain-lain</label>
                                <input type="text" name="jenis_lain" class="form-control form-control-sm" placeholder="Sebutkan...">
                            </div>
                        </div>
                    </div>

                    <!-- Uraian dan Bukti -->
                    <div class="section-card">
                        <div class="section-header"><i class="fas fa-file-alt"></i> Uraian dan Bukti Ketidaksesuaian</div>
                        <div class="section-body">
                            <div class="mb-2">
                                <label class="small fw-bold">Uraian ketidaksesuaian/potensi ketidaksesuaian</label>
                                <small class="text-muted d-block mb-1">(diisi oleh Auditor atau yang menemukan)</small>
                                <textarea name="uraian_ketidaksesuaian" class="form-control form-control-sm" rows="3"></textarea>
                            </div>
                            <div class="mb-2">
                                <label class="small fw-bold">Bukti ketidaksesuaian/potensi ketidaksesuaian</label>
                                <small class="text-muted d-block mb-1">(diisi oleh Auditor atau yang menemukan)</small>
                                <textarea name="bukti_ketidaksesuaian" class="form-control form-control-sm" rows="3"></textarea>
                            </div>
                        </div>
                    </div>

                    <!-- Pelapor dan Penerima -->
                    <div class="section-card">
                        <div class="section-header"><i class="fas fa-user-check"></i> Pelapor dan Penerima</div>
                        <div class="section-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="info-row">
                                        <div class="info-label">Dilaporkan oleh</div>
                                        <div class="info-value"><input type="text" name="dilaporkan_oleh" class="form-control form-control-sm"></div>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="info-row">
                                        <div class="info-label">Tanggal</div>
                                        <div class="info-value"><input type="date" name="tanggal_lapor" class="form-control form-control-sm"></div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="info-row">
                                        <div class="info-label">Auditor/Pelapor</div>
                                        <div class="info-value"><input type="text" name="auditor" class="form-control form-control-sm" value="<?= $_SESSION['full_name'] ?>" readonly></div>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="info-row">
                                        <div class="info-label">Diterima oleh (Penjamin Mutu)</div>
                                        <div class="info-value"><input type="text" name="penerima" class="form-control form-control-sm"></div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="info-row">
                                        <div class="info-label">Auditee/Pihak Terkait</div>
                                        <div class="info-value"><input type="text" name="auditee" class="form-control form-control-sm"></div>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="info-row">
                                        <div class="info-label">Tanggal rencana penyelesaian</div>
                                        <div class="info-value"><input type="date" name="tanggal_rencana" class="form-control form-control-sm"></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Investigasi dan Perbaikan -->
                    <div class="section-card">
                        <div class="section-header"><i class="fas fa-search"></i> Investigasi dan Perbaikan</div>
                        <div class="section-body">
                            <div class="mb-2">
                                <label class="small fw-bold">Investigasi penyebab ketidaksesuaian</label>
                                <small class="text-muted d-block mb-1">(diisi oleh Auditee atau pihak terkait)</small>
                                <textarea name="investigasi" class="form-control form-control-sm" rows="3"></textarea>
                            </div>
                            <div class="mb-2">
                                <label class="small fw-bold">Perbaikan yang dilakukan</label>
                                <small class="text-muted d-block mb-1">(diisi oleh Auditee atau pihak terkait)</small>
                                <textarea name="perbaikan" class="form-control form-control-sm" rows="3"></textarea>
                            </div>
                            <div class="mb-2">
                                <label class="small fw-bold">Tindakan perbaikan untuk mencegah terulangnya</label>
                                <small class="text-muted d-block mb-1">(diisi oleh Auditee atau pihak terkait)</small>
                                <textarea name="tindakan_perbaikan" class="form-control form-control-sm" rows="3"></textarea>
                            </div>
                        </div>
                    </div>

                    <!-- Verifikasi -->
                    <div class="section-card">
                        <div class="section-header"><i class="fas fa-check-double"></i> Verifikasi</div>
                        <div class="section-body">
                            <div class="mb-2">
                                <label class="small fw-bold">Verifikasi tindakan perbaikan/pencegahan</label>
                                <div class="check-group">
                                    <label class="check-option"><input type="radio" name="verifikasi" value="Diterima"> Diterima</label>
                                    <label class="check-option"><input type="radio" name="verifikasi" value="Ditolak"> Ditolak</label>
                                </div>
                            </div>
                            <div class="mb-2">
                                <label class="small fw-bold">Alasan jika ditolak</label>
                                <textarea name="alasan_tolak" class="form-control form-control-sm" rows="2"></textarea>
                            </div>
                        </div>
                    </div>

                    <!-- Lampiran -->
                    <div class="section-card">
                        <div class="section-header"><i class="fas fa-paperclip"></i> Lampiran</div>
                        <div class="section-body">
                            <textarea name="lampiran" class="form-control form-control-sm" rows="2" placeholder="Sebutkan lampiran yang dilampirkan..."></textarea>
                        </div>
                    </div>

                    <!-- Tanda Tangan -->
                    <div class="section-card">
                        <div class="section-header"><i class="fas fa-signature"></i> Tanda Tangan</div>
                        <div class="section-body">
                            <div class="row">
                                <div class="col-md-4">
                                    <label class="small fw-bold">Auditee/Pihak Terkait</label>
                                    <input type="text" name="auditee_ttd" class="form-control form-control-sm">
                                </div>
                                <div class="col-md-4">
                                    <label class="small fw-bold">Tanggal</label>
                                    <input type="date" name="tanggal_auditee" class="form-control form-control-sm">
                                </div>
                            </div>
                            <div class="row mt-2">
                                <div class="col-md-4">
                                    <label class="small fw-bold">Auditor/Pelapor</label>
                                    <input type="text" name="auditor_ttd" class="form-control form-control-sm" value="<?= $_SESSION['full_name'] ?>" readonly>
                                </div>
                                <div class="col-md-4">
                                    <label class="small fw-bold">Tanggal</label>
                                    <input type="date" name="tanggal_auditor" class="form-control form-control-sm">
                                </div>
                                <div class="col-md-4">
                                    <label class="small fw-bold">Penjamin Mutu</label>
                                    <input type="text" name="penjamin_mutu" class="form-control form-control-sm">
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="text-center mb-4 no-print">
                        <button type="submit" class="btn btn-warning btn-sm"><i class="fas fa-file-pdf"></i> Simpan & Cetak PDF</button>
                        <button type="button" class="btn btn-secondary btn-sm" onclick="window.print()"><i class="fas fa-print"></i> Print</button>
                        <a href="supervisi_menu.php?guru_id=<?= $guru_id ?>&guru_nama=<?= urlencode($guru_nama) ?>" class="btn btn-outline-secondary btn-sm">
                            <i class="fas fa-arrow-left"></i> Kembali
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <footer class="main-footer no-print"><strong>&copy; <?= date('Y') ?> SIM ISTIQLAL</strong></footer>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>