<?php  
// fungsi untuk pengecekan tampilan form
// jika form add data yang dipilih
if ($_GET['form']=='add') { ?> 
  <!-- tampilan form add data -->
	<!-- Content Header (Page header) -->
  <section class="content-header">
    <h1>
      <i class="fa fa-edit icon-title"></i> Tambah Biaya
    </h1>

  </section>

  <!-- Main content -->
  <section class="content">
    <div class="row">
      <div class="col-md-12">
        <div class="box box-primary">
          <!-- form start -->
          <form role="form" class="form-horizontal" action="modules/biaya/proses.php?act=insert" method="POST" name="formBiaya"  enctype="multipart/form-data">
            <div class="box-body">
			
			  <div class="form-group">
                <label class="col-sm-2 control-label">Tanggal</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control date-picker" data-date-format="dd-mm-yyyy" name="tanggal" autocomplete="off" value="<?php echo date("d-m-Y"); ?>" required>
                </div>
              </div>
			  
			  
			   <div class="form-group">
                <label class="col-sm-2 control-label">Kategori</label>
                <div class="col-sm-5">
                  <select class="chosen-select" name="kategori" data-placeholder="-- Pilih --" autocomplete="off" required>
					<option value=''></option>
					<?php
						$user = $_SESSION['id_user'];
						$query2 = mysqli_query($mysqli, "SELECT * FROM categorybiaya where category_status='Y'  ORDER BY category_name asc")
                                            or die('Ada kesalahan pada query tampil: '.mysqli_error($mysqli));

						// tampilkan data
						while ($data2 = mysqli_fetch_assoc($query2)) { 
							echo"<option value='$data2[category_id]'>$data2[category_name]</option>";
						}
						
						?>
                  </select>
                </div>
              </div>
			  
			  
			  
    

              <div class="form-group">
                <label class="col-sm-2 control-label">Keterangan</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" name="name" autocomplete="off" required>
                </div>
              </div>
			  
			  
			  <div class="form-group">
                <label class="col-sm-2 control-label">Jenis</label>
                <div class="col-sm-5">
                  <select class="chosen-select" name="jenis" data-placeholder="-- Pilih --" autocomplete="off" required>
					<option value=''></option>
					<?php
							echo"<option value='kredit'>Kredit</option>";
							echo"<option value='debet'>Debet</option>";
						
						?>
                  </select>
                </div>
              </div>
			  
			  <div class="form-group">
                <label class="col-sm-2 control-label">Qty</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="harga_beli" name="qty" autocomplete="off" onkeyup="hitung_total_stok(this)" onKeyPress="return goodchars(event,'0123456789',this)" >
                </div>
              </div>
			  
			  
			  <div class="form-group">
                <label class="col-sm-2 control-label">Harga</label>
                <div class="col-sm-5">
                  <div class="input-group">
                    <span class="input-group-addon">Rp.</span>
                    <input type="text" class="form-control" id="harga_jual" name="total"  onkeyup="hitung_total_stok(this)" autocomplete="off" onKeyPress="return goodchars(event,'0123456789',this)" >
                  </div>
                </div>
              </div>
			  
			  
			  <div class="form-group">
                <label class="col-sm-2 control-label">Total</label>
                <div class="col-sm-5">
                  <div class="input-group">
                    <span class="input-group-addon">Rp.</span>
                    <input type="text" class="form-control" id="total_stok" name="total_stok" autocomplete="off" onKeyPress="return goodchars(event,'0123456789',this)"  readonly>
                  </div>
                </div>
              </div>
			  
			  <div class="form-group">
                <label class="col-sm-2 control-label">Image</label>
                <div class="col-sm-5">
                  <input type="file" name="foto">
                </div>
              </div>

			  

            </div><!-- /.box body -->

            <div class="box-footer">
              <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                  <input type="submit" class="btn btn-primary btn-submit" name="simpan" value="Simpan">
                  <a href="?module=biaya" class="btn btn-default btn-reset">Batal</a>
                </div>
              </div>
            </div><!-- /.box footer -->
          </form>
        </div><!-- /.box -->
      </div><!--/.col -->
    </div>   <!-- /.row -->
  </section><!-- /.content -->
<?php
}
// jika form edit data yang dipilih
// isset : cek data ada / tidak
elseif ($_GET['form']=='edit') { 
  if (isset($_GET['id'])) {
      // fungsi query untuk menampilkan data dari tabel obat
      $query = mysqli_query($mysqli, "SELECT *,(select category_name from categorybiaya where category_id = biaya.kategori) as category_name  FROM biaya  WHERE id='$_GET[id]'") 
                                      or die('Ada kesalahan pada query tampil : '.mysqli_error($mysqli));
      $data  = mysqli_fetch_assoc($query);
	  $images = $data['images'];
    }
?>
  <!-- tampilan form edit data -->
  <!-- Content Header (Page header) -->
  <section class="content-header">
    <h1>
      <i class="fa fa-edit icon-title"></i> Edit Biaya
    </h1>

  </section>

  <!-- Main content -->
  <section class="content">
    <div class="row">
      <div class="col-md-12">
        <div class="box box-primary">
          <!-- form start -->
          <form role="form" class="form-horizontal" action="modules/biaya/proses.php?act=update" method="POST" name="formBiaya"   enctype="multipart/form-data">
            <div class="box-body">
              <input type="text" class="form-control" style="display:none;" name="id" value="<?php echo $data['id']; ?>" required>
              <div class="form-group">
                <label class="col-sm-2 control-label">Tanggal</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control date-picker" data-date-format="dd-mm-yyyy" name="tanggal" autocomplete="off" value="<?php echo date("d-m-Y",strtotime($data['billdate'])); ?>" required>
                </div>
              </div>
			  
			  
			   <div class="form-group">
                <label class="col-sm-2 control-label">Kategori</label>
                <div class="col-sm-5">
                  <select class="chosen-select" name="kategori" data-placeholder="-- Pilih --" autocomplete="off" required>
					<option value="<?php echo $data['kategori']; ?>"><?php echo $data['category_name']; ?></option>
					<?php
						$user = $_SESSION['id_user'];
						$query2 = mysqli_query($mysqli, "SELECT * FROM categorybiaya where category_status='Y'  ORDER BY category_name asc")
                                            or die('Ada kesalahan pada query tampil: '.mysqli_error($mysqli));

						// tampilkan data
						while ($data2 = mysqli_fetch_assoc($query2)) { 
							echo"<option value='$data2[category_id]'>$data2[category_name]</option>";
						}
						
						?>
                  </select>
                </div>
              </div>
			  
			  
			  
    

              <div class="form-group">
                <label class="col-sm-2 control-label">Keterangan</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" name="name" value="<?php echo $data['ket']; ?>" autocomplete="off" required>
                </div>
              </div>
			  
			  
			  <div class="form-group">
                <label class="col-sm-2 control-label">Jenis</label>
                <div class="col-sm-5">
                  <select class="chosen-select" name="jenis" data-placeholder="-- Pilih --" autocomplete="off" required>
					<?php
							echo"<option value='$data[jenis]'>$data[jenis]</option>";
							echo"<option value='kredit'>Kredit</option>";
							echo"<option value='debet'>Debet</option>";
						
						?>
                  </select>
                </div>
              </div>
			  
			  <div class="form-group">
                <label class="col-sm-2 control-label">Qty</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="harga_beli" name="qty" autocomplete="off" value="<?php echo $data['qty']; ?>" onkeyup="hitung_total_stok(this)" onKeyPress="return goodchars(event,'0123456789',this)" >
                </div>
              </div>
			  
			  
			  <div class="form-group">
                <label class="col-sm-2 control-label">Harga</label>
                <div class="col-sm-5">
                  <div class="input-group">
                    <span class="input-group-addon">Rp.</span>
                    <input type="text" class="form-control" id="harga_jual" name="total"  value="<?php echo $data['harga']; ?>" onkeyup="hitung_total_stok(this)" autocomplete="off" onKeyPress="return goodchars(event,'0123456789',this)" >
                  </div>
                </div>
              </div>
			  
			  
			  <div class="form-group">
                <label class="col-sm-2 control-label">Total</label>
                <div class="col-sm-5">
                  <div class="input-group">
                    <span class="input-group-addon">Rp.</span>
                    <input type="text" class="form-control" id="total_stok" name="total_stok" value="<?php $tot = $data['harga'] * $data['qty']; echo "$tot"; ?>" autocomplete="off" onKeyPress="return goodchars(event,'0123456789',this)"  readonly>
                  </div>
                </div>
              </div>
			  
			  <div class="form-group">
                <label class="col-sm-2 control-label">Image</label>
                <div class="col-sm-5">
                  <input type="file" name="foto">
                  <br/>
                <?php  
                if ($data['images']=="") { ?>
                  <img style="border:1px solid #eaeaea;border-radius:5px;" src="images/user/noimg.jpg" width="128">
                <?php
                }
                else { 
					if(substr($data['images'],-3) == "pdf"){?>
						<a href="images/user/<?php echo $data['images']; ?>"><img style="border:1px solid #eaeaea;border-radius:5px;"  src="images/user/pdf.jpg" width="128"></a>
					<?php
					}else{
					?>
						<img style="border:1px solid #eaeaea;border-radius:5px;" data-toggle='modal' data-target='#myModal' src="images/user/<?php echo $data['images']; ?>" width="128">
					
                <?php
					}
                }
                ?>
                </div>
              </div>





            </div><!-- /.box body -->

            <div class="box-footer">
              <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                  <input type="submit" class="btn btn-primary btn-submit" name="simpan" value="Simpan">
                  <a href="?module=biaya" class="btn btn-default btn-reset">Batal</a>
                </div>
              </div>
            </div><!-- /.box footer -->
          </form>
		  
		  
		  
		   <?php
			echo"							
				<div id='myModal' class='modal fade' tabindex='-1' role='dialog'>
				  <div class='modal-dialog'>
					<div class='modal-content'>
						<div class='modal-body'>
							<img src='images/user/$images' class='img-responsive'>
						</div>
					</div>
				  </div>
				</div>";
				
		?>
        </div><!-- /.box -->
      </div><!--/.col -->
    </div>   <!-- /.row -->
  </section><!-- /.content -->
<?php
}
?>


<script type="text/javascript">

  function hitung_total_stok() {
    bil1 = document.formBiaya.qty.value;
    bil2 = document.formBiaya.total.value;
	
	bil1 = bil1.replace(".","");
	bil2 = bil2.replace(".","");

    if (bil2 == "") {
      var hasil = "";
    }
    else {
      var hasil = eval(bil1.replace(".","")) * eval(bil2.replace(".",""));
    }

    document.formBiaya.total_stok.value = (hasil.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
  }
</script>
