<script type="text/javascript">
  function tampil_obat(input){
    var num = input.value;

    $.post("modules/trade/medicines_transaction/medicines.php", {
      dataidobat: num,
    }, function(response) {      
      $('#stok').html(response)

      document.getElementById('jumlah_masuk').focus();
    });
  }

  function cek_jumlah_masuk(input) {
    jml = document.formObatMasuk.jumlah_masuk.value;
    var jumlah = eval(jml);
    if(jumlah < 1){
      alert('Tu stok esta en 0 no puedes generar salidas !!');
      input.value = input.value.substring(0,input.value.length-1);
    }
  }

  function hitung_total_stok() {
    bil1 = document.formObatMasuk.stok.value;
    bil2 = document.formObatMasuk.jumlah_masuk.value;
	tt = document.formObatMasuk.transaccion.value;
	
    if (bil2 == "") {
      var hasil = "";
    }
    else {
      var salida = eval(bil1) - eval(bil2);
	  var hasil = eval(bil1) + eval(bil2);
    }

	if (tt=="Salida"){
		
		document.formObatMasuk.total_stok.value = (salida);
		
	}	else {
		document.formObatMasuk.total_stok.value = (hasil);
	} 
    
  }
</script>

<?php  

if ($_GET['form']=='add') { ?> 

  <section class="content-header">
    <h1>
      <i class="fa fa-edit icon-title"></i> Entrada    </h1>
    <ol class="breadcrumb">
      <li><a href="?module=start"><i class="fa fa-home"></i> Inicio </a></li>
      <li><a href="?module=medicines_transaction"> Entrada </a></li>
      <li class="active"> Agregar </li>
    </ol>
  </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/trade/medicines_transaction/proses.php?act=insert" method="POST" name="formObatMasuk">
            <div class="box-body">
              <?php  
            
              $query_id = mysqli_query($mysqli, "SELECT RIGHT(codigo_transaccion,6) as codigo FROM transaccion_medicamentos
                                                ORDER BY codigo_transaccion DESC LIMIT 1")
                                                or die('Error : '.mysqli_error($mysqli));

              $count = mysqli_num_rows($query_id);

              if ($count <> 0) {
                 
                  $data_id = mysqli_fetch_assoc($query_id);
                  $codigo    = $data_id['codigo']+1;
              } else {
                  $codigo = 1;
              }

             
              $tahun          = date("Y");
              $buat_id        = str_pad($codigo, 6, "0", STR_PAD_LEFT);
              $codigo_transaccion = "TR-$tahun-$buat_id";
              ?>

              <div class="form-group">
                <label class="col-sm-2 control-label">Codigo de Transacción </label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" name="codigo_transaccion" value="<?php echo $codigo_transaccion; ?>" readonly required>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label">Fecha</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control date-picker" data-date-format="dd-mm-yyyy" name="fecha_a" autocomplete="off" value="<?php echo date("d-m-Y"); ?>" readonly required>
                </div>
              </div>

              <hr>

              <div class="form-group">  
                <label class="col-sm-2 control-label">Producto</label>
                <div class="col-sm-5">
                  <select class="chosen-select" name="codigo" data-placeholder="-- Seleccionar Productos --" onchange="tampil_obat(this)" autocomplete="off" required>
                    <option value=""></option>
                    <?php
                      //$query_obat = mysqli_query($mysqli, "SELECT codigo, nombre FROM medicamentos where stock >= 1 ORDER BY nombre ASC")
                        $query_obat = mysqli_query($mysqli, "SELECT a.nombre,b.codigo FROM medicamentos as a, transaccion_medicamentos as b where b.observaciones = 'POR INVENTARIO INICIAL' and a.codigo = b.codigo ORDER BY a.nombre ASC")
                
                                                            or die('error '.mysqli_error($mysqli));
                      while ($data_obat = mysqli_fetch_assoc($query_obat)) {
                        echo"<option value=\"$data_obat[codigo]\"> $data_obat[codigo] | $data_obat[nombre] </option>";
                      }
                    ?>
                  </select>
                </div>
              </div>
			  
			 
			  
			    <span id='stok'>
              <div class="form-group">
                <label class="col-sm-2 control-label">Stock</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" id="stok" name="stock" readonly required>
				  
				  
				  
				  
                </div>
              </div>
              </span>
			  
			    <div class="form-group">
                <label class="col-sm-2 control-label">Transacción</label>
                <div class="col-sm-5">
				  <input type="text" class="form-control" name="transaccion" value="Entrada" readonly required>
                  <!--<select name="transaccion" id="transaccion" required class='form-control' onchange="hitung_total_stok();"readonly>
					<option value="Entrada">Entrada</option>
					
				  </select>-->
                </div>
              </div>

			   <div class="form-group">
                <label class="col-sm-2 control-label">Cantidad</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" id="jumlah_masuk" name="num" autocomplete="off" onKeyPress="return goodchars(event,'0123456789',this)" onkeyup="hitung_total_stok(this)&cek_jumlah_masuk(this)" required>
                </div>
              </div>
			  
			
			  
			  
			  
			  
			  
			  

              <div class="form-group">
                <label class="col-sm-2 control-label">Total Stock</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" id="total_stok" name="total_stock" readonly required>
                </div>
              </div>
			  
			  
			  
			   <div class="form-group">  
                <label class="col-sm-2 control-label">Motivo</label>
                <div class="col-sm-5">
                  <select class="chosen-select" name="observaciones" data-placeholder="-- Seleccionar motivo --" onchange="tampil_obat(this)" autocomplete="off" required>
                    <option value=""></option>
                    <?php
                      $query_motivo = mysqli_query($mysqli, "SELECT nombre,tipo FROM motivo where tipo = 'ENTRADA' AND id not in (1) ORDER BY nombre ASC")
                                                            or die('error '.mysqli_error($mysqli));
                      while ($data_motivo = mysqli_fetch_assoc($query_motivo)) {
                         echo"<option value=\"$data_motivo[nombre]\"> $data_motivo[tipo] | $data_motivo[nombre] </option>";
                      }
                    ?>
                  </select>
                </div>
              </div>
			  
			  
			
			  
			    <!--   <div class="form-group">
                <label class="col-sm-2 control-label">Motivo</label>
                <div class="col-sm-5">
				
				<select name="observaciones" id="observaciones" required class='form-control' onchange="hitung_total_stok();">
				    <option value="SELECCIONE UNA OPCION">Seleccione</option>
					<option value="POR ABASTESER CODIGO">POR ABASTESER CODIGO</option>
					<option value="POR DEVOLUCION">POR DEVOLUCION</option>
					<option value="POR AJUSTE DE INVENTARIO">POR AJUSTE DE INVENTARIO</option>
					<option value="POR INVENTARIO INICIAL">POR INVENTARIO INICIAL</option>
					
				  </select>
                
                </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="Guardar" value="Guardar">
                  <a href="?module=medicines_transaction" class="btn btn-default btn-reset">Cancelar</a>
                </div>
              </div>
            </div><!-- /.box footer -->
          </form>
        </div><!-- /.box -->
      </div><!--/.col -->
    </div>   <!-- /.row -->
  </section><!-- /.content -->
<?php
}
?>