<?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-9 col-sm-12 col-xs-12">
                    <div class="x_panel">
                        <div class="x_title" style="text-transform: capitalize;">
                            <h2>Alamat mac perangkat yang diizinkan<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>MAC</th>
                                        <th>keterangan</th>
                                        <th width="50">Hapus akses</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $no = 0;
                                    $query = mysqli_query($conn, "SELECT * FROM mac_akses ORDER BY id asc");
                                    while ($row = mysqli_fetch_array($query)) {
                                        $no++;
                                    ?>
                                        <tr>
                                            <td><?php echo $no; ?></td>
                                            <td><?php echo $row['mac']; ?></td>
                                            <td><?php echo $row['keterangan']; ?></td>
                                            <td> <a class="btn  btn-danger btn-xs" onclick="confirm_modal('?module=<?php echo $_GET['module']; ?>&aksi=hapus&id=<?php echo $row['id']; ?>');"><i class="glyphicon glyphicon-trash"></i> Hapus</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 MAC perangkat</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">Alamat MAC * gunakan pemisah "-":</label>
                        <input type="text" class="form-control" name="mac" autocomplete="off" required />
                        <br>
                        <label for="nama">Keterangan:</label>
                        <input type="text" class="form-control" name="keterangan" autocomplete="off" required />


                </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 mac_akses (mac,keterangan) VALUES('$_POST[mac]','$_POST[keterangan]')");

    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 mac_akses 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>