<?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>Akses Diizikan Edit Transaksi<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 width="50">No</th>

                                        <th>Tahun</th>
                                        <th>Bulan</th>
                                        <th width="50">Hapus akses</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $no = 0;
                                    $query = mysqli_query($conn, "SELECT * FROM akses_edit ORDER BY id asc");
                                    while ($row = mysqli_fetch_array($query)) {
                                        $no++;
                                    ?>
                                        <tr>
                                            <td><?php echo $no; ?></td>
                                            <td><?php echo $row['thn']; ?></td>
                                            <td><?php $tampil_bulan  = mysqli_query($conn, "SELECT nama_bulan FROM bulan WHERE id_bulan = $row[bln]");
                                                while ($namabln = mysqli_fetch_array($tampil_bulan)) {
                                                    echo $namabln['nama_bulan'];
                                                } ?></td>
                                            <td><a class='open_modal btn  btn-danger btn-xs' href="?module=<?php echo $_GET['module']; ?>&aksi=hapus&id=<?php echo $row['id']; ?>"><i class="glyphicon glyphicon-remove"></i></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">



                        <div class="input-group">
                            <div class="input-group-addon">Bulan</div>
                            <select id="heard" class="form-control" name="bulan">
                                <option value="<?php echo date('m'); ?>" selected>--</option>
                                <?php
                                $query  = mysqli_query($conn, "SELECT * FROM bulan order by id_bulan asc");
                                while ($r = mysqli_fetch_array($query)) {
                                    echo "<option value=\"$r[id_bulan]\">$r[nama_bulan]</option>";
                                }
                                ?>
                            </select>

                            <div class="input-group-addon">Tahun</div>
                            <select id="heard" class="form-control" name="tahun">
                                <option value="<?php echo date('Y'); ?>" selected>--</option>
                                <?php

                                for ($tahun = 2018; $tahun <= 2030; $tahun++) {

                                    echo "<option value=\"$tahun\">$tahun</option>";
                                }
                                ?>
                            </select>
                        </div>


                </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'] == 'simpan') {
    $module = $_GET['module'];
    mysqli_query($conn, "INSERT INTO akses_edit (bln,thn) VALUES('$_POST[bulan]','$_POST[tahun]')");

    echo "<script language='javascript'>document.location='?module=" . $module . "';</script>";
} elseif ($_GET['aksi'] == 'hapus') {
    $module = $_GET['module'];
    $id = $_GET['id'];
    $query = mysqli_query($conn, "Delete FROM akses_edit WHERE id='$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>