<?php require "../fungsi.php";

?>
<!-- Begin Page Content -->
<div class="container-fluid">

    <!-- Page Heading -->
    <div class="d-sm-flex align-items-center justify-content-between mb-4">
        <h1 class="h3 mb-0 text-gray-800">Dokumen Standar Lain</h1>
    </div>

    <!-- DataTales Example -->
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary"><!-- Tombol Tambah Data -->
                <a href="?page=add_stdlain" class="btn btn-success mb-3">
                    <i class="fas fa-plus"></i> Tambah Data
                </a>
            </h6>
        </div>
        <div class="card-body">
            <div class="table-responsive">


                <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                    <thead>
                        <tr>
                            <th>No</th>
                            <th>Nama Dokumen</th>
                            <th>Deskripsi</th>
                            <th>Tahun Terbit</th>
                            <th>Aksi</th>
                        </tr>
                    </thead>
                    <tfoot>
                        <tr>
                            <th>No</th>
                            <th>Nama Dokumen</th>
                            <th>Deskripsi</th>
                            <th>Tahun Terbit</th>
                            <th>Aksi</th>
                        </tr>
                    </tfoot>
                    <tbody>
                        <?php
                        if (isset($_GET['id'])) {
                            $id = $_GET['id'];
                            $table = "dokumen"; // sesuaikan dengan nama tabel
                            $idColumn = "id"; // Ganti dengan kolom ID di tabel Anda
                            if (hapusDataInt($conn, $table, $idColumn, $id)) {
                                echo "<div class='alert alert-success' 
        role='alert'>Data berhasil dihapus.</div>";
                            } else {
                                echo "<div class='alert alert-danger' 
        role='alert'>Data gagal dihapus!</div>";
                            }
                        }
                        $jenis_dokumen = 'standar lain'; // Misalnya kita ingin mengambil dokumen dengan jenis 'stdlain'
                        $dokumen = ambilDokumenBerdasarkanJenis($conn, $jenis_dokumen);

                        $data = ambilDokumenBerdasarkanJenis($conn, $jenis_dokumen); //ambilData($conn, "dokumen");

                        if ($data === null) {
                            // Jika tidak ada data ditemukan
                            echo "<div class='alert alert-warning' role='alert'>Data belum ada.</div>";
                        } else {
                            $no = 1;
                            //while ($row = $data->fetch_assoc()) {
                            foreach ($data as $row) {
                        ?>
                                <tr>
                                    <td><?= $no ?></td>
                                    <td><?= $row['nama_dokumen'] ?></td>
                                    <td><?= $row['deskripsi'] ?></td>
                                    <td><?= $row['tahun_terbit'] ?></td>
                                    <td>
                                        <a href="?page=edit&id=<?= $row['id'] ?>" class="btn btn-warning btn-sm">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <a href="?page=stdlain&id=<?= $row['id'] ?>" class="btn btn-danger btn-sm" onclick="return confirm('Apakah Anda yakin ingin menghapus dokumen ini?')">
                                            <i class="fas fa-trash"></i>
                                        </a>
                                        <a href="<?= $row['link_dokumen'] ?>" target="_blank" class="btn btn-info btn-sm">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                    </td>
                                </tr>
                        <?php
                                $no++;
                            }
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

</div>
<!-- /.container-fluid -->