<?php
include '../../config/conn.php';
if ($_GET['aksi'] == '') {
  
  if ($_GET['periode'] == 'y') {
    $bln = $_POST['bln'];
    $thn = $_POST['thn'];
    $periode = "WHERE bln='$bln' AND thn='$thn'";
    $periode_perdivisi = "AND bln='$bln' AND thn='$thn'";

  }else{
    $thn = date('Y');
  }

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

      <div class="row">
        <div class="col-md-10 col-sm-12 col-xs-12">
          <div class="x_panel">
            <div class="x_title" style="text-transform: capitalize;">
              <h2 style="color:black; font-size:20px;">Data Perencanaan Anggaran Bulanan<small></small></h2>
              <h2 style="color:blue; font-size:20px;">Divisi <?php echo namadivisi($_SESSION['id_divisi']); ?><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>
            <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">
            <form action="?module=<?php echo $_GET['module']; ?>&periode=y" enctype="multipart/form-data" method="POST">


                <div class="well" style="overflow: auto">

                  <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" style="background:white;"><small>Bulan</small></i></div>
                      <select id="heard" class="form-control" required name="bln">
                        <option value="">- Semua Bulan -</option>
                            <?php
                            $keri  = mysqli_query($conn, "SELECT * FROM bulan  order by id_bulan asc");
                            while ($data = mysqli_fetch_array($keri)) { ?>
                            <option value="<?php echo $data['id_bulan']; ?>" <?php if ($data['id_bulan'] == $bln) {
                                                                            echo "selected";
                                                                            } ?>><?php echo $data['nama_bulan']; ?></option>

                            <?php }  ?>
                        </select>
                      <div class="input-group-addon" style="background:white;"><small>Tahun</small></i></div>
                      <select id="heard" class="form-control" required name="thn">
                        <option value="">- Semua Tahun -</option>
                      <?php
                            $tahun_ini = date('Y');
                            $xthn = $tahun_ini - 2;
                            $ythn = $xthn + 5;
                            for ($tahun = $xthn; $tahun <= $ythn; $tahun++) { ?>
                            <option value="<?php echo $tahun; ?>" <?php if ($tahun == $thn) {
                                                                            echo "selected";
                                                                            } ?>><?php echo $tahun ; ?></option>

                            <?php }  ?>
                            </select>
                    </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>
            </div>
            <div class="x_content">

              <table style="color:black; font-size:20px;" 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>Penerimaan</th>
                    <th>Pengeluaran</th>
                    <th width="50">Validasi</th>
                    <th>Atur</th>
                    <th width="50">Aksi</th>
                  </tr>
                </thead>
                <tbody>
                  <?php
                  $no = 0;
                  $total_in=0;
                  $total_out=0;
                  if ($_SESSION['leveluser']=="admin") {
                    $query = mysqli_query($conn, "SELECT * FROM perencanaan_anggaran_bulan $periode ORDER BY id_divisi desc, bln desc, thn desc");   
                  }else{
                    $query = mysqli_query($conn, "SELECT * FROM perencanaan_anggaran_bulan WHERE id_divisi='$_SESSION[id_divisi]' $periode_perdivisi ORDER BY bln desc, thn 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($row['id_divisi']); ?></td>
                      <td><?php echo namabulan($row['bln']); ?></td>
                      <td><?php echo $row['thn']; ?></td>
                      <td><?php                      
                        echo rupiah(total_jml_in_per_id_ang($row['id_anggaran']));
                        $total_in=total_jml_in_per_id_ang($row['id_anggaran'])+$total_in;
                      ?></td>
                      <td><?php                      
                        echo rupiah(total_jml_out_per_id_ang($row['id_anggaran']));
                        $total_out=total_jml_out_per_id_ang($row['id_anggaran'])+$total_in;
                      ?></td>
                      <td> <?php

                  

                     if ($_SESSION['leveluser'] == "admin" ) {
                        if ($row['valid']=="1") {
                          echo "<a class=\"btn  btn-success btn-xs\"  onclick=\"confirm_modal('?module=$_GET[module]&aksi=ubahstatus&valid=3&id_anggaran=$row[id_anggaran]');\">Sudah Divalidasi</a>";
                        } elseif ($row['valid']=="2") {
                          echo "Diajukan";
                          echo "<a class=\"btn  btn-warning btn-xs\"  onclick=\"confirm_modal('?module=$_GET[module]&aksi=ubahstatus&valid=1&id_anggaran=$row[id_anggaran]');\">Terima </a>";
                          echo "<a class=\"btn  btn-danger btn-xs\"  onclick=\"confirm_modal('?module=$_GET[module]&aksi=ubahstatus&valid=3&id_anggaran=$row[id_anggaran]');\">Tolak </a>";
                        }else{
                          echo "Blm Diajukan";
                          echo "<a class=\"btn  btn-warning btn-xs\"  onclick=\"confirm_modal('?module=$_GET[module]&aksi=ubahstatus&valid=1&id_anggaran=$row[id_anggaran]');\">Terima </a>";
                        }
                      }else{
                        if ($row['valid']=="1") {
                          echo "<div style='color:green;font-size:14px'>Sudah Validasi</div>";
                        } elseif ($row['valid']=="2"){
                          echo "<a class=\"btn  btn-warning btn-xs\">Ditinjau</a>";
                        }elseif ($row['valid']=="3"){
                          echo "<div style='color:red;'>Ditolak</div>";
                          echo "<a class=\"btn  btn-primary btn-xs\" onclick=\"confirm_modal('?module=$_GET[module]&aksi=ubahstatus&valid=2&id_anggaran=$row[id_anggaran]');\">Ajukan Ulang</a>";
                        } else {
                          echo "<a class=\"btn  btn-primary btn-xs\" onclick=\"confirm_modal('?module=$_GET[module]&aksi=ubahstatus&valid=2&id_anggaran=$row[id_anggaran]');\">Ajukan Validasi</a>";
                        }
                      }

                        ?></td>
                      <td><a class='open_modal btn  btn-success btn-xs' href="?module=detail_perencanaan&id_anggaran=<?php echo $row['id_anggaran']; ?>"><i class="glyphicon glyphicon-edit"></i> Atur Anggaran</a> </td>
                      <td>
                      <?php
                        if ($row['valid']=="1" OR $row['valid']=="2") { } else { ?>
                          <a class='open_modal btn  btn-default btn-xs' href="?module=<?php echo $_GET['module']; ?>&aksi=edit&id_anggaran=<?php echo $row['id_anggaran']; ?>"><i class="glyphicon glyphicon-edit"></i> Edit</a> 
                          <?php 
                          }
                     
                        ?>
                      </td>
                    </tr>
                  <?php } ?>
                </tbody>
                <thead>
                  <tr>
                    <th colspan=5 style="text-align:center;">Total</th>
                    <th><?php echo rupiah($total_in); ?></th>
                    <th><?php echo rupiah($total_out); ?></th>
                    <th colspan=3>-</th>
                  </tr>
                </thead>

              </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 Perencanaan Anggaran</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="alamat">Divisi * :</label>
            <select id="heard" class="form-control" required name="id_divisi">

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

                $query2  = "SELECT * FROM jenis_divisi ORDER BY id_divisi";
                $tampil2 = mysqli_query($conn, $query2);
                while ($w = mysqli_fetch_array($tampil2)) {
                ?>
                  <option value="<?php echo $w['id_divisi']; ?>" <?php if ($_SESSION['id_divisi'] == $w['id_divisi']) {
                                                            echo "selected";
                                                          } ?>><?php echo $w['nama_divisi']; ?></option>";
                <?php
                }
                ?>
              </select>

            <label for="alamat"> Pilih Bulan * :</label>
              <select id="heard" class="form-control" required name="bln">
              <option value="">- Pilih Bulan -</option>
                <?php
                $keri  = mysqli_query($conn, "SELECT * FROM bulan  order by id_bulan asc");
                while ($data = mysqli_fetch_array($keri)) { ?>
                  <option value="<?php echo $data['id_bulan']; ?>" <?php if ($data['id_bulan'] == $r['bln']) {
                                                                  echo "selected";
                                                                } ?>><?php echo $data['nama_bulan']; ?></option>

                <?php }  ?>
              </select>

            <label for="telephone">Tahun:</label>
            <select id="heard" class="form-control" required name="thn">
                        <option value="">- Pilih Tahun -</option>
                      <?php
                            $tahun_ini = date('Y');
                            $xthn = $tahun_ini - 2;
                            $ythn = $xthn + 5;
                            for ($tahun = $xthn; $tahun <= $ythn; $tahun++) { ?>
                            <option value="<?php echo $tahun; ?>" <?php if ($tahun == $thn) {
                                                                            echo "selected";
                                                                            } ?>><?php echo $tahun ; ?></option>

                            <?php }  ?>
              </select>
          
        </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="2" 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 Bulan Dan Tahun</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_anggaran'];
  $query = mysqli_query($conn, "SELECT * FROM perencanaan_anggaran_bulan WHERE id_anggaran='$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">
                    
                    <input type="hidden" class="form-control" name="id_anggaran" value="<?php echo $r['id_anggaran']; ?>" />
                        <label for="alamat">Divisi * :</label>
                          <select id="heard" class="form-control" required name="id_divisi">

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

                                  if ($_SESSION['leveluser'] == "admin" ) {
                                    $hanya_div_tertentu = "";
                                  }else{
                                    $hanya_div_tertentu = "WHERE id_divisi = '$r[id_divisi]'";
                                  }

                              $query2  = "SELECT * FROM jenis_divisi $hanya_div_tertentu ORDER BY id_divisi";
                              $tampil2 = mysqli_query($conn, $query2);
                              while ($w = mysqli_fetch_array($tampil2)) {
                              ?>
                                <option value="<?php echo $w['id_divisi']; ?>" <?php if ($r['id_divisi'] == $w['id_divisi']) {
                                                                          echo "selected";
                                                                        } ?>><?php echo $w['nama_divisi']; ?></option>";
                              <?php
                              }
                              ?>
                            </select>

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

                              <?php }  ?>
                            </select>

                          <label for="telephone">Tahun:</label>
                          <input type="number" class="form-control" max="2500" value="<?php echo $r['thn']; ?>" name="thn" autocomplete="off" />
                        
                  </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'];
    $query=mysqli_query($conn, "UPDATE perencanaan_anggaran_bulan SET id_divisi = '$_POST[id_divisi]',
                                    bln = '$_POST[bln]',
                                    thn = '$_POST[thn]' 
                                    WHERE id_anggaran = '$_POST[id_anggaran]'");
  
  if (!$query) {
    echo mysqli_error($conn);
    echo "gagal";
} else {
    echo "<script>ySimpan('?module=" . $module . "');</script>";
}
} elseif ($_GET['aksi'] == 'simpan') {
  $module = $_GET['module'];
  $query=mysqli_query($conn, "INSERT INTO perencanaan_anggaran_bulan(
                                  id_divisi,
                                  bln,
                                  thn) VALUES(
                                                 '$_POST[id_divisi]',
                                                 '$_POST[bln]',
                                                 '$_POST[thn]')");
      //cek berhasil atau tidak
      if (!$query) {
          echo mysqli_error($conn);
          echo "gagal";
      } else {
          echo "<script>ySimpan('?module=" . $module . "');</script>";
      }
} elseif ($_GET['aksi'] == 'hapus') {
  $module = $_GET['module'];
  $id = $_GET['id'];
  $query = mysqli_query($conn, "Delete FROM pekerja WHERE id_pekerja='$id'");
  echo "<script language='javascript'>document.location='?module=" . $module . "';</script>";
} elseif ($_GET['aksi'] == 'ubahstatus') {
  $module = $_GET['module'];
    $query=mysqli_query($conn, "UPDATE perencanaan_anggaran_bulan SET valid = '$_GET[valid]'
                                       WHERE id_anggaran = '$_GET[id_anggaran]'");
  
  if (!$query) {
    echo mysqli_error($conn);
    echo "gagal";
} else {
    echo "<script>ySimpan('?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 Melakukan Perubahan Pada 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>