<?php
include '../../config/conn.php';
$module = $_GET['module'];


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>Infaq Harian 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>No</th>
                                        <th>ID Trasaksi</th>
                                        <th>tanggal</th>
                                        <th>Kelas</th>
                                        <th>Jumlah</th>
                                        <th>Keterangan</th>
                                        <?php if ($_SESSION['leveluser'] == 'admin' or $_SESSION['leveluser'] == 'user1') { ?>
                                            <th>Aksi</th>
                                        <?php } ?>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $no = 0;
                                    $hariini = date('Y-m-d');
                                    $query = mysqli_query($conn, "SELECT * FROM transaksi WHERE id_jenis='13' AND tanggal='$hariini' ORDER BY id_transaksi DESC");
                                    while ($row = mysqli_fetch_array($query)) {

                                        $jenrt = mysqli_query($conn, "SELECT nama_jenis FROM jenis_rt WHERE id='$row[id_jenis_rt]'");
                                        $jn = mysqli_fetch_array($jenrt);
                                        $no++;
                                    ?>
                                        <tr>
                                            <td><?php echo $no; ?></td>
                                            <td><?php echo $row['id_transaksi']; ?></td>
                                            <td><?php echo $row['tanggal']; ?></td>
                                            <td><?php echo $jn['nama_jenis']; ?></td>
                                            <td><?php echo $row['debit']; ?></td>
                                            <td><?php echo $row['keterangan']; ?></td>
                                            <?php if ($_SESSION['leveluser'] == 'admin' or $_SESSION['leveluser'] == 'user1') { ?>
                                                <td><a class='open_modal btn  btn-default btn-xs' href="?module=<?php echo $_GET['module']; ?>&aksi=edit&id=<?php echo $row['id_transaksi']; ?>"><i class="glyphicon glyphicon-edit"></i> Edit</a>
                                                    <a class="btn  btn-danger btn-xs" onclick="confirm_modal('?module=<?php echo $_GET['module']; ?>&aksi=hapus&id_tran=<?php echo $row['id_transaksi']; ?>');"><i class="glyphicon glyphicon-trash"></i> Hapus</a>
                                                </td>
                                            <?php } ?>
                                        </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>
                        <select id="heard" class="form-control" required name="jenis_rt">
                            <option value="" selected>- Pilih kelas -</option>
                            <?php
                            $query  = mysqli_query($conn, "SELECT * FROM jenis_rt order by id asc");
                            while ($r = mysqli_fetch_array($query)) {
                                echo "<option value=\"$r[id]\">$r[nama_jenis]</option>";
                            }
                            ?>
                        </select>
                        <label f>Tanggal * :</label>
                        <div class="input-group">
                            <div class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></div>
                            <input type="text" class="form-control " disabled autocomplete="off" value="<?php echo date('d-m-Y'); ?>" id="tanggal4" required>
                            <input type="hidden" value="<?php echo date('d-m-Y'); ?>" name="tanggal">
                        </div>
                        <label>Jenis Pembayaran * :</label>
                        <select id="heard" class="form-control" required name="jenis_kas">

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

                            $query2  = "SELECT * FROM jenis_kas ORDER BY id";
                            $tampil2 = mysqli_query($conn, $query2);
                            while ($w = mysqli_fetch_array($tampil2)) {
                                echo "<option value=\"$w[id]\">$w[nama]</option>";
                            }
                            ?>
                        </select>
                        <label>Jumlah * :</label>
                        <input type="number" class="form-control" autocomplete="off" name="jumlah" required />
                        <label>Keterangan :</label>
                        <textarea class="form-control" autocomplete="off" name="keterangan"></textarea>


                </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 transaksi WHERE id_transaksi ='$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">Nama Kelas * :</label>
                                        <select id="heard" class="form-control" required name="jenis_rt">
                                            <option value="" selected>- Pilih kelas -</option>
                                            <?php
                                            $query  = mysqli_query($conn, "SELECT * FROM jenis_rt order by id asc");
                                            while ($m = mysqli_fetch_array($query)) {
                                                if ($r['id_jenis_rt'] == $m['id']) {
                                                    $sel = "selected";
                                                } else {
                                                    $sel = "";
                                                }
                                                echo "<option $sel value=\"$m[id]\">$m[nama_jenis]</option>";
                                            }
                                            ?>
                                        </select>
                                        <label f>Tanggal * :</label>
                                        <div class="input-group">
                                            <div class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></div>
                                            <input type="text" class="form-control " autocomplete="off" value="<?php echo date('d-m-Y', strtotime($r['tanggal'])); ?>" id="tanggal2" name="tanggal" required>
                                        </div>
                                        <label>Jenis Pembayaran * :</label>
                                        <select id="heard" class="form-control" required name="jenis_kas">

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

                                            $query2  = "SELECT * FROM jenis_kas ORDER BY id";
                                            $tampil2 = mysqli_query($conn, $query2);
                                            while ($w = mysqli_fetch_array($tampil2)) {
                                                if ($r['jenis_kas'] == $w['id']) {
                                                    echo "<option value=\"$w[id]\" selected>$w[nama]</option>";
                                                } else {
                                                    echo "<option value=\"$w[id]\">$w[nama]</option>";
                                                }
                                            }
                                            ?>
                                        </select>
                                        <label>Jumlah * :</label>
                                        <input type="number" class="form-control" autocomplete="off" value="<?php echo $r['debit']; ?>" name="jumlah" required>
                                        <label>Keterangan :</label>
                                        <textarea class="form-control" autocomplete="off" name="keterangan"><?php echo $r['keterangan']; ?></textarea>
                                    </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') {

    $tgl = date('Y-m-d', strtotime($_POST['tanggal']));
    $save = mysqli_query($conn, "UPDATE transaksi SET tanggal = '$tgl',
                                            debit='$_POST[jumlah]',
                                            keterangan='$_POST[keterangan]',
                                            id_jenis_rt='$_POST[jenis_rt]',
                                            jenis_kas = '$_POST[jenis_kas]'
                                    WHERE id_transaksi = '$_POST[id]'");

    // echo "<script language='javascript'>document.location='?module=" . $module . "';</script>";
    if (!$save) {
        echo "GAGAL";
    } else {
       // historyedit($_POST['id'], $_SESSION['id'], $_SESSION['mac']);
       echo "<script language='javascript'>document.location='?module=" . $module . "';</script>";
    }
} elseif ($_GET['aksi'] == 'simpan') {
    $newID = id_transaksi();
    $tgl = date('Y-m-d', strtotime($_POST['tanggal']));

    $save = mysqli_query($conn, "INSERT INTO transaksi (id_transaksi,id_jenis,tanggal,debit,keterangan,id_jenis_rt,id_pegawai,jenis_kas)
    VALUES('$newID',13,'$tgl','$_POST[jumlah]','$_POST[keterangan]','$_POST[jenis_rt]','$_SESSION[id]','$_POST[jenis_kas]')");

     
    if (!$save) {
        echo "GAGAL";
    } else {
        // historysimpan($newID, $_SESSION['id'], $_SESSION['mac']);
        echo "<script language='javascript'>document.location='?module=" . $module . "';</script>";
    }
} elseif ($_GET['aksi'] == 'hapus') {

    $query = mysqli_query($conn, "DELETE FROM transaksi WHERE id_transaksi='$_GET[id_tran]'");
    // echo "<script language='javascript'>document.location='?module=" . $module . "';</script>";
    if (!$query) {
        echo "GAGAL";
    } else {
        //historyhapus($_GET['id_tran'], $_SESSION['id'], $_SESSION['mac']);
       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>