<?php
$page_title = "Tata Cara Pengajuan";
include 'layout/header.php';
include 'layout/sidebar.php';
require_once '../config.php';

if ($_SESSION['level'] != 'administrator') {
    echo "<script>alert('Akses Ditolak!'); window.location.href='index.php';</script>";
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $aksi = $_POST['aksi'];
    $no_urut = $_POST['no_urut'];
    $judul = trim($_POST['judul']);
    $deskripsi = trim($_POST['deskripsi']);
    $url_file = trim($_POST['url_file']);

    if ($aksi == 'tambah') {
        $stmt = $pdo->prepare("INSERT INTO tbl_tatacara (no_urut, judul, deskripsi, url_file) VALUES (?, ?, ?, ?)");
        $stmt->execute([$no_urut, $judul, $deskripsi, $url_file]);
    } elseif ($aksi == 'edit') {
        $id = $_POST['id_tatacara'];
        $stmt = $pdo->prepare("UPDATE tbl_tatacara SET no_urut=?, judul=?, deskripsi=?, url_file=? WHERE id=?");
        $stmt->execute([$no_urut, $judul, $deskripsi, $url_file, $id]);
    } elseif ($aksi == 'hapus') {
        $id = $_POST['id_tatacara'];
        $stmt = $pdo->prepare("DELETE FROM tbl_tatacara WHERE id=?");
        $stmt->execute([$id]);
    }
    echo "<script>window.location.href='tatacara.php';</script>";
}

$tatacara = $pdo->query("SELECT * FROM tbl_tatacara ORDER BY no_urut ASC")->fetchAll();
?>

<div class="content-wrapper">
    <div class="content-header">
        <div class="container-fluid">
            <h1 class="m-0">Pengaturan Tata Cara Pengajuan</h1>
        </div>
    </div>

    <section class="content">
        <div class="container-fluid">
            <div class="card">
                <div class="card-header">
                    <button class="btn btn-primary" data-toggle="modal" data-target="#modalTambah"><i class="fas fa-plus"></i> Tambah Langkah</button>
                </div>
                <div class="card-body">
                    <table class="table table-bordered align-middle">
                        <thead class="bg-light">
                            <tr>
                                <th>No. Urut</th>
                                <th>Judul Langkah</th>
                                <th>Deskripsi</th>
                                <th>URL File / Link</th>
                                <th>Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach($tatacara as $tc): ?>
                            <tr>
                                <td class="text-center"><strong><?= $tc->no_urut ?></strong></td>
                                <td><?= htmlspecialchars($tc->judul) ?></td>
                                <td><?= nl2br(htmlspecialchars($tc->deskripsi)) ?></td>
                                <td><?= !empty($tc->url_file) ? '<a href="'.htmlspecialchars($tc->url_file).'" target="_blank">Lihat Link</a>' : '-' ?></td>
                                <td>
                                    <button class="btn btn-sm btn-info" onclick='editTC(<?= json_encode($tc) ?>)'><i class="fas fa-edit"></i> Edit</button>
                                    <form action="" method="POST" class="d-inline" onsubmit="return confirm('Hapus langkah ini?');">
                                        <input type="hidden" name="aksi" value="hapus">
                                        <input type="hidden" name="id_tatacara" value="<?= $tc->id ?>">
                                        <button class="btn btn-sm btn-danger"><i class="fas fa-trash"></i> Hapus</button>
                                    </form>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </section>
</div>

<!-- Modal -->
<div class="modal fade" id="modalTambah" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="tatacara.php" method="POST">
                <input type="hidden" name="aksi" id="f_aksi" value="tambah">
                <input type="hidden" name="id_tatacara" id="f_id">
                <div class="modal-header bg-primary text-white">
                    <h5 class="modal-title" id="m_title">Tambah Langkah</h5>
                    <button type="button" class="close text-white" data-dismiss="modal">&times;</button>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label>No. Urut (Step ke-)</label>
                        <input type="number" name="no_urut" id="f_no" class="form-control" required>
                    </div>
                    <div class="form-group">
                        <label>Judul Langkah</label>
                        <input type="text" name="judul" id="f_judul" class="form-control" required>
                    </div>
                    <div class="form-group">
                        <label>Deskripsi Instruksi</label>
                        <textarea name="deskripsi" id="f_deskripsi" class="form-control" rows="3" required></textarea>
                    </div>
                    <div class="form-group">
                        <label>URL / Link Download (Opsional)</label>
                        <input type="text" name="url_file" id="f_url" class="form-control" placeholder="http://...">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-primary">Simpan</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
function editTC(data) {
    document.getElementById('m_title').innerText = 'Edit Langkah';
    document.getElementById('f_aksi').value = 'edit';
    document.getElementById('f_id').value = data.id;
    document.getElementById('f_no').value = data.no_urut;
    document.getElementById('f_judul').value = data.judul;
    document.getElementById('f_deskripsi').value = data.deskripsi;
    document.getElementById('f_url').value = data.url_file;
    $('#modalTambah').modal('show');
}
$('#modalTambah').on('hidden.bs.modal', function () {
    document.getElementById('m_title').innerText = 'Tambah Langkah';
    document.getElementById('f_aksi').value = 'tambah';
    $(this).find('form')[0].reset();
});
</script>

<?php include 'layout/footer.php'; ?>
