<?php use_helper('UrlFunctions'); ?>
<?php use_helper( 'UtilFunctions' ); ?>
<?php use_helper( 'IncludePartial' ); ?>
<style>
.header { background-color: #E1E1E1; color: #444444; font-family: Arial,Helvetica,sans-serif; font-size: 11px; font-weight: bold; }
.pager { background-color: #F4F4F4; background-image: none; border-bottom: thin solid #999999; }
</style>
<style type="text/css">
    .ui-dialog .ui-dialog-titlebar-close
    {
        position: absolute;
        right: .3em;
        top: 50%;
        width: 20px;
        margin: -10px 0 0 0;
        padding: 1px;
        height: 20px;
        background-position: -96px -128px;
        /*background-image: url("images/ui-icons_056b93_256x240.png");*/
        background-image: url("<?=Constantes::ROOT_DIR_APP?>web/css/jquery/themes/start_1.12.1/images/ui-icons_056b93_256x240.png");
    }
</style>


<div class="shadow-sm p-3 mb-2 bg-white rounded card card-body">
    <div class="row">
        <div class="col mb-1">
            <h4 class="text-info text-nowrap mb-0"><?=$s_Titulo?></h4>
        </div>
        <div class="form-row mx-1">
            <input type="button" class="btn btn-primary btn-block" value="Regresar" onclick="location.href='<?=buildUrl('requerimiento/list' )?>';" >
        </div>
    </div>
</div>

<ul class="nav nav-tabs m-1" id="myTab" role="tablist" style="margin-bottom: 0px !important;">
    <li class="nav-item">
        <a class="nav-link " id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="false" onclick="location.href='<?=buildUrl( 'requerimiento/addedit', "TipoProc=upd&i_Id=".$i_Id )?>';">General</a>
    </li>
    <li class="nav-item">
        <a class="nav-link active" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="true">Detalle</a>
    </li>
</ul>

<div class="tab-content card" style="border-top: 0px;" id="myTabContent">
    <div class="tab-pane fade show active" id="profile" role="tabpanel" aria-labelledby="profile-tab">
        <div class="form-row mx-0">
            <?php include_partial( "infohead", array( "o_DefaultBN" => $o_DefaultBN ) ); ?>
        </div>
    </div>
</div>

<div class="dropdown-divider"></div>

<form name="form1" id="form1" method="POST" action="" onsubmit="">
    <input type="hidden" id="i_Id" name="i_Id" value="<?=$i_Id?>">
    <input type="hidden" id="Control" name="Control" value="1">
    
    <div class="card">
        <!--  <h5 class="card-header">Datos Generales</h5>-->
        
        <div class="card-body" style="padding: 2px;">
            
            <div class="form-row mx-0">
                <div class="col-md-4 mb-1">
                    &nbsp;
                    <!--<label class="col-form-label" for="">Linea</label>
                    --><?/*=crearSelect( array( "IdLinea", "getIdLinea", "getDescripcion" ), $a_MST_ClaseLinea, $IdLinea, "listprecios", "llenarComboLinea", array( "IdFamilia" ), "GetFamilia", array( "IdLinea", "IdFamilia", "CodigoBarras" ) )*/?>
                </div>
                <div class="col-md-4 mb-1">
                    &nbsp;
                    <!--<label class="col-form-label" for="IdRango">Familia</label>
                    --><?/*=crearSelect( array( "IdFamilia", "getIdFamilia", "getDescripcion" ), $a_MST_ClaseFamilia, $IdFamilia )*/?>
                </div>
                <div class="col-md-4 mb-1">
                    <button type="button" class="btn btn-primary btn-block" id="btnAgregar">Agregar Producto</button>
                    <script>
                        $( "#btnAgregar" )
                            .click
                            (
                                function()
                                {
                                    openPopup();
                                }
                            );

                        closeWindow = function()
                        {
                            $( '#dialog2' ).dialog( 'close' );
                            $( '#dialog' ).dialog( 'close' );
                        }
                        openPopup = function()
                        {
                            var s_param = "type/01";
                            $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'transacciones/listItem' )?>' + s_param );
                            $( '#dialog' ).dialog( 'open' );
                        }

                        asignarItem = function( id_item, item_descripcion, id_unidad )
                        {
                            pr( arguments );

                            executeSaveOrderDetail( id_item );

                            closeWindow();

                            setTimeout
                            (
                                function()
                                {

                                }, 300
                            )

                            //$( "#content_nvo tr #IdItem" ).trigger( "blur" );
                        }
                    </script>
                </div>
            </div>
    
        </div>
        
    </div>
    
    <div class="dropdown-divider"></div>
    
    <div id="content_alert" style=""></div>
    
    <fieldset class="">
        <!-- <legend class="w-auto">Productos</legend> -->
        <div class="table-responsive" id="content_result_grid1">
            <table class="table  table-bordered table-hover">
                <thead class="table-primary">
                    <tr>
                        <th scope="col" style="text-align: center;">Nro</th>
                        <th scope="col" style="text-align: center;">Codigo</th>
                        <th scope="col" style="text-align: center;">Descripcion</th>
                        <th scope="col" style="text-align: center;">Stock Actual</th>
                        <th scope="col" style="text-align: center;">Stock Minimo</th>
                        <th scope="col" style="text-align: center;">Diferencia</th>
                        <th scope="col" style="text-align: center;">&nbsp;</th>
                    </tr>
                </thead>
            </table>
        </div>
    </fieldset>
</form>

<script>
    executeSaveOrderDetail = function( id_item )
    {
        var str_serialize = "Id=<?=$i_Id?>&IdItem=" + id_item + "&ReqCantidadSugerida=1";

        executeAjaxJson
        (
            "<?php echo buildUrl( 'requerimiento/saveDetail' ) ?>",
            str_serialize,
            function( o_json )
            {
                if( o_json.error )
                {
                    mostrarMensajeError( o_json.mensaje_error );
                    return false;
                }
                executeFindDetail();
                b_GuardarItem = false;
            }
        );
    }
    executeFindDetail = function()
    {
        var i_Id = $( "#i_Id" ).val();

        var a_Params = [];
        a_Params.push( "i_Id=" + i_Id );
        a_Params.push( "ControlEdit=1" );

        executeAjaxUpdater
        (
            "content_result_grid1",
            "<?php echo buildUrl( 'requerimiento/findDetailReq' ) ?>",
            a_Params.join( "&" ),
            function( response )
            {
            }
        );
    }
    executeDeleteDetail = function( i_Id )
    {
        x = confirm( "Esta seguro de eliminar el detalle?" );
        if( x )
        {
            a_Params = [];
            a_Params.push( "i_Id=" + i_Id );
            executeAjaxJson
            (
                "<?=buildUrl( 'requerimiento/deleteDetail' )?>",
                a_Params.join( "&" ),
                function( o_json )
                {
                    if( o_json.error )
                    {
                        mostrarMensajeError( o_json.mensaje_error );
                        return true;
                    }
                    executeFindDetail();
                }
            );
        }
    }

    actualizarCantidad = function( IdDet, Cant )
    {
        a_Params = [];
        a_Params.push( "TipoProc=ActualizarCantidadDet" );
        a_Params.push( "IdRequerimientoDetalle=" + IdDet );
        a_Params.push( "Qty=" + Cant );
        executeAjaxJson
        (
            "<?=buildUrl( 'requerimiento/request' )?>",
            a_Params.join( "&" ),
            function( o_json )
            {
                if( o_json.error )
                {
                    mostrarMensajeError( o_json.mensaje_error );
                    return true;
                }
                executeFindDetail();
            }
        );
    }

    executeFindDetail();
</script>

<script>
//ESCAPE, DETECTA EL F5, CTRL+F5
$( document ).bind
(
    'keyup', 
    function( e ) 
    {
        //console.log( e.which );
        if( e.which === 116 ) 
        {
           
        }
        if( e.which === 82 && e.ctrlKey ) 
        {
           
        }
        if( e.which === 119 ) 
        {
           
        }
        if( e.which === 27 ) //ESCAPE
        {
            executeFindDetail();
        }
    }
);
//$( "#CodigoBarras" ).focus();
</script>

<?=crearFuentePopupIframe( array( "div_id"=>"dialog", "iframe_id"=>"myIframe", "popup_title"=>"Buscar Producto", "modal"=>true ) )?>