<?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>Jenis 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>No.</th>
                                        <th>ID</th>
                                        <th>Nama Jenis</th>
                                        <th>Divisi/Unit</th>
                                        <th>Jenis Kas</th>
                                        <th>Kategori</th>
                                        <th width="50">Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $no = 0;
                                    $query = mysqli_query($conn, "SELECT * FROM jenis_transaksi ORDER BY id_divisi ASC, nama_jenis ASC");
                                    while ($row = mysqli_fetch_array($query)) {
                                        $no++;
                                    ?>
                                        <tr>
                                            <td><?php echo $no; ?></td>
                                            <td><?php echo $row['id_jenis']; ?></td>
                                            <td><?php echo namajenis($row['id_jenis']); ?></td>
                                            <td><?php echo namadivisi($row['id_divisi']); ?></td>
                                            <td><?php if($row['jenis']=="0"){ echo "Kas Masuk";} elseif($row['jenis']=="2") { echo "Kas Keluar";} ?></td>
                                            <td><?php echo namakategori($row['id_kategori']); ?></td>
                                             
                                              <?php if ($_SESSION['leveluser'] == 'admin') { ?>
                                               <td><a class='open_modal btn  btn-default btn-xs' href="?module=<?php echo $_GET['module']; ?>&aksi=edit&id=<?php echo $row['id_jenis']; ?>"><i class="glyphicon glyphicon-edit"></i> Edit</a> </td>
                                             <?php } else {
                                                            echo "<a data-toggle='modal' data-target='#akses_edit'><i>Akses dibatasi</i></a>";
                                                        } ?>
                                             
                                             
                                             
                                                     </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">Divisi * :</label>
                         <select id="heard" class="form-control" required name="id_divisi">

                                <option value="" selected>- Pilih Divisi -</option>
                                <?php

                                $query2  = "SELECT * FROM jenis_divisi ORDER BY id_divisi";
                                $tampil2 = mysqli_query($conn, $query2);
                                while ($w = mysqli_fetch_array($tampil2)) { ?>
                                    <option value="<?php echo $w['id_divisi']; ?>" <?php if ($data['id_divisi'] == $w['id_divisi']) {
                                                                                        echo "selected";
                                                                                    } ?>><?php echo $w['nama_divisi']; ?></option>";
                                <?php }
                                ?>
                            </select>

                            <label for="nama">Kategori * :</label>
                         <select id="heard" class="form-control" required name="id_kategori">

                                <option value="" selected>- Pilih Kategori -</option>
                                <?php

                                $query2  = "SELECT * FROM kategori_kas ORDER BY nama_kategori ASC";
                                $tampil2 = mysqli_query($conn, $query2);
                                while ($w = mysqli_fetch_array($tampil2)) { ?>
                                    <option value="<?php echo $w['id_kategori']; ?>" <?php if ($data['id_kategori'] == $w['id_kategori']) {
                                                                                        echo "selected";
                                                                                    } ?>><?php echo $w['nama_kategori']; ?></option>";
                                <?php }
                                ?>
                            </select>

                        <label for="nama">Pemasukan / Pengeluaran * :</label>
                         <select id="heard" class="form-control" required name="in_out">

                                <option value="" selected>- Pilih -</option>
                                <option value="0"> Pemasukan </option>
                                <option value="2"> Pengeluaran </option>
                            </select>
                        
                        <label for="nama">Nama Jenis Transaksi * :</label>
                        <input type="text" class="form-control" name="nama_jenis_transaksi" 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'] == 'edit') {

    $id = $_GET['id'];
    $query = mysqli_query($conn, "SELECT * FROM jenis_transaksi WHERE id_jenis ='$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=<?php echo $_GET['module']; ?>&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">Divisi * :</label>
                                         <select id="heard" class="form-control" required name="id_divisi">
                
                                                <option value="">- Pilih Divisi -</option>
                                                <?php
                
                                                $query2  = "SELECT * FROM jenis_divisi ORDER BY id_divisi";
                                                $tampil2 = mysqli_query($conn, $query2);
                                                while ($w = mysqli_fetch_array($tampil2)) { ?>
                                                    <option value="<?php echo $w['id_divisi']; ?>" <?php if ($r['id_divisi'] == $w['id_divisi']) {
                                                                                                        echo "selected";
                                                                                                    } ?>><?php echo $w['nama_divisi']; ?></option>";
                                                <?php }
                                                ?>
                                           </select>

                                           <label for="nama">Kategori * :</label>
                                         <select id="heard" class="form-control" required name="id_kategori">
                
                                                <option value="">- Pilih Kategori -</option>
                                                <?php
                
                                                $query2  = "SELECT * FROM kategori_kas ORDER BY nama_kategori ASC";
                                                $tampil2 = mysqli_query($conn, $query2);
                                                while ($w = mysqli_fetch_array($tampil2)) { ?>
                                                    <option value="<?php echo $w['id_kategori']; ?>" <?php if ($r['id_kategori'] == $w['id_kategori']) {
                                                                                                        echo "selected";
                                                                                                    } ?>><?php echo $w['nama_kategori']; ?></option>";
                                                <?php }
                                                ?>
                                           </select>

                                           <label for="nama">Pemasukan / Pengeluaran * :</label>
                                            <select id="heard" class="form-control" required name="in_out">

                                                    <option value="" selected>- Pilih -</option>
                                                    <option value="0" <?php if ($r['jenis']=="0"){ echo "selected"; } ?>> Pemasukan </option>
                                                    <option value="2" <?php if ($r['jenis']=="2"){ echo "selected"; } ?>> Pengeluaran </option>
                                            </select>
                                           
                                            <label for="nama">Nama Jenis Transaksi :</label>
                                        <input type="text" class="form-control" name="nama_jenis_transaksi" value="<?php echo $r['nama_jenis']; ?>" />


                                    </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_transaksi SET nama_jenis = '$_POST[nama_jenis_transaksi]', id_divisi = '$_POST[id_divisi]', jenis = '$_POST[in_out]' , id_kategori = '$_POST[id_kategori]' 
                                    WHERE id_jenis = '$_POST[id]'");

    echo "<script language='javascript'>document.location='?module=" . $module . "';</script>";
} elseif ($_GET['aksi'] == 'simpan') {
    $module = $_GET['module'];
       mysqli_query($conn, "INSERT INTO jenis_transaksi (nama_jenis,id_divisi,jenis,id_kategori) VALUES('$_POST[nama_jenis_transaksi]','$_POST[id_divisi]','$_POST[in_out]','$_POST[id_kategori]')");

    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>

<!-- modal akses edit-->
<div class="modal fade" id="akses_edit">
    <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;">Hubungi Administrator untuk merevisi</h4>
            </div>
            <div class="modal-footer" style="margin:0px; border-top:0px; text-align:center;">
                <button type="button" class="btn btn-success btn-sm" data-dismiss="modal">OK</button>
            </div>
        </div>
    </div>
</div>