<?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 <?php echo $_GET['module']; ?> <small></small></h2>
              <div class="clearfix"></div>
            </div>
            <a href="?module=donatur&aksi=tambah_donatur" class="btn btn-success btn-sm" data-toggle="" data-target=""><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>ID Donatur</th>
                    <!--<th>No Rekening</th>-->
                    <th>Nama</th>
                    <th>Alamat</th>
                    <th>No. HP</th>
                    <th>Jumlah Donasi</th>

                    <?php if ($_SESSION['leveluser'] == 'admin' or $_SESSION['leveluser'] == 'user1') { ?>
                      <th width="110">Aksi</th>
                    <?php } ?>
                  </tr>
                </thead>
                <tbody>
                  <?php
                  $no = 0;
                  $query = mysqli_query($conn, "SELECT * FROM donatur ORDER BY nama_donatur");
                  while ($row = mysqli_fetch_array($query)) {
                    $no++;
                  ?>
                    <tr>
                      <td><?php echo $no; ?></td>
                      <td><?php echo $row['id_donatur']; ?></td>
                      <!--<td><?php //echo $row['no_rekening'];
                              ?></td>-->
                      <td><?php echo $row['nama_donatur']; ?></td>
                      <td><?php echo $row['alamat']; ?></td>
                      <td><?php echo $row['no_hp']; ?></td>
                      <td><?php echo rupiah($row['biaya_donasi']); ?></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_donatur']; ?>"><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=<?php echo $row['id_donatur'] ?>   ');"><i class="glyphicon glyphicon-trash"></i> Hapus</a></td>
                      <?php } ?>
                    </tr>
                  <?php } ?>
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>




<?php  } elseif ($_GET['aksi'] == 'edit') {
  $id = $_GET['id'];
  //$id = decrypt($idd);
  $query = mysqli_query($conn, "SELECT * FROM donatur WHERE id_donatur='$id'");
  $r = mysqli_fetch_array($query);
  $tgl = date('d-m-Y', strtotime($r['tanggal_lahir']));
  $tgl1 = date('d-m-Y', strtotime($r['tanggal_masuk']));
?>

  <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=donatur&aksi=simpan_edit" enctype="multipart/form-data" method="POST">
                <div class="row">
                  <div class="col-md-12">
                    <?php include 'foto.php'; ?>
                  </div>

                  <div class="col-md-6">
                    <label for="id">ID donatur :</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 :</label>
                    <input type="text" autocomplete="off" class="form-control" name="nama" value="<?php echo $r['nama_donatur']; ?>">
                    <label for="nama">NO. HP :</label>
                    <input type="text" autocomplete="off" class="form-control" name="no_hp" value="<?php echo $r['no_hp']; ?>">
                    <label for="nama">Alamat :</label>
                    <textarea autocomplete="off" class="form-control" name="alamat"><?php echo $r['alamat']; ?></textarea>
                    <label style="color: darkred;">Jumlah Donasi :</label>
                    <input type="number" class="form-control" autocomplete="off" name="biaya_donasi" value="<?php echo $r['biaya_donasi']; ?>" required>
                  </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" name="upload" onclick="return confirm('Apakah yakin akan diedit?')" class="btn btn-success btn-sm">Simpan</button><br>
                </div><br><br>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <script type="text/javascript">
    var uploadField = document.getElementById("gambar");
    uploadField.onchange = function() {
      if (this.files[0].size > 4000000) { // ini untuk ukuran 4mb, 1000000 untuk 1 MB.
        alert("Maaf. File Terlalu Besar ! Maksimal Upload 4 MB");
        this.value = "";
      };
    };
  </script>


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

  include 'acak_id.php';
  while ($cekid >= 1) {
    include 'acak_id.php';
  }

?>

  <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>TAMBAH DATA donatur</h2>
              <div class="clearfix"></div>
            </div>
            <div class="x_content">
              <form action="?module=donatur&aksi=simpan" enctype="multipart/form-data" method="POST">
                <div class="row">
                  <div class="col-md-6">
                    <label for="id">ID donatur :</label>
                    <input type="text" class="form-control" disabled value="<?php echo $newID; ?>">
                    <input type="hidden" class="form-control" name="id" value="<?php echo $newID; ?>">
                    <label for="nama">Nama :</label>
                    <input type="text" autocomplete="off" class="form-control" name="nama" value="">
                    <label for="nama">NO. HP :</label>
                    <input type="text" autocomplete="off" class="form-control" name="no_hp" value="">
                    <label for="nama">Alamat :</label>
                    <textarea autocomplete="off" class="form-control" name="alamat" value=""></textarea>
                    <label style="color: darkred;">Jumlah Donasi :</label>
                    <input type="number" class="form-control" autocomplete="off" name="biaya_donasi" required>
                  </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" onclick="return confirm('Apakah yakin akan disimpan?')" class="btn btn-success btn-sm">Simpan</button><br>
                </div><br><br>
              </form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <script type="text/javascript">
    var uploadField = document.getElementById("gambar");
    uploadField.onchange = function() {
      if (this.files[0].size > 4000000) { // ini untuk ukuran 4mb, 1000000 untuk 1 MB.
        alert("Maaf. File Terlalu Besar ! Maksimal Upload 4 MB");
        this.value = "";
      };
    };
  </script>
<?php

} elseif ($_GET['aksi'] == 'simpan') {
  $module = $_GET['module'];
  $id = $_POST['id'];



  $save = mysqli_query($conn, "INSERT INTO donatur(id_donatur,
                                  nama_donatur,
                                  alamat,
                                  no_hp,biaya_donasi) VALUES('$id',
                                                 '$_POST[nama]',
                                                 '$_POST[alamat]',
                                                 '$_POST[no_hp]',
                                                 '$_POST[biaya_donasi]'
                                                 )");




  if (!$save) {
    echo "<script language='javascript'>document.location='?module=" . $module . "';</script>";
  } else {
    historydatasimpan($_SESSION['id'], $_SESSION['mac'], $id);
    echo "<script language='javascript'>document.location='?module=" . $module . "';</script>";
  }
} elseif ($_GET['aksi'] == 'simpan_edit') {
  $module = $_GET['module'];
  $tanggal = $_POST['tanggal_lahir'];
  $password   = md5($_POST['password']);
  $tgl = date('Y-m-d', strtotime($tanggal));
  $tgl1 = date('Y-m-d', strtotime($_POST['tanggal_masuk']));

  $query = mysqli_query($conn, "UPDATE donatur SET nama_donatur = '$_POST[nama]',
                                    alamat = '$_POST[alamat]',
                                    no_hp = '$_POST[no_hp]',
                                    biaya_donasi = '$_POST[biaya_donasi]' 
                                    WHERE id_donatur = '$_POST[id]'");
  $id = $_POST['id'];
  if ($query) {
    historydataedit($_SESSION['id'], $_SESSION['mac'], $id);
    echo "<script language='javascript'>document.location='?module=" . $module . "&aksi=edit&id=" . $_POST['id'] . "';</script>";
  } else {
    echo "GAGAL EDIT";
  }
} elseif ($_GET['aksi'] == 'hapus') {
  $module = $_GET['module'];
  $id = $_GET['id'];
  //$id = decrypt($idd);
  if ($id == "") {
    echo "gagal hapus";
  } else {
    $query = mysqli_query($conn, "Delete FROM donatur WHERE id_donatur='$id'");
    historydatahapus($_SESSION['id'], $_SESSION['mac'], $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>