<?php  
$queryclose = mysqli_query($mysqli, "SELECT apertura FROM apertura_cierre where id = 1") 
or die('error: '.mysqli_error($mysqli));
$rows  = mysqli_num_rows($queryclose);
$data  = mysqli_fetch_assoc($queryclose);
$bandera = $data['apertura'];
/*print_r($bandera);
die();*/

if($bandera == 'CERRADO') {
	echo '<script>
                alert("EL ALMACEN SE ENCUENTRA CERRADO POR INVENTARIO DEL SUPERVISOR");
                window.location = "https://almacencdmxve.casatequilaarandas.com/main.php?module=closed";
          </script>';     
    return;
}  
if ($_GET['form']=='add') { ?> 

  <section class="content-header">
    <h1>
      <i class="fa fa-edit icon-title"></i> Agregar productos
    </h1>
    <ol class="breadcrumb">
      <li><a href="?module=start"><i class="fa fa-home"></i> Inicio </a></li>
      <li><a href="?module=medicines"> productos </a></li>
      <li class="active"> Más </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/proses.php?act=insert" method="POST">
            <div class="box-body">
              <?php  
          
               $query_id = mysqli_query($mysqli, "SELECT RIGHT(codigo,3) as codigo FROM medicamentos
                                                ORDER BY codigo 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;
              }


              $buat_id   = str_pad($codigo, 3, "0", STR_PAD_LEFT);
              $codigo = "AB$buat_id";
              ?>

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

              <div class="form-group">
                <label class="col-sm-2 control-label">Nombre</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" name="nombre" autocomplete="off" required>
                </div>
              </div>

			  
			      <div class="form-group">  
                <label class="col-sm-2 control-label">Unidad</label>
                <div class="col-sm-5">
                  <select class="chosen-select" name="unidad" data-placeholder="-- Seleccionar motivo --" onchange="tampil_obat(this)" autocomplete="off" required>
                    <option value=""></option>
                    <?php
                      $query_unidades = mysqli_query($mysqli, "SELECT nombre,tipo FROM unidades ORDER BY nombre ASC")
                                                            or die('error '.mysqli_error($mysqli));
                      while ($data_unidades = mysqli_fetch_assoc($query_unidades)) {
                         echo"<option value=\"$data_unidades[nombre]\"> $data_unidades[tipo] | $data_unidades[nombre] </option>";
                      }
                    ?>
                  </select>
                </div>
              </div>

 
<!--
              <div class="form-group">
                <label class="col-sm-2 control-label">Unidad</label>
                <div class="col-sm-5">
                  <select class="chosen-select" name="unidad" data-placeholder="-- Seleccionar --" autocomplete="off" required>
                    <option value=""></option>
                    <option value="Piezas">Piezas</option>
                    <option value="cajas">Cajas</option>
                    <option value="caja">botellas</option>
                    
                  </select>
                </div>
              </div>  -->
			  
			    <div class="form-group">
                <label class="col-sm-2 control-label">Precio de compra</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" name="precio_compra2" autocomplete="off" required>
                </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" class="btn btn-default btn-reset">Cancelar</a>
                </div>
              </div>
            </div><!-- /.box footer -->
          </form>
        </div><!-- /.box -->
      </div><!--/.col -->
    </div>   <!-- /.row -->
  </section><!-- /.content -->
<?php
}

elseif ($_GET['form']=='edit') { 
  if (isset($_GET['id'])) {

      $query = mysqli_query($mysqli, "SELECT codigo,nombre,precio_compra2,unidad FROM medicamentos WHERE codigo='$_GET[id]'") 
                                      or die('error: '.mysqli_error($mysqli));
      $data  = mysqli_fetch_assoc($query);
    }
?>

  <section class="content-header">
    <h1>
      <i class="fa fa-edit icon-title"></i> Modificar Producto
    </h1>
    <ol class="breadcrumb">
      <li><a href="?module=start"><i class="fa fa-home"></i> Inicio </a></li>
      <li><a href="?module=medicines"> Productos </a></li>
      <li class="active"> Modificar </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/proses.php?act=update" method="POST">
            <div class="box-body">
              
              <div class="form-group">
                <label class="col-sm-2 control-label">Codigo</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" name="codigo" value="<?php echo $data['codigo']; ?>" readonly required>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label">Nombre</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" name="nombre" autocomplete="off" value="<?php echo $data['nombre']; ?>" required>
                </div>
				</div>
   
             <div class="form-group">  
                <label class="col-sm-2 control-label">Unidad</label>
                <div class="col-sm-5">
                  <select class="chosen-select" name="unidad" data-placeholder="-- Seleccionar motivo --" onchange="tampil_obat(this)" autocomplete="off" required>
                   <option value="<?php echo $data['unidad']; ?>"><?php echo $data['unidad']; ?></option>
                    <?php
                      $query_unidades = mysqli_query($mysqli, "SELECT nombre,tipo FROM unidades ORDER BY nombre ASC")
                                                            or die('error '.mysqli_error($mysqli));
                      while ($data_unidades = mysqli_fetch_assoc($query_unidades)) {
                         echo"<option value=\"$data_unidades[nombre]\"> $data_unidades[tipo] | $data_unidades[nombre] </option>";
                      }
                    ?>
                  </select>
                </div>
              </div>



         <!--     <div class="form-group">
                <label class="col-sm-2 control-label">Unidad</label>
                <div class="col-sm-5">
                  <select class="chosen-select" name="unidad" data-placeholder="-- Seleccionar --" autocomplete="off" required>
                    <option value="<?php echo $data['unidad']; ?>"><?php echo $data['unidad']; ?></option>
                   <option value="Piezas">Piezas</option>
                    <option value="cajas">Cajas</option>
                    <option value="caja">Botellas</option>
                 
                  </select>
                </div>
              </div>  -->
			  
			    <div class="form-group">
                <label class="col-sm-2 control-label">Precio de compra</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" name="precio_compra2" 
				  value="<?php echo $data['precio_compra2']; ?>" required>
                </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" class="btn btn-default btn-reset">Cancelar</a>
                </div>
              </div>
            </div><!-- /.box footer -->
          </form>
        </div><!-- /.box -->
      </div><!--/.col -->
    </div>   <!-- /.row -->
  </section><!-- /.content -->
<?php
}


