<?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-12 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 Tagihan / Hutang<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-12 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-12 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</th>
                    <th>Divisi</th>
                    <th>Nama Hutang/Tagihan</th>
                    <th>Jenis_transaksi</th>
                    <th>Jatuh_tempo</th>
                    <th>Status</th>
                    <th>Keterangan</th>
                    <th>Jumlah</th>
                    <th>dibayar</th>
                    <th>Sisa</th>
                    <th>Validasi</th>
                    
                    <th>Detail</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 tagihan_hutang $periode ORDER BY id_tag_hut desc");   
                  }else{
                    $query = mysqli_query($conn, "SELECT * FROM tagihan_hutang WHERE id_divisi='$_SESSION[id_divisi]' ORDER BY id_tag_hut desc");  
                  }

                  while ($row = mysqli_fetch_array($query)) {
                    $no++;
                  ?>
                    <tr>
                      <td><?php echo $no; ?></td>
                      <td><?php echo $row['id_tag_hut']; ?></td>
                      <td><?php echo namadivisi($row['id_divisi']); ?></td>
                      <td><?php echo $row['nama_tag_hut']; ?></td>
                      <td><?php echo namajenis($row['id_jenis']); ?></td>
                      <td><?php echo $row['tgl_jatuh_tempo']; ?></td>
                      <td><?php 
                       if ($row['status']=="1") {
                        echo "<a class=\"btn  btn-primary btn-xs\" onclick=\"confirm_modal('?module=$_GET[module]&aksi=ubahstatus&valid=0&id_tag_hut=$row[id_tag_hut]');\">Selesai</a>";
                      } else {
                        echo "<a class=\"btn  btn-warning btn-xs\" onclick=\"confirm_modal('?module=$_GET[module]&aksi=ubahstatus&valid=1&id_tag_hut=$row[id_tag_hut]');\">Blm Lunas</a>";
                      }
                      
                      ?></td>
                      <td>
                          <?php 
                          $text = $row['ket_tag_hut'];
                          if(strlen($text) > 20) {
                              echo substr($text, 0, 20) . '...';
                          } else {
                              echo $text;
                          }
                          ?>
                      </td>
                      <td><?php echo rupiah($row['jml_tag_hut']); ?></td>
                      <td><?php echo rupiah(total_taghut_sdhdibayar($row['id_tag_hut'])); ?></td>
                      <td><?php echo rupiah($row['jml_tag_hut']-total_taghut_sdhdibayar($row['id_tag_hut'])); ?></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=ubahvalid&valid=3&id_tag_hut=$row[id_tag_hut]');\">Sudah Divalidasi</a>";
                        } elseif ($row['valid']=="2") {
                          echo "Diajukan";
                          echo "<a class=\"btn  btn-warning btn-xs\"  onclick=\"confirm_modal('?module=$_GET[module]&aksi=ubahvalid&valid=1&id_tag_hut=$row[id_tag_hut]');\">Terima </a>";
                          echo "<a class=\"btn  btn-danger btn-xs\"  onclick=\"confirm_modal('?module=$_GET[module]&aksi=ubahvalid&valid=3&id_tag_hut=$row[id_tag_hut]');\">Tolak </a>";
                        }else{
                          echo "Blm Diajukan";
                          echo "<a class=\"btn  btn-warning btn-xs\"  onclick=\"confirm_modal('?module=$_GET[module]&aksi=ubahvalid&valid=1&id_tag_hut=$row[id_tag_hut]');\">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=ubahvalid&valid=2&id_tag_hut=$row[id_tag_hut]');\">Ajukan Ulang</a>";
                        } else {
                          echo "<a class=\"btn  btn-primary btn-xs\" onclick=\"confirm_modal('?module=$_GET[module]&aksi=ubahvalid&valid=2&id_tag_hut=$row[id_tag_hut]');\">Ajukan Validasi</a>";
                        }
                      }

                        ?></td>
                      <td><a class='open_modal btn  btn-success btn-xs' href="?module=cek-detail-tag-hut&id_tag_hut=<?php echo $row['id_tag_hut']; ?>"><i class="glyphicon glyphicon-edit"></i> Lihat</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_tag_hut=<?php echo $row['id_tag_hut']; ?>"><i class="glyphicon glyphicon-edit"></i> Edit</a> 
                          <?php 
                          }
                     
                        ?>
                      </td>
                    </tr>
                  <?php } ?>
                </tbody>

              </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 Hutang/Tagihan</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="telephone">Nama Tagihan/Hutang :</label>
            <input type="text" maxlength="50" class="form-control" name="nama_tag_hut" style="font-weight: bold;color:black;font-size:large" autocomplete="off" required>
           
            <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='$_SESSION[id_divisi]' 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_tag_hut" style="font-weight: bold;color:black;font-size:large" autocomplete="off" required>
           
            <label f>Tanggal Jatuh Tempo* :</label>
          <div class="input-group">
            <div class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></div>
            <input type="text" class="form-control" name="tgl_jatuh_tempo" required value="" id="tanggal4" autocomplete="off">
          </div>

          <label for="telephone">Keterangan :</label>
            <textarea class="form-control" name="ket_tag_hut" 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="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">DATA TAGIHAN/HUTANG</h4><br>
          <h4 class="modal-title" id="myModalLabel">Apabila Penganggaran sudah selesai WAJIB klik 'Ajukan Validasi'
          </h4>
          <h4 class="modal-title" id="myModalLabel">DATA AKAN DIINPUT MELALUI TRANSAKSI HARIAN DAN LAPORAN TRANSAKSI HARIAN
          </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_tag_hut'];
  $query = mysqli_query($conn, "SELECT * FROM tagihan_hutang WHERE id_tag_hut='$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><br>
              <label style="color: red;">UNTUK MENGHAPUS DATA HUBUNGI ADMIN PUSAT</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_tag_hut" value="<?php echo $r['id_tag_hut']; ?>" />
                        <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="telephone">Nama Tagihan/Hutang :</label>
            <input type="text" maxlength="50" class="form-control" name="nama_tag_hut" value="<?php echo $r['nama_tag_hut']; ?>" style="font-weight: bold;color:black;font-size:large" autocomplete="off" required>
           
            <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='$_SESSION[id_divisi]' AND jenis!='0' 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_tag_hut" value="<?php echo $r['jml_tag_hut']; ?>" style="font-weight: bold;color:black;font-size:large" autocomplete="off" required>
           
            <label f>Tanggal Jatuh Tempo* :</label>
          <div class="input-group">
            <div class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></div>
            <input type="text" class="form-control" name="tgl_jatuh_tempo" required value="<?php echo date('d-m-Y', strtotime($r['tgl_jatuh_tempo'])); ?>" id="tanggal4" autocomplete="off">
          </div>

          <label for="telephone">Keterangan :</label>
            <textarea class="form-control" name="ket_tag_hut" autocomplete="off" required><?php echo $r['ket_tag_hut']; ?></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>

                  <?php
                  if ($_SESSION['leveluser'] == "admin" ) {
                    echo "<button type='submit' class='btn btn-success btn-sm'>Simpan</button><br>";

                  }else{
                    if ($row['valid']=="1") {
                      echo "<div style='color:green;font-size:14px'>Hanya Lihat - Sudah Validasi</div>";
                    }  else {
                      echo "<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'];
  $tgl1 = date('Y-m-d', strtotime($_POST['tgl_jatuh_tempo']));
    $query=mysqli_query($conn, "UPDATE tagihan_hutang SET id_divisi = '$_POST[id_divisi]',
                                    nama_tag_hut = '$_POST[nama_tag_hut]',
                                    id_jenis = '$_POST[id_jenis]',
                                    jml_tag_hut = '$_POST[jml_tag_hut]',
                                    tgl_jatuh_tempo = '$tgl1',
                                    ket_tag_hut = '$_POST[ket_tag_hut]'
                                    WHERE id_tag_hut = '$_POST[id_tag_hut]'");
  
  if (!$query) {
    echo mysqli_error($conn);
    echo "gagal";
} else {
    echo "<script>ySimpan('?module=" . $module . "');</script>";
}
} elseif ($_GET['aksi'] == 'simpan') {
  $module = $_GET['module'];
  $tgl = date('Y-m-d', strtotime($_POST['tgl_jatuh_tempo']));
  $query=mysqli_query($conn, "INSERT INTO tagihan_hutang(
                                  id_divisi,nama_tag_hut, id_jenis, jml_tag_hut, tgl_jatuh_tempo, 
                                  ket_tag_hut) VALUES(
                                                 '$_POST[id_divisi]',
                                                 '$_POST[nama_tag_hut]',
                                                 '$_POST[id_jenis]',
                                                 '$_POST[jml_tag_hut]',
                                                 '$tgl',
                                                 '$_POST[ket_tag_hut]'
                                                 )");
      //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 tagihan_hutang SET status = '$_GET[valid]'
                                       WHERE id_tag_hut = '$_GET[id_tag_hut]'");
  
  if (!$query) {
    echo mysqli_error($conn);
    echo "gagal";
} else {
    echo "<script>ySimpan('?module=" . $module . "');</script>";
}
} elseif ($_GET['aksi'] == 'ubahvalid') {
  $module = $_GET['module'];
    $query=mysqli_query($conn, "UPDATE tagihan_hutang SET valid = '$_GET[valid]'
                                       WHERE id_tag_hut = '$_GET[id_tag_hut]'");
  
  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>