<?php use_helper('UrlFunctions'); ?>
<?php use_helper( 'IncludePartial' ); ?>
<style>
  .ui-autocomplete-loading { background: white url("<?=Constantes::ROOT_DIR_IMG?>common/progress.gif") right center no-repeat; }
</style>
<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">
.marcoTabActive {
  /*background:#F1F1F1 none repeat scroll 0 0;*/
  background:#A3C1E0 none repeat scroll 0 0;  font-family:Geneva, Arial, Helvetica, sans-serif;  font-size:14px;  border-top-width: 1px;
  border-right-width: 1px;  border-left-width: 1px;  border-top-style: solid;  border-right-style: solid;  border-left-style: solid;  border-top-color: #999999;
  border-right-color: #999999;  border-left-color: #999999;  font-weight: bold;  /*color:#990000;*/
}
.label { color: #444444; }
.lblSmall { color: #8D8D8D; font-size: 10px; padding-right: 8px; }
</style>
<style>
.codigoItem{ color: blue; text-decoration: underline; cursor: pointer; }
.codigoItem:hover{ text-decoration: none; cursor: pointer; }
</style>
<script>
mostrarMensajeError = function( s_MensajeError )
{
    $( "#resultContent" ).html( '<div id="mensaje_error_content" class="error">' + s_MensajeError + '</div>' );
    setTimeout( function(){ $( "#mensaje_error_content" ).fadeOut( 800 ).fadeIn( 800 ).fadeOut( 500 ).fadeIn( 500 ).fadeOut( 300 ); }, 4000 );
}
blockContent = function( s_IdElement )
{
    $( '#' + s_IdElement ).block
            ( 
                { 
                    message: null,
                    css: { cursor: 'context-menu' }
                } 
            ); 
    $( ".blockUI" ).css( "cursor", "context-menu" );
};
unBlockContent = function( s_IdElement )
{
    $( '#' + s_IdElement ).unblock();
}
</script>
<?php if( $noedit == "2" || is_null( $noedit ) ){ ?>
<div class="titleBody descripProy">
    <ul>
        <li>
            <?=$s_Titulo?>
        </li>
    </ul>
</div>
<?php } ?>
<table class="cuerpo" width="99%" border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td valign="top">
            <form id="form1" name="form1" method="POST" onsubmit="return false;"  enctype="multipart/form-data">
                <input type="hidden" name="s_TipoProc" id="s_TipoProc" value="<?=$s_TipoProc?>">
                <input type="hidden" name="s_TipoProc" id="i_Id" value="<?=$i_Id?>">
                <input type="hidden" name="TypeSub" id="TypeSub" value="0">
                <table class="" width="100%">
                    <tr>
                        <td valign="top">
                            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                    <td>
                                        <table id="tabShowScan" cellpadding="0" cellspacing="0" class="mnuElement">
                                            <tr>
                                                <td class="mnuInactivoIzq"><img src="<?=Constantes::ROOT_DIR_IMG?>common/spacer.gif" width="0" /></td>
                                                <td class="mnuInactivoTxt" nowrap="nowrap" onclick="location.href='<?=buildUrl( $action, "block=001&s_TipoProc=upd&i_Id=".$i_Id )?>';">General</td>
                                                <td class="mnuInactivoDer"><img src="<?=Constantes::ROOT_DIR_IMG?>common/spacer.gif" width="1" /></td>
                                                <?php if( $o_ALM_TransaccionCabeceraBN->getIdTransaccionCabeceraConcat() ){ ?>
                                                <td class="mnuActivoIzq"><img src="<?=Constantes::ROOT_DIR_IMG?>common/spacer.gif" width="0" /></td>
                                                <td class="mnuActivoTxt" nowrap="nowrap" onclick="location.href='<?=buildUrl( 'transacciones/detail', "block=001&noedit=$noedit&action=$action2&s_TipoProc=upd&i_Id=".$i_Id )?>';">Detalle</td>
                                                <td class="mnuActivoDer"><img src="<?=Constantes::ROOT_DIR_IMG?>common/spacer.gif" width="1" /></td>
                                                <th>&nbsp;</th>
                                                <th>&nbsp;</th>
                                                <th>&nbsp;</th>
                                                <th><input type="button" class="button" value="Imprirmir" onclick="window.open( '<?=buildUrl( 'transacciones/reporte', "block=001&i_Id=".$i_Id )?>', '_blank' );"></th>
                                                <?php } ?>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                                
                                <tr>
                                    <td class="marco">
                                        <table class="" width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
                                            <tr>
                                                <td width="100%" valign="top">
                                                        <?php include_partial( 
                                                                                "infohead", 
                                                                                array( 
                                                                                        "o_ALM_TransaccionCabeceraBN" => $o_ALM_TransaccionCabeceraBN,
                                                                                        "o_GSS_CompaniaBN"            => $o_GSS_CompaniaBN,
                                                                                        "o_ALM_AlmacenMastOrgBN"      => $o_ALM_AlmacenMastOrgBN,
                                                                                        "o_ALM_AlmacenMastDstBN"      => $o_ALM_AlmacenMastDstBN,
                                                                                        "o_ALM_TransaccionMastBN"     => $o_ALM_TransaccionMastBN,
                                                                                        "o_ALM_TipoDocumentoBN"       => $o_ALM_TipoDocumentoBN,
                                                                                        "o_ALM_TipoDocumentoDocRefBN"       => $o_ALM_TipoDocumentoDocRefBN,
                                                                                     )
                                                                             ); ?>
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="marco">
                                    
                                        <div id="resultContent" style=""></div>
                                        
                                        <div>
                                            <?php if( !$noedit ){ ?>
                                            <table class="marco" width="100%" border="0" style="">
                                                <tr>
                                                    <td nowrap="nowrap">
                                                        <table  border="0">
                                                            <tr>
                                                                <td>
                                                                    <input id="btnAgregar" type="button" class="button" value="Agregar Item" onclick=""/>
                                                                    <script>
                                                                    $( "#btnAgregar" )
                                                                    .click
                                                                    (
                                                                        function()
                                                                        {
                                                                            //console.log( 132 );
                                                                            $( "#content_nvo" ).html( $( "#content_copy_nvo" ).html() );
                                                                            $( "#content_nvo tr #IdItem" ).on( "keyup", eventoPopup );
                                                                            $( "#content_nvo tr #IdItem" ).on( "blur", executeFindItem );
                                                                            $( "#content_nvo tr #IdItem" ).focus();
                                                                            openPopup();
                                                                        }
                                                                    );
                                                                    </script>
                                                                </td>
                                                                <td>
                                                                    &nbsp;
                                                                </td>
                                                            </tr>
                                                        </table>
                                                    </td>
                                                </tr>
                                            </table>
                                            <?php } ?>

                                            <fieldset style="padding-left: 6px; padding-right: 6px; margin-top: 0;border-left: 1px solid #999999; border-right: 1px solid #999999; margin-left: 0; margin-right: 0;">
                                                <div id="content_result_grid1">
                                                    <table class="grid" cellspacing="0" cellpadding="0" border="0" align="center" width="100%" style="border-bottom: 1px solid #999999;">
                                                        <tbody id="">
                                                            <tr>
                                                                <td width="2%"  align="center" class="header"><b>No</b></td>
                                                                <td width="10%" align="center" class="header"><b>C&oacute;digo</b></td>
                                                                <td width="30%" align=""       class="header"><b>Descripci&oacute;n</b></td>
                                                                <td width="10%" align="center" class="header"><b>Stock Actual</b></td>
                                                                <td width="10%" align="center" class="header"><b>U.Medida</b></td>
                                                                <td width="10%" align="center" class="header"><b>Cantidad</b></td>
                                                                <!--<td width="10%" align="center" class="header"><b>Precio</b></td>-->
                                                                <!--<td width="10%" align="center" class="header"><b>Ex.Impuesto</b></td>-->
                                                                <!--<td width="10%" align="center" class="header"><b>Total</b></td>-->
                                                                <td width="5%"  align="center" class="header">&nbsp;</td>
                                                            </tr>
                                                        </tbody>
                                                        <tbody id="content_nvo">
                                                        </tbody>
                                                    </table>
                                                </div>
                                            </fieldset>
                                        </div>
                                    
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </form>
        </td>
    </tr>
</table>

                                        <?php if( !$noedit ){ ?>
                                        <table cellpadding="0" cellspacing="0" width="100%" border="0" style="">
                                            <tr>
                                                <td align="center" >
                                                    <input type="button" class="button" value="Salir" onclick="location.href='<?php echo buildUrl( 'transacciones/list', 'block=001' ) ?>'">
                                                </td>
                                            </tr>
                                        </table>
                                        <?php } ?>
                                        
<div id="content_result_grid_pl1" style="display:none;">
    <table class="grid" cellspacing="0" cellpadding="0" borde r="0" align="center" width="100%" style="border-bottom: 1px solid #999999;">
        <tbody id="content_copy_nvo">
            <tr>
                <td align="center">&nbsp;</td>
                <td align="center"><input type="text" class="text" name="IdItem" id="IdItem" value="" style="width: 85%; text-align: center;" ></td>
                <td align="" id="DescripcionCompleta">&nbsp;</td>
                <td align="center" id="StockActual">&nbsp;</td>
                <td align="center">
                    <select class="combo" name="IdUnidad" id="IdUnidad">
                        <option value=""> - Seleccione - </option>
                        <?php foreach ( $a_MST_UnidadMast as $i_Key => $o_MST_UnidadMastBN ){ ?>
                        <option value="<?=$o_MST_UnidadMastBN->getIdUnidad()?>"> <?=$o_MST_UnidadMastBN->getDescripcionCorta()?> </option>
                        <?php } ?>
                    </select>
                </td>
                <td align="center"><input type="text" class="text number" name="Cantidad" id="Cantidad" value="" style="width: 60%; text-align: center;" ></td>
                <!--<td align="center"><input type="text" class="text number" name="PrecioUnitario" id="PrecioUnitario" value="" style="width: 60%; text-align: center;" ></td>-->
                <!--<td align="center" id="AfectoImpuesto2Flag">&nbsp;</td>-->
                <!--<td align="center">&nbsp;</td>-->
                <td align="center"><input type="button" class="button" value="Guardar" onclick="executeSaveOrderDetail();"></td>
            </tr>
        </tbody>
    </table>
</div>
<div id="dialog"><iframe id="myIframe" src="" frameborder="0" height="100%" width="100%"></iframe></div>
<script>
    objMainTab.modalUIDialog( "#dialog", { title : "Buscar Item", width : 530, height  : 350 } );
</script>
<script>
b_GuardarItem = false;
executeFindItem = function()
{
    var item_code = $( "#content_nvo tr #IdItem" ).val();
    var str_serialize = "s_ItemId=" + item_code + "&type=";
    executeAjaxJson( 
                        "<?php echo buildUrl( 'transacciones/findItem', "block=001" ) ?>", 
                        str_serialize,
                        function( o_json )
                        {
                            console.log( o_json );
                            //ocultarCargaImg();
                            if( o_json.error )
                            {
                                mostrarMensajeError( o_json.mensaje_error );
                                return false;
                            }
                            if( o_json.result )
                            {
                                b_GuardarItem = true;
                                $( "#content_nvo tr #DescripcionCompleta" ).html( o_json.DescripcionCompleta );
                                $( "#content_nvo tr #AfectoImpuesto2Flag" ).html( o_json.NoAfectoImpuestoVentasFlag );
                                $( "#content_nvo tr #IdUnidad" )[0].value = o_json.UnidadCompra;
                                $( "#content_nvo tr #StockActual" ).html( o_json.StockActual );
                            }
                        } 
                   );
}
eventoPopup = function( object )
{
    console.log( object );
    if( object.which === 107 ) 
    {
        object.target.value = "";
		openPopup();
    }
}
openPopup = function()
{
    $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'transacciones/listItem', 'block=001&type=' )?>' );
    $( '#dialog' ).dialog( 'open' );	
}
asignarItem = function( id_item, item_descripcion )
{
    $( "#content_nvo tr #IdItem" ).val( id_item );
    $( "#content_nvo tr #DescripcionCompleta" ).html( item_descripcion );
    $( "#content_nvo tr #IdItem" ).trigger( "blur" );
}
</script>
<script>
validacionPrecio = function( v_PrecioValue )
{
    if( v_PrecioValue == "" )
    {
        alert( "Precio vacia." );
        return false;
    }
    if( !$.isNumeric( v_PrecioValue ) )
    {
        alert( "Precio no valida." );
        return false;
    }
    var f_Precio = parseFloat( v_PrecioValue );
    if( f_Precio <= 0 )
    {
        alert( "El Precio ingresado no es valida." );
        return false;
    }
    return true;
}
validacionCantidad = function( v_CantValue )
{
    if( v_CantValue == "" )
    {
        alert( "Cantidad vacia." );
        return false;
    }
    if( !$.isNumeric( v_CantValue ) )
    {
        alert( "Cantidad no valida." );
        return false;
    }
    i_Cant = parseInt( v_CantValue );
    if( i_Cant < 1 )
    {
        alert( "La cantidad ingresada no es valida." );
        return false;
    }
    return true;
}
executeSaveOrderDetail = function()
{
    if( b_GuardarItem )
    {
        var elm = $( '#content_nvo' ).find( "input[name='Cantidad']" );
        var b_Return = validacionCantidad( elm.val() );
        if( !b_Return ) return false;
        
        /*var elm = $( '#content_nvo' ).find( "input[name='PrecioUnitario']" );
        var b_Return = validacionPrecio( elm.val() );
        if( !b_Return ) return false;*/
        
        var i_Id = $( "#i_Id" ).val();
        
        var str_serialize = $('#content_nvo :input').serialize();
        str_serialize += "&i_Id=" + i_Id + "&s_TipoProc=nvo";
        console.log( str_serialize );
    	
        executeAjaxJson( 
                            "<?php echo buildUrl( 'transacciones/saveDetail', "block=001" ) ?>", 
                            str_serialize,
                            function( o_json )
                            {
                                console.log( o_json );
                                //ocultarCargaImg();
                                if( o_json.error )
                                {
                                    mostrarMensajeError( o_json.mensaje_error );
                                    return false;
                                }
                                executeFindDetail();
                                b_GuardarItem = false;
                            } 
                       );
    }
    else
    {
        alert( "No se puede guardar." );
    }
}
executeFindDetail = function()
{
	var i_Id = $( "#i_Id" ).val();
	
    var a_Params = [];
    a_Params.push( "i_Id=" + i_Id );
    <?php if( $noedit ){ ?>
    a_Params.push( "noedit=1" );
    <?php } ?>
    executeAjaxUpdater( 
                            "content_result_grid1", 
                            "<?php echo buildUrl( 'transacciones/findDetail', "block=001" ) ?>", 
                            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( 'transacciones/deleteDetail', "block=001" )?>", 
                                a_Params.join( "&" ), 
                                function( o_json )
                                {
                                    if( o_json.error )
                                    {
                                        mostrarMensajeError( o_json.mensaje_error );
                                        return true;
                                    }
                                    executeFindDetail();
                                } 
                          );
    }
}
</script>
<script>
//ESCAPE, DETECTA EL F5, CTRL+F5
$( document ).bind
(
    'keyup', 
    function( e ) 
    {
        //console.log( e );
        if( e.which === 116 ) 
        {
           
        }
        if( e.which === 82 && e.ctrlKey ) 
        {
           
        }
        if( e.which === 119 ) 
        {
           
        }
        if( e.which === 27 ) 
        {
            //$( "#content_nvo" ).html( "" );
            executeFindDetail();
        }
    }
);
</script>
<script>
executeFindDetail();
</script>