
<?php 
require('../conexion.php');

$clave_receta="";
if(isset($_POST['clave_receta'])){
    $clave_receta= $_POST['clave_receta'];
    $query=query("SELECT A.id 'ID', B.id 'MAP_ID', A.rem_costo_unico 'REM_COSTO', A.rem_unidad_medida 'REM_UNIDADM', A.rem_entrada 'REM_ENTRADA', A.rem_estatus 'REM_ESTATUS', B.map_nombre 'MAP_NOMBRE' FROM receta_materia_prima A, materia_prima B, receta C WHERE C.id ='$clave_receta' AND A.map_id = B.id AND A.rec_id = C.id");
    //$datos=mysqli_fetch_assoc($query);
    $datos=array();
    $reg=0;
    while($resultado=mysqli_fetch_assoc($query))
    {
    $datos[$reg]["ID"]=$resultado["ID"];
    $datos[$reg]["MAP_ID"]=$resultado["MAP_ID"];
    $datos[$reg]["REM_UNIDADM"]=$resultado["REM_UNIDADM"];
    $datos[$reg]["REM_COSTO"]=$resultado["REM_COSTO"];
    $datos[$reg]["REM_ESTATUS"]=$resultado["REM_ESTATUS"];
    $datos[$reg]["MAP_NOMBRE"]=$resultado["MAP_NOMBRE"];
    $datos[$reg]["REM_ENTRADA"]=$resultado["REM_ENTRADA"];
    $reg++;
    }
}else{
    $datos="";
}

$salida = "";
if($datos != ""){ 
    if($query->num_rows > 0){
        $salida.="<table class='table' id='lista-materia-prima'>
        <thead style='background: #052659';>
            <tr>
                <th class='align-middle text-center' scope='col'>ID</th>
                <th class='align-middle text-center' scope='col'>Clave</th>
                <th class='align-middle text-center' scope='col'>Nombre</th>
                <th class='align-middle text-center' scope='col'>Precio U.</th>
                <th class='align-middle text-center' scope='col'>Unidad</th>
                <th class='align-middle text-center' scope='col'>Cantidad</th>
            </tr>
        </thead>
        <tbody>";
        for($rr=0;$rr<count($datos);$rr++){
            $id_materia_prima = $datos[$rr]['MAP_ID'];
            $datos_map = query("SELECT map_clave 'CLAVE',map_costo_unico 'MAP_COSTO' FROM materia_prima WHERE id='$id_materia_prima'");
            $result=mysqli_fetch_assoc($datos_map);
            $salida.="<tr>
                        <td>
                            <input type='text' class='form-control' name='rem_id_".$rr."' id='rem_id_".$rr."' value='".$datos[$rr]['ID']."'>
                        </td>
                        <td>
                            <input type='text' class='form-control' name='rem_clave_".$rr."' id='rem_clave_".$rr."' value='".$result['CLAVE']."'>
                        </td>
                        <td>
                            <input type='text' class='form-control' name='rem_nombre_".$rr."' id='rem_nombre_".$rr."' value='".$datos[$rr]['MAP_NOMBRE']."'>
                        </td>
                        
                        <td>
                            <input type='text' class='form-control' name='rem_costo_".$rr."' id='rem_costo_".$rr."' value='".$result['MAP_COSTO']."'>
                        </td>
                        <td>
                            <input type='text' class='form-control' name='rem_unidad_".$rr."' id='rem_unidad_".$rr."' value='".$datos[$rr]['REM_UNIDADM']."'>
                        </td>
                        <td>
                            <input type='text' class='form-control' name='rem_entrada_".$rr."' id='rem_entrada_".$rr."' value='".$datos[$rr]['REM_ENTRADA']."'>
                        </td>
                    </tr>
                ";
            }
            $salida.="
                </tbody>
                </table>";
        }else{
            $salida.="<table class='table' id='lista-materia-prima'>
        <thead style='background: #052659;'>
            <tr>
                <th class='align-middle text-center' scope='col'>ID</th>
                <th class='align-middle text-center' scope='col'>Clave</th>
                <th class='align-middle text-center' scope='col'>Nombre</th>
                <th class='align-middle text-center' scope='col'>Precio U.</th>
                <th class='align-middle text-center' scope='col'>Unidad</th>
                <th class='align-middle text-center' scope='col'>Cantidad</th>
            </tr>
        </thead>
        <tbody></tbody>
        </table>";
        }
    }
    else{
        $salida.="<table class='table'>
        <thead style='background: #052659;'>
            <tr>
                <th class='align-middle text-center' scope='col'>ID</th>
                <th class='align-middle text-center' scope='col'>Clave</th>
                <th class='align-middle text-center' scope='col'>Nombre</th>
                <th class='align-middle text-center' scope='col'>Cantidad</th>
                <th class='align-middle text-center' scope='col'>Precio U.</th>
                <th class='align-middle text-center' scope='col'>Unidad</th>
            </tr>
        </thead>
        <tbody>
            
        </tbody>
        </table>";
    }
echo $salida;
?>