<?php
$id_adm = $_SESSION['id'];
$jentran = '10';
include '../../config/conn.php';
if ($_GET['aksi'] == '') {

  if ($_GET['id'] == "") {
    $idd = $_POST['id'];
  } else {
    $idd = $_GET['id'];
  }
  $id = $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" style="padding-bottom:50px;">
            <div class="x_title" style="text-transform: capitalize;">
              <h2>Data Pembayaran</h2>
              <div class="clearfix"></div>
            </div>
            <div class="x_content">
              <div class="row">
                <div class="col-md-6">
                  <label for="id">ID donatur :</label>
                  <input type="text" class="form-control" disabled value="<?php echo $r['id_donatur']; ?>">
                  <input type="hidden" class="form-control" name="id" value="<?php echo $r['id_donatur']; ?>">

                  <label for="nama">Nama :</label>
                  <input type="text" class="form-control" name="nama" disabled value="<?php echo $r['nama_donatur']; ?>">

                  <label for="nama">No.HP :</label>
                  <input type="text" class="form-control" name="no_hp" disabled value="<?php echo $r['no_hp']; ?>">

                  <label for="nama">Alamat :</label>
                  <input type="text" class="form-control" name="alamat" disabled value="<?php echo $r['alamat']; ?>">


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

                <a class="btn btn-success btn-sm" data-toggle="modal" id="butt" data-target="#modalAdd"><i class="glyphicon glyphicon-plus"></i> Tambah Pembayaran</a>


                <!--<button type="submit" onclick="return confirm('Apakah yakin akan diedit?')" class="btn btn-success btn-sm">Simpan</button><br>-->
              </div>
            </div>
            <!--tampil data pembayaran-->
            <table id="datatable" class="table table-striped table-bordered">
              <thead>
                <tr>
                  <th width="10px">No</th>
                  <th width="110px">Kode Transaksi</th>
                  <th>Tanggal</th>
                  <th>Jenis Donasi</th>
                  <th>bulan</th>
                  <th>tahun</th>
                  <th>Jumlah</th>
                  <th>Jenis Kas</th>
                  <th>Keterangan</th>
                  <th>Cetak</th>
                  <?php if ($_SESSION['leveluser'] == 'admin' or $_SESSION['leveluser'] == 'user1') { ?>

                    <th width="110px">Aksi</th>
                  <?php } ?>
                </tr>
              </thead>
              <tbody>
                <?php
                $no = 0;


                $query = mysqli_query($conn, "SELECT * FROM transaksi WHERE id_jenis='10' AND id_donatur='$id' ORDER BY tanggal DESC");
                while ($row = mysqli_fetch_array($query)) {
                  $no++;
                ?>
                  <tr>
                    <td><?php echo $no; ?></td>
                    <td><?php echo $row['id_transaksi']; ?></td>
                    <td><?php echo $row['tanggal']; ?></td>

                    <td><?php
                        $keri  = mysqli_query($conn, "SELECT * FROM jenis_donatur WHERE id = '$row[jenis_donatur]' ");
                        while ($jendon = mysqli_fetch_array($keri)) {
                          echo $jendon['nama_jenis'];
                        }
                        ?></td>
                    <td><?php echo namabulan($row['spp_bulan']); ?></td>
                    <td><?php echo $row['spp_tahun']; ?></td>
                    <td><?php echo "Rp." . rupiah($row['debit']); ?></td>
                    <td>
                      <!-- jenis kas -->
                      <?php
                      $jeniskas = mysqli_query($conn, "SELECT * FROM jenis_kas WHERE id='$row[jenis_kas]'");
                      $jekas = mysqli_fetch_array($jeniskas);
                      echo $jekas['nama'];
                      ?>
                    </td>
                    <td><?php echo $row['keterangan']; ?></td>
                    <td width="50px"><a class="btn btn-primary btn-xs" onclick="window.open('component/com_pembayaran_donatur/cetak_donatur.php?id_tran=<?php echo $row['id_transaksi']; ?>','nama window','width=1000,height=500,toolbar=no,location=no,directories=no,status=no,menubar=no, scrollbars=no,resizable=yes,copyhistory=no')"><i class="glyphicon glyphicon-print"></i></a></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=form_edit&id_tran=<?php echo $row['id_transaksi']; ?>&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']; ?>&id_tran=<?php echo $row['id_transaksi']; ?>');"><i class="glyphicon glyphicon-trash"></i> Hapus</a>
                      </td>
                    <?php } ?>
                  </tr>
                <?php } ?>
              </tbody>
            </table>

            <?php include 'tagihan_donatur_tetap.php' ?>


          </div>
        </div>
      </div>
    </div>
  </div>




  <!--form tambah pembayaran-->
  <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 Pembayaran Donatur</h4>
        </div>
        <div class="modal-body">
          <form action="?module=<?php echo "pilih-donatur"; ?>&aksi=simpan" enctype="multipart/form-data" method="POST">

            <label for="id">ID Transaksi * :</label>
            <input type="text" class="form-control" disabled value="<?php echo id_transaksi(); ?>">
            <input type="hidden" class="form-control" name="id" value="<?php echo id_transaksi(); ?>">
            <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_donatur" value="<?php echo $id; ?>">

            <label f>Tanggal pembayaran * :</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 Donasi *:</label>
            <select id="heard" class="form-control" required name="jenis_donatur">
              <option value="" selected>- Pilih Jenis -</option>
              <?php
              $query  = mysqli_query($conn, "SELECT * FROM jenis_donatur order by id asc");
              while ($r = mysqli_fetch_array($query)) {
                echo "<option value=\"$r[id]\">$r[nama_jenis]</option>";
              }
              ?>
            </select>

            <label>Pembayaran Bulan/Tahun *:</label>
            <select id="heard" class="form-control" required name="bulan">
              <option value="" selected>- Pilih bulan -</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>
            <select id="heard" class="form-control" required name="tahun">
              <option value="" selected>- Pilih tahun -</option>
              <?php
              for ($tahun = 2018; $tahun <= 2025; $tahun++) {
                echo "<option value=\"$tahun\">$tahun</option>";
              }
              ?>
            </select>

            <label>Jumlah pembayaran *:</label>

            <script type="text/javascript">
              function changetextbox() {
                if (document.getElementById("jml_auto").value != 0) {
                  document.getElementById("jumlah_bayar").disabled = true;
                  document.getElementById("jumlah_bayar").value = '';
                } else {
                  document.getElementById("jumlah_bayar").disabled = false;

                }
              }
            </script>

            <select onchange="changetextbox();" id="jml_auto" class="form-control" name="jml_auto">
              <option value=0>-input manual-</option>
              <option value=850000>850.000</option>
              <option value=800000>800.000</option>
              <option value=750000>750.000</option>
              <option value=700000>700.000</option>
              <option value=650000>650.000</option>
              <option value=600000>600.000</option>
              <option value=550000>550.000</option>
              <option value=500000>500.000</option>
              <option value=450000>450.000</option>
              <option value=400000>400.000</option>
              <option value=350000>350.000</option>
              <option value=300000>300.000</option>
              <option value=250000>250.000</option>
              <option value=200000>200.000</option>
              <option value=150000>150.000</option>
              <option value=100000>100.000</option>
              <option value=50000>50.000</option>


            </select>


            <input type="number" class="form-control" autocomplete="off" id="jumlah_bayar" name="jumlah_bayar" required>

            <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 for="alamat">Keterangan :</label>
            <textarea class="form-control" 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>
  <!-- Modal Javascript -->
  <script type="text/javascript">
    //window.setTimeout(function() {
    //    document.getElementById("butt").click();
    // }, 500); // 1200000 = seconds*1000
  </script>

  <!--untuk edit data spp-->
