<?php
include '../../config/conn.php';
if ($_GET['aksi'] == '') { 
  
  $query = mysqli_query($conn, "SELECT * FROM perencanaan_anggaran_bulan WHERE id_anggaran='$_GET[id_anggaran]'");

  while ($row = mysqli_fetch_array($query)) {
    $xdivisi=$row['id_divisi']; //untuk mengambil jenis transaksi sebagai pembeda jenis transaksi pengeluaran
    $xbln=$row['bln'];
    $xthn=$row['thn'];
    $xidanggaran=$row['id_anggaran'];
    $xvalid=$row['valid'];
  }

  
  
  ?>
  <div class="col" role="main">
    <div class="">
      <div class="clearfix"></div>

      <div class="row">
        <div class="col-md-6 col-sm-12 col-xs-12">
          <div class="x_panel">
            <div class="x_title" style="text-transform: capitalize;">
              <h2 style="color:black;font-size:18px">Rekap Per Jenis Transaksi Perencanaan Anggaran <div style="color:blue;"><?php echo namadivisi($xdivisi)." ".namabulan($xbln)." ".$xthn ?></div></h2>

              <div class="clearfix"></div>
            </div>
             <div class="divider-dashed"></div>
            <div class="x_content">
            <label style="color:black;font-size:18px" for="alamat">Pendapatan</label>
              <table style="color:black;font-size:18px" id="datatable000" class="table table-striped table-bordered">
                <thead>
                  <tr>
                    <th width="50">No</th>
                    <th>Id Anggaran</th>
                    <th>Jenis Transaksi</th>
                    <th>Total</th>
                  </tr>
                </thead>
                <tbody>
                  <?php
                  $no = 0;
                  $query = mysqli_query($conn, "SELECT 
                  pda.id_jenis, 
                  SUM(pda.jml) AS total_jml
              FROM 
                  perencanaan_detail_anggaran pda
              JOIN 
                  jenis_transaksi jt ON pda.id_jenis = jt.id_jenis
              WHERE 
                  pda.id_anggaran = '$xidanggaran'
                  AND jt.jenis = 0
              GROUP BY 
                  pda.id_jenis
              ORDER BY 
                  pda.id_jenis ASC;
              ;");
                  $total=0;
                  while ($row = mysqli_fetch_array($query)) {
                    $no++;
                  ?>
                    <tr>
                      <td><?php echo $no; ?></td>
                      <td><?php echo $xidanggaran; ?></td>
                      <td><?php echo namajenis($row['id_jenis'])."".jeniskas_in_out($row['id_jenis']);?></td>
                      <td><?php echo rupiah($row['total_jml']); ?></th> </tr>
                  <?php 
                $total=$total+$row['total_jml'];
                } ?>
                </tbody>
                <thead>
                  <tr>
                    <th colspan=3 style="text-align:center;">Jenis Transaksi</th>
                    <th><?php echo rupiah($total); ?></th>
                  </tr>
                </thead>
              </table> <br>

              <label style="color:black;font-size:18px" for="alamat">Pengeluaran</label>
              <table style="color:black;font-size:18px" id="datatable000" class="table table-striped table-bordered">
                <thead>
                  <tr>
                    <th width="50">No</th>
                    <th>Id Anggaran</th>
                    <th>Jenis Transaksi</th>
                    <th>Total</th>
                  </tr>
                </thead>
                <tbody>
                  <?php
                  $no = 0;
                  $query = mysqli_query($conn, "SELECT 
                  pda.id_jenis, 
                  SUM(pda.jml) AS total_jml
              FROM 
                  perencanaan_detail_anggaran pda
              JOIN 
                  jenis_transaksi jt ON pda.id_jenis = jt.id_jenis
              WHERE 
                  pda.id_anggaran = '$xidanggaran'
                  AND jt.jenis = 2
              GROUP BY 
                  pda.id_jenis
              ORDER BY 
                  pda.id_jenis ASC;
              ;");
                  $total=0;
                  while ($row = mysqli_fetch_array($query)) {
                    $no++;
                  ?>
                    <tr>
                      <td><?php echo $no; ?></td>
                      <td><?php echo $xidanggaran; ?></td>
                      <td><?php echo namajenis($row['id_jenis'])."".jeniskas_in_out($row['id_jenis']);?></td>
                      <td><?php echo rupiah($row['total_jml']); ?></th> </tr>
                  <?php 
                $total=$total+$row['total_jml'];
                } ?>
                </tbody>
                <thead>
                  <tr>
                    <th colspan=3 style="text-align:center;">Total</th>
                    <th><?php echo rupiah($total); ?></th>
                  </tr>
                </thead>
              </table>
            </div>
          </div>
        </div>

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

  <div class="col" role="main">
    <div class="">
      <div class="clearfix"></div>

      <div class="row">
        <div class="col-md-11 col-sm-12 col-xs-12">
          <div class="x_panel">
            <div class="x_title" style="text-transform: capitalize;">
              <h2>Detail Perencanaan Anggaran <div style="color:blue;"><?php echo namadivisi($xdivisi)." ".namabulan($xbln)." ".$xthn ?></div></h2>

              <div class="clearfix"></div>
            </div>
             
            <?php
            if($xvalid=="0" OR $xvalid=="3") { ?>
             <a class="btn btn-success btn-sm" data-toggle="modal" data-target="#modalAdd"><i class="glyphicon glyphicon-plus"></i>Tambah</a><br>
             <?php
            echo "<a class=\"btn  btn-primary btn-xs\" onclick=\"confirm_modal('?module=perencanaan&aksi=ubahstatus&valid=2&id_anggaran=$row[id_anggaran]');\">Ajukan Validasi</a>";
            } elseif($xvalid=="1") { ?>
            <div style="color:red;">DATA SUDAH DIVALIDASI</div>
            <?php
              } elseif($xvalid=="2") {
                ?>
                <div style="color:red;">DATA SEDANG DITINJAU</div>
                <?php

              }
            ?>
            <a class='open_modal btn  btn-default btn-xs' href="?module=perencanaan"><i class="glyphicon glyphicon-edit"></i> Kembali</a>
            <a class="btn btn-default btn-sm" data-toggle="modal" data-target="#modalinfo"><i class="glyphicon glyphicon-question-sign"></i> Info</a>
                       
            
            <div class="divider-dashed"></div>
            <div class="x_content">

              <table style="color:black;font-size:18px" id="datatable" class="table table-striped table-bordered">
                <thead>
                  <tr>
                    <th width="50">No</th>
                    <th>Id Anggaran</th>
                    <th>Divisi</th>
                    <th>Bulan</th>
                    <th>Tahun</th>
                    <th>Jenis Transaksi</th>
                    <th>Keterangan</th>
                    <th>Jumlah</th>
                    <th>Aksi</th>
                  </tr>
                </thead>
                <tbody>
                  <?php
                  $no = 0;
                  $query = mysqli_query($conn, "SELECT * FROM perencanaan_detail_anggaran WHERE id_anggaran='$xidanggaran' ORDER BY id desc");
                  while ($row = mysqli_fetch_array($query)) {
                    $no++;
                  ?>
                    <tr>
                      <td><?php echo $no; ?></td>
                      <td><?php echo $row['id_anggaran']; ?></td>
                      <td><?php echo namadivisi($xdivisi); ?></td>
                      <td><?php echo namabulan($xbln); ?></td>
                      <td><?php echo $xthn; ?></td>
                      <td><?php echo namajenis($row['id_jenis'])."".jeniskas_in_out($row['id_jenis']);  ?></td>
                      <td><?php echo $row['ket']; ?></td>
                      <th><?php echo rupiah($row['jml']); ?></th>
                      <td>
                      <?php if($xvalid=="0" OR $xvalid=="3") { ?>
                        <a class='open_modal btn  btn-default btn-xs' href="?module=<?php echo $_GET['module']; ?>&aksi=edit&id=<?php echo $row['id']; ?>&id_divisi=<?php echo $xdivisi; ?>&bln=<?php echo $xbln; ?>&thn=<?php echo $xthn; ?>"><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']; ?>&id_anggaran=<?php echo $xidanggaran; ?>');"><i class="glyphicon glyphicon-trash"></i> Hapus</a>

                      <?php } else {?>
                      <div style="color:green;">-</div>
                      <?php
                        }
                      ?>
                      
                      
                      </td>
                    </tr>
                  <?php } ?>
                </tbody>
              </table>
            </div>
          </div>
        </div>

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

  <!-- 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 Perencanaan <div style="color:blue;"><?php echo namadivisi($xdivisi)." ".namabulan($xbln)." ".$xthn ?></div></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">
          <input type="hidden" class="form-control" name="id_anggaran" value="<?php echo $xidanggaran; ?>">
          <label for="alamat">Jenis Transaksi * :</label>
            <select id="heard" class="form-control" required name="id_jenis">

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

                $query2  = "SELECT * FROM jenis_transaksi WHERE id_divisi=$xdivisi ORDER BY jenis ASC, nama_jenis ASC";
                $tampil2 = mysqli_query($conn, $query2);
                while ($w = mysqli_fetch_array($tampil2)) {
                ?>
                  <option value="<?php echo $w['id_jenis']; ?>" <?php if ($r['id_jenis'] == $w['id_jenis']) {
                                                            echo "selected";
                                                          } ?>><?php echo $w['nama_jenis']; ?> <?php if($w['jenis']=="0"){ echo "(Kas Masuk)";} elseif($w['jenis']=="2"){ echo "(Kas Keluar)";} ?></option>";
                <?php
                }
                ?>
              </select>

            <label for="telephone">Jumlah :</label>
            <input type="number" class="form-control" name="jml" style="font-weight: bold;color:black;font-size:large" autocomplete="off" required>
            <label for="telephone">Keterangan :</label>
            <textarea class="form-control" name="ket" autocomplete="off" required></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 info -->
  <div class="modal fade" id="modalinfo" 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">Info</h4>
        </div>
        <div class="modal-body">
          <h4 class="modal-title" id="myModalLabel">Tambahkan Anggaran Berdasarkan Jenis Transaksi</h4><br>
          <h4 class="modal-title" id="myModalLabel">Tabel Diatas Adalah rekap dari total anggaran per jenis 
            transaksi serta dibedakan pengeluaran dan pemasukan</h4><br>
          <h4 class="modal-title" id="myModalLabel">Tabel Dibawah Adalah Rincian Semua Penganggaran</h4><br>
          <h4 class="modal-title" id="myModalLabel">Apabila Penganggaran sudah selesai WAJIB klik 'Ajukan Validasi'
          </h4>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default btn-sm" data-dismiss="modal">Ok</button>
        </div>
  
      </div>
    </div>
  </div>


<?php  } elseif ($_GET['aksi'] == 'edit') {
$id = $_GET['id'];
$query = mysqli_query($conn, "SELECT * FROM perencanaan_detail_anggaran WHERE id='$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><br>
            <div class="clearfix"></div>
            <label style="color: red;">MENGUBAH DATA INI AKAN MENGUBAH DATA ANGGARAN YANG SUDAH DIBUAT</label>
          </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="telephone">Divisi :</label>
                  <input type="input" readonly class="form-control" name="jml" value="<?php echo namadivisi($_GET['id_divisi']); ?>" style="font-weight: bold;color:black;font-size:large" autocomplete="off" required>
                  <label for="telephone">Bulan-Tahun :</label>
                  <input type="input" readonly class="form-control" name="jml" value="<?php echo namabulan($_GET['bln'])." - ".$_GET['thn']; ?>" style="font-weight: bold;color:black;font-size:large" autocomplete="off" required>
                 
                  <input type="hidden" class="form-control" name="id" value="<?php echo $r['id']; ?>" />
                  <input type="hidden" class="form-control" name="id_anggaran" value="<?php echo $r['id_anggaran']; ?>" />
                  <label for="alamat">Jenis Transaksi * :</label>
                  <select id="heard" class="form-control" required name="id_jenis">

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

                      $query2  = "SELECT * FROM jenis_transaksi WHERE id_divisi='$_GET[id_divisi]' AND jenis!='0' ORDER BY nama_jenis";
                      $tampil2 = mysqli_query($conn, $query2);
                      while ($w = mysqli_fetch_array($tampil2)) {
                      ?>
                        <option value="<?php echo $w['id_jenis']; ?>" <?php if ($r['id_jenis'] == $w['id_jenis']) {
                                                                  echo "selected";
                                                                } ?>><?php echo $w['nama_jenis']; ?></option>";
                      <?php
                      }
                      ?>
                    </select>

                  <label for="telephone">Jumlah :</label>
                  <input type="number" class="form-control" name="jml" value="<?php echo $r['jml']; ?>" style="font-weight: bold;color:black;font-size:large" autocomplete="off" required>
                  <label for="telephone">Keterangan :</label>
                  <textarea class="form-control" name="ket" autocomplete="off" required><?php echo $r['ket']; ?></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') {
  $module = $_GET['module'];
  $password   = md5($_POST[password]);

    mysqli_query($conn, "UPDATE perencanaan_detail_anggaran SET id_jenis = '$_POST[id_jenis]',
                                    ket = '$_POST[ket]',
                                    jml = '$_POST[jml]' 
                                    WHERE id = '$_POST[id]'");
  
  //cek berhasil atau tidak
if (!$query) {
    echo mysqli_error($conn);
    echo "gagal";
} else {
    echo "<script>ySimpan('?module=" . $module . "&id_anggaran=" . $_POST[id_anggaran] . "');</script>";
}
} elseif ($_GET['aksi'] == 'simpan') {
  $module = $_GET['module'];
  $query=mysqli_query($conn, "INSERT INTO perencanaan_detail_anggaran(
                                  id_anggaran,
                                  ket,
                                  id_jenis,
                                  jml) VALUES(
                                                 '$_POST[id_anggaran]',
                                                 '$_POST[ket]',
                                                 '$_POST[id_jenis]',
                                                 '$_POST[jml]')");
      //cek berhasil atau tidak
      if (!$query) {
          echo mysqli_error($conn);
          echo "gagal";
      } else {
          echo "<script>ySimpan('?module=" . $module . "&id_anggaran=" . $_POST[id_anggaran] . "');</script>";
      }
} elseif ($_GET['aksi'] == 'hapus') {
  $module = $_GET['module'];
  $id = $_GET['id'];
  $query = mysqli_query($conn, "Delete FROM perencanaan_detail_anggaran WHERE id='$id'");
  if (!$query) {
    echo mysqli_error($conn);
    echo "gagal";
      } else {
          echo "<script>ySimpan('?module=" . $module . "&id_anggaran=" . $_GET[id_anggaran] . "');</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 mengubah 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">Ok</a>
        <button type="button" class="btn btn-success btn-sm" data-dismiss="modal">Batal</button>
      </div>
    </div>
  </div>
</div>

