<?php
include '../../config/conn.php';

if ($_GET['aksi'] == '') { ?>
    <div class="col" role="main">
        <div class="">
            <div class="clearfix"></div>

            <div class="row">
                <div class="col-md-12 col-sm-12 col-xs-12">
                    <div class="x_panel">
                        <div class="x_title" style="text-transform: capitalize;">
                            <h2>Data Jenis Rumah Tahfidz <small></small></h2>

                            <div class="clearfix"></div>
                        </div>
                        <a class="btn btn-success btn-sm" data-toggle="modal" data-target="#modalAdd"><i class="glyphicon glyphicon-plus"></i>Tambah</a>
                        <div class="divider-dashed"></div>
                        <div class="x_content">

                            <table id="datatable" class="table table-striped table-bordered">
                                <thead>
                                    <tr>
                                        
                                        <th>ID</th>
                                        <th>Nama Kelas</th>
                                        <th>Jenis</th>
                                        <th width="50">Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $no = 0;
                                    $query = mysqli_query($conn, "SELECT * FROM jenis_rt ORDER BY id asc");
                                    while ($row = mysqli_fetch_array($query)) {
                                        $no++;
                                    ?>
                                        <tr>
                                            
                                            <td><?php echo $row['id']; ?></td>
                                            <td><?php echo $row['nama_jenis']; ?></td>
                                            <td><?php if($row['kategori']==1){echo "Rumah Tahfidz Pagi";}else{echo "Rumah Tahfidz Sore";} ?></td>
                                            <td><a class='open_modal btn  btn-default btn-xs' href="?module=<?php echo $_GET['module']; ?>&aksi=edit&id=<?php echo $row['id']; ?>"><i class="glyphicon glyphicon-edit"></i> Edit</a> </td>
                                        </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>
    </div>
    </div>
    </div><br>

    <!-- Modal -->
    <div class="modal fade" id="modalAdd" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">Tambah Data</h4>
                </div>
                <div class="modal-body">
                    <!-- start form for validation -->
                    <form action="?module=<?php echo $_GET['module']; ?>&aksi=simpan" enctype="multipart/form-data" method="POST">



                        <label for="nama">Nama Kelas * :</label>
                        <input type="text" class="form-control" name="kelas" autocomplete="off" required />
                        <label for="nama">Jenis Rumah Tahfidz * :</label>
                        <select id="heard" class="form-control" required name="jenis_rt">
                            <option value="" selected>- Pilih jenis -</option>
                            <option value="1" >Rumah tahfidz Pagi</option>
                            <option value="2" >Rumah tahfidz Sore</option>
                        </select>


                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default btn-sm" data-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-success btn-sm">Simpan</button>
                </div>
                </form>
            </div>
        </div>
    </div>


<?php  } elseif ($_GET['aksi'] == 'edit') {

    $id = $_GET['id'];
    $query = mysqli_query($conn, "SELECT * FROM jenis_donatur WHERE id ='$id'");
    $r = mysqli_fetch_array($query);

?>

    <div class="col" role="main">
        <div class="">
            <div class="clearfix"></div>

            <div class="row">
                <div class="col-md-12 col-sm-12 col-xs-12">
                    <div class="x_panel">
                        <div class="x_title" style="text-transform: capitalize;">
                            <h2>Edit Data <?php echo $_GET['module']; ?></h2>
                            <div class="clearfix"></div>
                        </div>
                        <div class="x_content">
                            <form action="?module=jenis-donasi&aksi=simpan_edit" enctype="multipart/form-data" method="POST">
                                <div class="row">

                                    <div class="col-md-6">
                                        <label for="id">ID :</label>
                                        <input type="text" class="form-control" disabled value="<?php echo $id; ?>" />
                                        <input type="hidden" class="form-control" name="id" value="<?php echo $id; ?>" />

                                        <label for="nama">Nama Kelas :</label>
                                        <input type="text" class="form-control" name="nama" value="<?php echo $r['nama_jenis']; ?>" />

                                        <select id="heard" class="form-control" required name="jenis_rt">
                                            <option value="" selected>- Pilih jenis -</option>
                                            <option value="1" >Rumah tahfidz Pagi</option>
                                            <option value="2" >Rumah tahfidz Sore</option>
                                        </select>


                                    </div>

                                </div>
                                <div class="ln_solid"></div>
                                <div class="form-group">
                                    <button type="button" class="btn btn-default btn-sm" onclick=self.history.back()>Batal</button>
                                    <button type="submit" class="btn btn-success btn-sm">Simpan</button><br>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

<?php } elseif ($_GET['aksi'] == 'simpan_edit') {
    $module = $_GET['module'];

    mysqli_query($conn, "UPDATE jenis_donatur SET nama_jenis = '$_POST[nama]' 
                                    WHERE id = '$_POST[id]'");

    echo "<script language='javascript'>document.location='?module=" . $module . "';</script>";
} elseif ($_GET['aksi'] == 'simpan') {
    $module = $_GET['module'];
    $password = md5($_POST['password']);
    mysqli_query($conn, "INSERT INTO jenis_rt (nama_jenis,kategori) VALUES('$_POST[kelas]','$_POST[jenis_rt]')");

    echo "<script language='javascript'>document.location='?module=" . $module . "';</script>";
} elseif ($_GET['aksi'] == 'hapus') {
    $module = $_GET['module'];
    $idd = $_GET['id'];
    $id = decrypt($idd);
    $query = mysqli_query($conn, "Delete FROM pegawai WHERE id_pegawai='$id'");
    echo "<script language='javascript'>document.location='?module=" . $module . "';</script>";
}
?>

<div class="modal fade" id="modal_delete">
    <div class="modal-dialog">
        <div class="modal-content" style="margin-top:100px;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" style="text-align:center;">Apakah anda yakin menghapus data ini ?</h4>
            </div>
            <div class="modal-footer" style="margin:0px; border-top:0px; text-align:center;">
                <a href="#" class="btn btn-danger btn-sm" id="delete_link">Hapus</a>
                <button type="button" class="btn btn-success btn-sm" data-dismiss="modal">Batal</button>
            </div>
        </div>
    </div>
</div>