<?php
$b_Control = true;
?>
<script>
mostrarCargaImg = function(){ $('#s_ImagenLoad')[0].style.display = 'inline'; }
ocultarCargaImg = function(){ $('#s_ImagenLoad')[0].style.display = 'none'; }

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 );
}
</script>
<script>
ejecutarGuardar = function()
{
    ///////////////////////////////////////////////////////////////
    mostrarCargaImg();
    var strFormSerialize = $( 'form' ).serialize();
    executeAjaxJson
    ( 
        "<?php echo buildUrl( 'cot/saveAddEdit' ) ?>",
        strFormSerialize,
        function( o_json )
        {
            console.log( o_json );
            ocultarCargaImg();
            if( o_json.error )
            {
                mostrarMensajeError( o_json.mensaje_error );
                return false;
            }
            //return false;
            if( !o_json.error )
            {
                location.href = "<?=buildUrl( 'cot/detailVta', "i_Id=" )?>" + o_json.id;
            }
            else
            {
            	mostrarMensajeError( o_json.mensaje_error );
                //location.href = "<?=buildUrl( 'cot/list', "" )?>";
            }
        } 
    );
}
</script>

<div id="resultContent" style=""></div>
<input type="hidden" name="IdUbigeo" id="IdUbigeo" value="">

