<?php
session_start();


require_once "../../../config/database.php";

if(isset($_POST['dataidobat'])) {
	$codigo = $_POST['dataidobat'];

 
  /*$query = mysqli_query($mysqli, "SELECT codigo,nombre,unidad,stock FROM medicamentos WHERE codigo='$codigo'")
                                  or die('error '.mysqli_error($mysqli));*/
								  
	$query = mysqli_query($mysqli, "SELECT
                                    a.codigo,
                                    a.nombre,
                                    a.unidad,
                                    a.stock,
                                    (a.stock - COALESCE(SUM(CASE WHEN b.transito = 'T' THEN b.cantidad ELSE 0 END), 0)) AS disponibles,
                                    COALESCE(SUM(CASE WHEN b.transito = 'T' THEN b.cantidad ELSE 0 END), 0) AS transito
                                FROM
                                    medicamentos AS a
                                LEFT JOIN
                                    remision_detalle AS b ON a.codigo = b.codigo
                                WHERE
                                    a.codigo = '$codigo'  -- Asegúrate de que 'AB237' sea la columna correcta y pertenezca a la tabla 'medicamentos'
                                GROUP BY
                                    a.codigo, a.nombre, a.unidad, a.stock;

                                        ")
                                  or die('error '.mysqli_error($mysqli));


  $data = mysqli_fetch_assoc($query);

  $stock   = $data['stock'];
  $unidad = $data['unidad'];
  $transito = $data['transito'];
  
  if ( $data['disponibles'] == null ){
	  
		 $disponibles = $stock; 
	  }
	  else {
		  
		  $disponibles = $data['disponibles'];
	  }
  
  
  
  

	if($stock != '') {
		echo "<div class='form-group'>
                <label class='col-sm-2 control-label'>Stock</label>
                <div class='col-sm-5'>
                  <div class='input-group'>
                    <input type='text' class='form-control' id='stok' name='stock' value='$stock' readonly>
                    
                  </div>
                </div>
              </div>";
	} else {
		echo "<div class='form-group'>
                <label class='col-sm-2 control-label'>Stock</label>
                <div class='col-sm-5'>
                  <div class='input-group'>
                    <input type='text' class='form-control' id='stok' name='stock' value='' readonly>
                    <span class='input-group-addon'></span>
                  </div>
                </div>
              </div>";
	}		
	
	if($transito != '') {
		echo "<div class='form-group'>
                <label class='col-sm-2 control-label'>En transito</label>
                <div class='col-sm-5'>
                  <div class='input-group'>
                    <input type='text' class='form-control' id='transito' name='transito' value='$transito' readonly>
                  </div>
                </div>
              </div>";
	} else {
		echo "<div class='form-group'>
                <label class='col-sm-2 control-label'>En transito</label>
                <div class='col-sm-5'>
                  <div class='input-group'>
                    <input type='text' class='form-control' id='transito' name='transito' value='' readonly>
                    <!-- <span class='input-group-addon'></span> -->
                  </div>
                </div>
              </div>";
	}		
	
	if($disponibles != '') {
		echo "<div class='form-group'>
                <label class='col-sm-2 control-label'>Disponible</label>
                <div class='col-sm-5'>
                  <div class='input-group'>
                    <input type='text' class='form-control' id='disponibles' name='disponible' value='$disponibles' readonly>
                    <span class='input-group-addon'>Piezas al momento</span>
                  </div>
                </div>
              </div>";
	} else {
		echo "<div class='form-group'>
                <label class='col-sm-2 control-label'>Disponible</label>
                <div class='col-sm-5'>
                  <div class='input-group'>
                    <input type='text' class='form-control' id='disponibles' name='disponible' value='$disponibles' readonly>
                    <span class='input-group-addon'></span>
                  </div>
                </div>
              </div>";
	}		
}

	

?> 