<?php } elseif ($_GET['aksi'] == 'form_edit') { ?>
  <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">

            <form action="?module=<?php echo "pilih-donatur"; ?>&aksi=simpan_edit" enctype="multipart/form-data" method="POST">
              <?php


              $query = "SELECT * FROM transaksi WHERE id_transaksi='$_GET[id_tran]' ";
              $hasil = mysqli_query($conn, $query);
              $data = mysqli_fetch_array($hasil);

              ?>
              <label for="id">ID Transaksi * :</label>
              <input type="text" class="form-control" disabled value="<?php echo $_GET['id_tran'];
                                                                      ?>">
              <input type="hidden" class="form-control" name="id" value="<?php echo $_GET['id_tran']; ?>">
              <label for="id">ID donatur * :</label>
              <input type="text" class="form-control" disabled value="<?php echo $data['id_donatur']; ?>">
              <input type="hidden" class="form-control" name="id_donatur" value="<?php echo $data['id_donatur']; ?>">

              <label f>Tanggal pembayaran * :</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($data['tanggal'])); ?>" id="tanggal4" name="tanggal" required>
              </div>

              <label>Jenis Donasi *:</label>
              <select id="heard" class="form-control" required name="jenis_donatur">

                <?php
                $keri  = mysqli_query($conn, "SELECT * FROM jenis_donatur  order by id asc");
                while ($r = mysqli_fetch_array($keri)) { ?>
                  <option value="<?php echo $r['id']; ?>" <?php if ($r['id'] == $data['jenis_donatur']) {
                                                            echo "selected";
                                                          } ?>><?php echo $r['nama_jenis']; ?></option>

                <?php }  ?>
              </select>

              <label>Pembayaran Bulan/Tahun *:</label>
              <select id="heard" class="form-control" required name="bulan">
                <option value="" selected>- Pilih bulan -</option>
                <?php
                $query  = mysqli_query($conn, "SELECT * FROM bulan order by id_bulan asc");
                while ($r = mysqli_fetch_array($query)) {
                  if ($r['id_bulan'] == $data['spp_bulan']) {
                    echo "<option value=\"$r[id_bulan]\" selected>$r[nama_bulan]</option>";
                  } else {
                    echo "<option value=\"$r[id_bulan]\" >$r[nama_bulan]</option>";
                  }
                }
                ?>
              </select>
              <select id="heard" class="form-control" required name="tahun">
                <option value="" selected>- Pilih tahun -</option>
                <?php
                for ($tahun = 2018; $tahun <= 2025; $tahun++) {

                  if ($tahun == $data['spp_tahun']) {
                    echo "<option value=\"$tahun\" selected>$tahun</option>";
                  } else {
                    echo "<option value=\"$tahun\" >$tahun</option>";
                  }
                }
                ?>
              </select>

              <label>Jumlah pembayaran *:</label>
              <input type="number" class="form-control" autocomplete="off" name="jumlah_bayar" value="<?php echo $data['debit']; ?>" required>


              <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)) {
                ?>
                  <option value="<?php echo $w['id']; ?>" <?php if ($data['jenis_kas'] == $w['id']) {
                                                            echo "selected";
                                                          } ?>><?php echo $w['nama']; ?></option>";
                <?php
                }
                ?>
              </select>

              <label for="alamat">Keterangan :</label>
              <textarea class="form-control" name="keterangan"><?php echo $data['keterangan']; ?></textarea>


          </div>
          <div class="modal-footer">
            <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>
          </div>
          </form>

        </div>
      </div>
    </div>
  </div>
  </div><br><br><br>