<table width="100%">
    <tr>
        <td width="100%" valign="top" >
            <fieldset class="formGen" style="margin-top:0px">
                <table border="0" cellspacing="6" cellpadding="0" style="">
                     <tr>
                        <td nowrap="nowrap" width="8%" class="cabecera"><label class="label req" for="OrdenTipoId" >Tipo Orden:</label></td>
                        <td nowrap="nowrap" width="45%">
                            <select class="combo required" id="OrdenTipoId" name="OrdenTipoId" onchange="">
                                <option value="1" <?php if( $o_VTA_OrdenBN->getOrdenTipoId()=='1' ){ echo "selected"; } ?>> ESTANDAR </option>
                                <option value="2" <?php if( $o_VTA_OrdenBN->getOrdenTipoId()=='2' ){ echo "selected"; } ?>> MASTER </option>
                            </select>
                        </td>
                        
                        <td nowrap="nowrap" width="5%">&nbsp;</td>
                        
                        <td nowrap="nowrap" width="15%" class="cabecera"><label class="label req" for="MonedaCod" >Moneda :</label></td>
                        <td nowrap="nowrap" width="25%">
                            <select class="combo required" id="MonedaCod" name="MonedaCod">
                                <?php foreach ( $a_GSS_MonedaMast as $i_Key => $o_GSS_MonedaMastBN ){ ?>
                                <option value="<?=$o_GSS_MonedaMastBN->getIdMoneda()?>" <?php if( $o_VTA_OrdenBN->getMonedaCod()==$o_GSS_MonedaMastBN->getIdMoneda() ){ echo "selected"; } ?>> <?=$o_GSS_MonedaMastBN->getDescripcion()?> </option>
                                <?php } ?>
                            </select>
                        </td>
                    </tr>
                    
                    <tr>
                        <td nowrap="nowrap" class="cabecera"><label class="label req" for="ClienteNumero" >Cliente :</label></td>
                        <td nowrap="nowrap" >
                            <input type="hidden" name="ClienteNumero" id="ClienteNumero" value="<?=$o_VTA_OrdenBN->getClienteId()?>">
                            <table border="0" cellpadding="0" cellspacing="0" width="100%">
                                <tr>
                                    <td width="20%">
                                        <input type="text" class="text" id="ClienteRUC" 
                                                name="ClienteRUC" 
                                                value=""  
                                                onblur="buscarInfoxNumDocumento( this.value )" onfocus="this.select();"
                                                style="width:100%; text-align:center;">
                                    </td>
                                    <td width="2%">&nbsp;</td>
                                    <td width="50%">
                                        <input class="text required" type="text" name="ClienteNombre" id="ClienteNombre" value="" style="width:100%" onfocus="this.select();"/>
                                    </td>
                                    <td width="5%">
                                        <?php if( !$o_VTA_OrdenBN->getOrdenId() ){ ?>
                                        <input type="button" value="..." class="button" id="btnCliente">
                                        <?php } ?>
                                    </td>
                                    <td width="5%">
                                        <?php if( !$o_VTA_OrdenBN->getOrdenId() ){ ?>
                                        <button type="button" class="button" id="btnNvoClie">
                                            <?=addImgButton( "btnSave.gif" );?>
                                        </button>
                                        <script type="text/javascript">
                                        $( "#btnNvoClie" ).click
                                        (
                                            function()
                                            {
                                            	$( '#myIframe2' ).attr( 'src', '<?=buildUrl( 'pro/addeditclie', 's_TipoProc=nvo' )?>' );
                                                $( '#dialog2' ).dialog( 'open' );
                                            }
                                        );
                                        </script>
                                        <?php } ?>
                                    </td>
                                </tr>
                            </table>
                            <script>
                            $( "#ClienteNombre" ).autocomplete
                            (
                                {
                                    source: "<?=buildUrl( 'pro/request', "TipoProc=Autocomplete" )?>",
                                    minLength: 2,
                                    select: function( event, ui ) 
                                    {
                                        console.log( ui.item.id );
                                        $( "#ClienteNumero" ).val( ui.item.id );
                                        buscarInfoCliente();
                                    }
                                }
                            );
                            $( "#btnCliente" ).click
                            (
                                function()
                                {
                                    $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'pro/clie', '' )?>' );
                                    $( '#dialog' ).dialog( 'open' );
                                }
                            );
                            </script>
                            <script>
                            asignarValores = function( Id, Nombre, Documento )
                            {
                                $( "#ClienteNumero" ).val( Id );
                                $( "#ClienteNombre" ).val( Nombre );
                                //$( "#ClienteRUC" ).val( Documento );
                                $( '#dialog' ).dialog( 'close' );
                                $( '#dialog2' ).dialog( 'close' );
                                buscarInfoCliente();
                            }
                            limpiarValores = function()
                            {
                            	//LIMPIAR
                                $( "#ClienteNombre" ).val( "" );
                                $( "#ClienteRUC" ).val( "" );
                                $( "#sDireccion" ).val( "" );
                                $( "#sDepartamento" ).val( "" );
                                $( "#sProvincia" ).val( "" );
                                $( "#sDistrito" ).val( "" );
                                $( "#sPais" ).val( "PERU" );
                            }
                            buscarInfoCliente = function()
                            {
                            	limpiarValores();
                                /////////
                            	mostrarCargaImg();
                            	var IdPersona= $( "#ClienteNumero" ).val();
                            	var strFormSerialize = "TipoProc=getClienteInfo&IdPersona=" + IdPersona;
                                executeAjaxJson( 
                                                    "<?php echo buildUrl( 'pro/request', "" ) ?>", 
                                                    strFormSerialize,
                                                    function( o_json )
                                                    {
                                                        console.log( o_json );
                                                        ocultarCargaImg();
                                                        if( o_json.error )
                                                        {
                                                            mostrarMensajeError( o_json.mensaje_error );
                                                            return false;
                                                        }
                                                        $( "#ClienteNombre" ).val( o_json.Nombre );
                                                        $( "#ClienteRUC" ).val( o_json.Documento );
                                                        $( "#sDireccion" ).val( o_json.Direccion );
                                                        $( "#sDepartamento" ).val( o_json.DepartamentoDesc );
                                                        $( "#sProvincia" ).val( o_json.ProvinciaDesc );
                                                        $( "#sDistrito" ).val( o_json.DistritoDesc );
                                                        $( "#IdUbigeo" ).val( o_json.IdUbigeo );
                                                        $( "#cboCanal" ).val( o_json.CanalCliente );
                                                        $( "#sPais" ).val( "PERU" );
                                                    } 
                                               );
                            }
                            </script>
                        </td>
                        
                        <td nowrap="nowrap" >&nbsp;</td>
                        
                        <td nowrap="nowrap" class="cabecera"><label class="label req" for="OrdenFecha">Fecha Orden</label></td>
                        <td nowrap="nowrap">
                            <input type="text" class="text required" name="OrdenFecha" id="OrdenFecha" value="<?=$o_VTA_OrdenBN->getOrdenFecha()?>" style="width:70%; text-align:center; color: #fc6d00; font-size: 12px; font-weight: bold;" disabled="disabled" />
                            <script type="text/javascript">
                            //$( "#OrdenFecha" ).datepicker( { dateFormat: "yy-mm-dd", changeYear:true, yearRange: "-1:+1", changeMonth: true, onClose: function ( s_date ){ this.focus(); $( "#OrdenVenc" ).val( s_date ); } } );
                            </script>
                        </td>
                    </tr>
                    
                    <tr>
                        <td nowrap="nowrap" align="" rowspan="2"><label class="label req" for="Comentario" >Direcci&oacute;n :</label></td>
                        <td nowrap="nowrap" rowspan="2">
                            <table cellpadding="0" cellspacing="0" border="0" width="100%">
                                <tr>
                                    <td style="width: 60%;">
                                        <textarea class="" name="sDireccion" id="sDireccion" rows="1" style="width: 95%"></textarea>
                                        <br><label style="color:DarkGrey;font-size: 9px;">Direcci&oacute;n</label>
                                    </td>
                                    <td style="width: 35%;">
                                        <input type="text" readonly class="text" name="sDistrito" id="sDistrito" value="" style="width: 95%;" /><br>
                                        <label style="color:DarkGrey;font-size: 9px;">Distrito</label>
                                    </td>                             
                                </tr>
                            </table>
                            <table cellpadding="0" cellspacing="0" border="0" width="100%">
                                <tr>
                                    <td>
                                        <input type="text" readonly class="text" name="sDepartamento" id="sDepartamento" value="" /><br>
                                        <label style="color:DarkGrey;font-size: 9px;">Departamento</label>
                                    </td>
                                    <td>
                                        <input type="text" readonly class="text" name="sProvincia" id="sProvincia" value="" /><br>
                                        <label style="color:DarkGrey;font-size: 9px;">Provincia</label>
                                    </td>
                                    <td>
                                        <input type="text" readonly class="text" name="sPais" id="sPais" value="" /><br>
                                        <label style="color:DarkGrey;font-size: 9px;">Pa&iacute;s</label>
                                    </td>
                                </tr>
                            </table>
                        </td>
                        
                        <td rowspan="2">&nbsp;</td>
                        
                        <td nowrap="nowrap" ><label class="label req" for="OrdenVenc" >Fecha Expiraci&oacute;n</label></td>
                        <td nowrap="nowrap" class="cabecera" align="center">
                            <input type="text" class="text required" name="OrdenFecha" id="OrdenFecha" value="<?=$o_VTA_OrdenBN->getOrdenVenc()?>" style="width:70%; text-align:center; color: #fc6d00; font-size: 12px; font-weight: bold;" disabled="disabled"/>
                        </td>
                    </tr>
                    
                    <tr>
                        <td nowrap="nowrap" ><label class="label req" for="MonedaCambioValor" >Tipo de Cambio</label></td>
                        <td nowrap="nowrap" class="cabecera" align="center">
                            <select class="combo" id="MonedaCambioValor" name="MonedaCambioValor">
                                <?php if( GSS_TipoCambioFN::getStaticTipoCambioCobranza() ){ ?>
                                <option value="<?=GSS_TipoCambioFN::getStaticTipoCambioCobranza()?>"> COBRANZA - <?=GSS_TipoCambioFN::getStaticTipoCambioCobranza()?> </option>
                                <?php } ?>
                            </select>
                        </td>
                    </tr>
                    
                    <tr>
                        <td nowrap="nowrap" class="cabecera"><label class="label req" for="Comentario" >Comentario :</label></td>
                        <td nowrap="nowrap" >
                            <textarea class=""  name="Comentario" id="Comentario" style="width:99%" rows="2"><?=$o_VTA_OrdenBN->getOrdenObs()?></textarea>
                        </td>
                         
                        <td nowrap="nowrap">&nbsp;</td>

                        
                        <td nowrap="nowrap" class="cabecera"><label class="label req" for="IdVendedor" >Vendedor :</label></td>
                        <td nowrap="nowrap">
                            <select class="combo required" id="IdVendedor" name="IdVendedor"><option value=""> - Seleccione - </option>
                            <?php foreach ( VTA_VendedorFN::select_all() as $a_Vendedor ) { ?>
                                <option value="<?=$a_Vendedor['IdPersona']?>"> <?=$a_Vendedor['NombreCompleto']?> </option>
                            <?php } ?></select>
                        </td>
                        
                    </tr>
                    <tr>
                        <td nowrap="nowrap" class="cabecera"><label class="label req" for="FactRegla" >Tipo de Factura :</label></td>
                        <td nowrap="nowrap">
                            <select class="combo required" id="FactRegla" name="FactRegla">
                                <option value="MER"> NORMAL </option>
                                <option value="TTG"> TRANSFERENCIA A TITULO GRATUITO </option>
                            </select>
                        </td>
                        <td>&nbsp;</td>
                        <td nowrap="nowrap" class="cabecera"><label class="label req" for="cboCanal" >&nbsp;</label></td>
                        <td nowrap="nowrap">&nbsp;</td>
                    </tr>
                </table>
            </fieldset>
        </td>
    </tr>
