<?php
$id_adm = $_SESSION['id'];
$jentran = '10';
include '../../config/conn.php';
?>


<?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;">
             
              <div class="clearfix">
                  
                  <!--pilih periode donatur-->
              <form action="?module=pembayaran-donatur-tdk-tetap&periode=y" enctype="multipart/form-data" method="POST">


                <div class="well" style="overflow: auto">
 <h2>Data Pembayaran Donatur Tidak Tetap</h2>
                  <div class="col-md-12 col-sm-12 col-xs-12" style="padding: 4px;">
                    <label for="nama">Pilih Periode :</label>
                  </div>

                  <div class="col-md-6 col-sm-12 col-xs-12" style="padding: 4px;">

                    <div class="input-group">
                      <div class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></div>
                      <input type="text" autocomplete="off" class="form-control " id="tanggal" required value="<?php if ($_GET['periode'] == 'y') {
                                                                                                                  echo $_POST['tanggal1'];
                                                                                                                } else {
                                                                                                                  echo "";
                                                                                                                } ?>" name="tanggal1">
                      <div class="input-group-addon" style="background:white;"><small>Sampai</small></i></div>
                      <div class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></div>
                      <input type="text" autocomplete="off" class="form-control " required value="<?php if ($_GET['periode'] == 'y') {
                                                                                                    echo $_POST['tanggal2'];
                                                                                                  } else {
                                                                                                    echo "";
                                                                                                  } ?>" id="tanggal2" name="tanggal2">
                    </div>
                  </div>

                  <div class="col-md-6 col-sm-12 col-xs-12" style="padding: 4px;">
                    <button type="submit" class="btn btn-primary btn-sm">Pilih</button>



              </form>
              
              <!--tutup periode-->
              </div>
              
              
              
              

              <div class="x_content">
                  

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

                <!--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>Nama</th>
                      <th>Jenis Donasi</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;

                  if ($_GET['periode'] == 'y') {
                       $tgl1 = date('Y-m-d', strtotime($_POST['tanggal1']));
                       $tgl2 = date('Y-m-d', strtotime($_POST['tanggal2']));
                          $query = mysqli_query($conn, "SELECT * FROM transaksi WHERE id_jenis='10' AND id_donatur='' AND tanggal BETWEEN '$tgl1' AND '$tgl2' 
                          ORDER BY tanggal ASC");
              
                  } else {
                    $hariini = date('Y-m-d');
                     $query = mysqli_query($conn, "SELECT * FROM transaksi WHERE id_jenis='10' AND id_donatur='' AND tanggal='$hariini' 
                          ORDER BY id_transaksi 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 echo $row['nama_donatur_tidak_tetap']; ?></td>
                        <td><?php
                            $keri  = mysqli_query($conn, "SELECT * FROM jenis_donatur WHERE id = '$row[jenis_donatur]' ");
                            while ($r = mysqli_fetch_array($keri)) {
                              echo $r['nama_jenis'];
                            }
                            ?></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> <?php
                          //akses edit bulan lalu
                          $blnakses = date('n', strtotime($row['tanggal']));
                          $thnakses = date('Y', strtotime($row['tanggal']));

                          $cek_akses = "SELECT * FROM akses_edit WHERE bln = '$blnakses' AND thn = '$thnakses'";
                          $cekakses1 = mysqli_query($conn, $cek_akses);
                          $count = mysqli_num_rows($cekakses1);
                          if (($blnakses >= date('n')) && ($thnakses >= date('Y'))) {

                          ?>

                            <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>
                         
                          <?php } else if ($count == 1) { ?>
                             <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>
                         
                          <?php

                          } else {
                            echo "<a data-toggle='modal' data-target='#akses_edit'><i>Akses dibatasi</i></a>";
                          }

                          ?>
                              
                 
                              
                              
                         </td>
                        <?php } ?>
                      </tr>
                    <?php } ?>
                  </tbody>
                </table>
              </div>
            </div>


          </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 "pembayaran-donatur-tdk-tetap"; ?>&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">Nama donatur * :</label>
            <input type="text" name="nama" class="form-control" value="">

            <label>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 Donatur *:</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>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 "pembayaran-donatur-tdk-tetap"; ?>&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">Nama Donatur * :</label>
              <input type="text" class="form-control" name="nama" value="<?php echo $data['nama_donatur_tidak_tetap']; ?>">

              <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>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'];
  }
  $save = mysqli_query($conn, "INSERT INTO transaksi(id_transaksi,
                                  nama_donatur_tidak_tetap,
                                  jenis_donatur,
                                  tanggal,
                                  debit,
                                  kredit,
                                  keterangan,id_jenis,id_pegawai,jenis_kas) VALUES('$_POST[id]',
                                                 '$_POST[nama]',
                                                 '$_POST[jenis_donatur]',
                                                 '$tgl',
                                                 '$jml',
                                                 '0',
                                                 '$_POST[keterangan]','10','$_SESSION[id]','$_POST[jenis_kas]')");




  $id_don = $_POST['id_donatur'];
  // echo "<script language='javascript'>document.location='?module=pembayaran-donatur-tdk-tetap';</script>";
  if ($save) {
    echo "<script>ySimpan('?module=pembayaran-donatur-tdk-tetap');</script>";
    historysimpan($_POST['id'], $_SESSION['id'], $_SESSION['mac'], $jentran);
  } else {
    echo "<script>nSimpan('?module=pembayaran-donatur-tdk-tetap');</script>";
  }
} elseif ($_GET['aksi'] == 'simpan_edit') {
  $module = $_GET['module'];
  $id_san = $_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]',
                                    nama_donatur_tidak_tetap =  '$_POST[nama]',
                                    jenis_kas = '$_POST[jenis_kas]',
                                    log_edit='$logedit' 
                                    WHERE id_transaksi = '$_POST[id]'") or die("GAGAL");


  // echo "<script language='javascript'>document.location='?module=pembayaran-donatur-tdk-tetap';</script>";
  if (!$save) {
    echo "<script>nSimpan('?module=pembayaran-donatur-tdk-tetap');</script>";
  } else {
    historyedit($_POST['id'], $_SESSION['id'], $_SESSION['mac'], $jentran);
    echo "<script>ySimpan('?module=pembayaran-donatur-tdk-tetap');</script>";
  }
} elseif ($_GET['aksi'] == 'hapus') {
  $module = $_GET['module'];
  $id = $_GET['id'];
  $query = mysqli_query($conn, "DELETE FROM transaksi WHERE id_donatur='' AND id_transaksi='$_GET[id_tran]'");
  // echo "<script language='javascript'>document.location='?module=$module';</script>";
  if (!$query) {
    echo "<script>nHapus('?module=pembayaran-donatur-tdk-tetap');</script>";
  } else {
    historyhapus($_GET['id_tran'], $_SESSION['id'], $_SESSION['mac'], $jentran);
    echo "<script>yHapus('?module=pembayaran-donatur-tdk-tetap');</script>";
  }
} ?>

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

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