<div class="main-content">
    <div class="container-fluid" style="margin-top: 50px;">
        <h2><i class="bi bi-grid-3x3-gap"></i> Template 2: Matriks Pembagian Peran</h2>

        <form method="post" action="<?= base_url("dialog_kinerja/save/{$pegawai_id}/2") ?>">
            <div class="card mb-4">
                <div class="card-body">
                    <p><strong>OPD:</strong> <?= htmlspecialchars($pegawai->unor_induk_nama ?? 'Tidak tersedia') ?></p>
                    <p><strong>Nama Pimpinan:</strong> <?= htmlspecialchars($pegawai->nama) ?></p>
                </div>
            </div>

            <div class="card mb-4">
                <div class="card-header bg-primary text-white">
                    Kontribusi terhadap IKI OPD (Matriks Peran)
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered" id="iki-table">
                            <thead class="table-light">
                                <tr>
                                    <th rowspan="2" class="text-center" style="vertical-align: middle; width: 50px;">No</th>
                                    <th rowspan="2" class="text-center" style="vertical-align: middle; width: 300px;">
                                        Nama Pegawai<br>NIP<br>Jabatan
                                    </th>
                                    <th colspan="4" class="text-center" style="vertical-align: middle;">INDIKATOR</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if (!empty($matriks_data)): ?>
                                    <?php foreach ($matriks_data as $row): ?>
                                        <tr>
                                            <td class="text-center"><?= $row['no'] ?></td>
                                            <td class="align-middle">
                                                <strong><?= htmlspecialchars($row['nama']) ?></strong><br>
                                                <small class="text-muted"><?= htmlspecialchars($row['nip']) ?></small><br>
                                                <small><?= htmlspecialchars($row['jabatan']) ?></small><br>

                                                <?php if ($row['level_posisi'] == 'pimpinan'): ?>
                                                    <span class="badge bg-success">Pimpinan</span>
                                                <?php elseif ($row['level_posisi'] == 'middle'): ?>
                                                    <span class="badge bg-warning text-dark">ML</span>
                                                <?php else: ?>
                                                    <span class="badge bg-secondary">Bawahan</span>
                                                <?php endif; ?>

                                                <input type="hidden" name="nama_<?= $row['no'] ?>" value="<?= htmlspecialchars($row['nama']) ?>">
                                                <input type="hidden" name="nip_<?= $row['no'] ?>" value="<?= htmlspecialchars($row['nip']) ?>">
                                                <input type="hidden" name="jabatan_<?= $row['no'] ?>" value="<?= htmlspecialchars($row['jabatan']) ?>">
                                            </td>

                                            <td>
                                                <input type="text" name="Idis_<?= $row['no'] ?>" 
                                                       value="<?= htmlspecialchars($row['indicators']['Idis']) ?>" 
                                                       class="form-control form-control-sm">
                                            </td>
                                            <td>
                                                <input type="text" name="I.kin._<?= $row['no'] ?>" 
                                                       value="<?= htmlspecialchars($row['indicators']['I.kin.']) ?>" 
                                                       class="form-control form-control-sm">
                                            </td>
                                            <td>
                                                <input type="text" name="I.2.1_<?= $row['no'] ?>" 
                                                       value="<?= htmlspecialchars($row['indicators']['I.2.1']) ?>" 
                                                       class="form-control form-control-sm">
                                            </td>
                                            <td>
                                                <input type="text" name="dst_<?= $row['no'] ?>" 
                                                       value="<?= htmlspecialchars($row['indicators']['dst']) ?>" 
                                                       class="form-control form-control-sm">
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                <?php else: ?>
                                    <tr>
                                        <td colspan="6" class="text-center">Data bawahan tidak ditemukan.</td>
                                    </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <div class="d-flex gap-2 mt-3">
                <a href="<?= base_url("dialog_kinerja/template/{$pegawai_id}/1") ?>" class="btn btn-secondary">Back</a>
                <button type="submit" class="btn btn-primary">Simpan & Lanjut</button>
            </div>
        </form>
    </div>
</div>