</table>

<?php if( $o_VTA_OrdenBN->getOrdenId() ){ ?>
<script>
setTimeout
(
    function()
    {
    	buscarInfoCliente();
    }, 
    500
);

</script>
<?php } ?>

<script>
var b_ControlBuscarSerie = true;

buscarInfoxNumDocumento = function( value )
{
	mostrarCargaImg();
	var idtipodocumento = $( "#IdTipoDocumento" ).val();
    executeAjaxJson
    ( 
        '<?php echo buildUrl( 'pro/request' ) ?>', 
        'TipoProc=getInfoCliexNumDoc&IdTipoDocumento=BV&NumDniRuc=' + value, 
        function( o_json )
        {
            console.log( o_json );
            ocultarCargaImg();
            if( o_json.Result )
            {
            	$( "#ClienteNumero" ).val( o_json.idpersona );
                buscarInfoCliente();
            }
            else
            {
            	limpiarValores();
            }
        }
    );
}
</script>

<?=crearFuentePopupIframe( array( "div_id"=>"dialog", "iframe_id"=>"myIframe", "popup_title"=>"Buscar Cliente", "popup_width"=>"530", "popup_height"=>"350", "modal" => true ) )?>

<?=crearFuentePopupIframe( array( "div_id"=>"dialog2", "iframe_id"=>"myIframe2", "popup_title"=>"Cliente", "modal" => true ) )?>

<script>
$.validator.setDefaults
(
    {
        submitHandler: function( form ) 
        {
            ejecutarGuardar();
        }
    }
);
$( "#form1" ).validate();
</script>