<?php } elseif ($_GET['aksi'] == 'simpan') {
  $module = $_GET['module'];
  $tgl = date('Y-m-d', strtotime($_POST['tanggal']));
  if ($_POST['jumlah_bayar'] == "") {
    $jml = $_POST['jml_auto'];
  } else {
    $jml = $_POST['jumlah_bayar'];
  }
  $s = mysqli_query($conn, "INSERT INTO transaksi(id_transaksi,
                                  id_donatur,
                                  jenis_donatur,
                                  tanggal,
                                  debit,
                                  kredit,
                                  spp_bulan,
                                  spp_tahun,
                                  keterangan,id_jenis,id_pegawai,jenis_kas) VALUES('$_POST[id]',
                                                 '$_POST[id_donatur]',
                                                 '$_POST[jenis_donatur]',
                                                 '$tgl',
                                                 '$jml',
                                                 '0',
                                                 '$_POST[bulan]',
                                                 '$_POST[tahun]',
                                                 '$_POST[keterangan]','10','$_SESSION[id]','$_POST[jenis_kas]')");




  $id_don = $_POST['id_donatur'];
  // echo "<script language='javascript'>document.location='?module=pilih-donatur&id=$id_don';</script>";
  if ($s) {
    historysimpan($_POST['id'], $_SESSION['id'], $_SESSION['mac'], $jentran);
    echo "<script>ySimpan('?module=pilih-donatur&id=$id_don');</script>";
  } else {

    echo "<script>nSimpan('?module=pilih-donatur&id=$id_don');</script>";
  }
} elseif ($_GET['aksi'] == 'simpan_edit') {
  $module = $_GET['module'];
  $id_don = $_POST['id_donatur'];
  $tgl1 = date('Y-m-d', strtotime($_POST['tanggal']));

  //log edit 
  $query = "SELECT * FROM transaksi WHERE id_transaksi='$_POST[id]' ";
  $hasil = mysqli_query($conn, $query);
  $data = mysqli_fetch_array($hasil);


  $logedit = $data['log_edit'] . "[" . $_SESSION['id'] . "~" . date('Y-m-d H:i:s') . "]";

  $save = mysqli_query($conn, "UPDATE transaksi SET tanggal = '$tgl1',
                                    debit = '$_POST[jumlah_bayar]',
                                    keterangan = '$_POST[keterangan]',
                                    jenis_donatur = '$_POST[jenis_donatur]',
                                    spp_bulan = '$_POST[bulan]',
                                    spp_tahun = '$_POST[tahun]',
                                    jenis_kas = '$_POST[jenis_kas]',
                                    log_edit='$logedit' 
                                    WHERE id_transaksi = '$_POST[id]'") or die("GAGAL");


  if (!$save) {
    echo "<script>nSimpan('?module=pilih-donatur&id=$id_don');</script>";
  } else {
    historyedit($_POST['id'], $_SESSION['id'], $_SESSION['mac'], $jentran);
    echo "<script>ySimpan('?module=pilih-donatur&id=$id_don');</script>";
  }


  // echo "<script language='javascript'>document.location='?module=pilih-donatur&id=$id_san';</script>";
} elseif ($_GET['aksi'] == 'hapus') {
  $module = $_GET['module'];
  $id = $_GET['id'];
  $query = mysqli_query($conn, "DELETE FROM transaksi WHERE id_donatur='$id' AND id_transaksi='$_GET[id_tran]'");
  if (!$query) {
    echo "<script>nHapus('?module=pilih-donatur&id=$id');</script>";
  } else {
    historyhapus($_GET['id_tran'], $_SESSION['id'], $_SESSION['mac'], $jentran);
    echo "<script>yHapus('?module=pilih-donatur&id=$id');</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>