<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( 'ubigeo/saveAddEdit', "block=001" ) ?>", 
                        strFormSerialize,
                        function( o_json )
                        {
                            console.log( o_json );
                            ocultarCargaImg();
                            if( o_json.error )
                            {
                                mostrarMensajeError( o_json.mensaje_error );
                                return false;
                            }
                            //return false;
                            switch ( o_json.type_sub )
                            {
                                case "0":
                                    location.href = "<?=buildUrl( 'ubigeo/addedit', "block=001&s_TipoProc=upd&i_Id=")?>" + o_json.id;
                                break;
                                case "1":
                                    location.href = "<?=buildUrl( 'ubigeo/list', "block=001")?>";
                                break;
                            }
                        } 
                   );
}
function LoadCampos(Objeto) {
	var TipoUbigeo = Objeto.value;
	switch (TipoUbigeo) {
		case 'DEPT':
	        $("#rowIdDepartamento").css("display","none");
	        $("#rowIdDepartamento").css("visibility","hidden");
	        $("#rowIdProvincia").css("display","none");
	        $("#rowIdProvincia").css("visibility","hidden");
	        break;
	    case 'PROV':
	        $("#rowIdDepartamento").css("display","");
	        $("#rowIdDepartamento").css("visibility","visible");
	        $("#rowIdProvincia").css("display","none");
	        $("#rowIdProvincia").css("visibility","hidden");
	        break;
	     case 'DIST':
	        $("#rowIdDepartamento").css("display","");
	        $("#rowIdDepartamento").css("visibility","visible");
	        $("#rowIdProvincia").css("display","");
	        $("#rowIdProvincia").css("visibility","visible");
	        break;
	     case '':
	        $("#rowIdDepartamento").css("display","none");
	        $("#rowIdDepartamento").css("visibility","hidden");
	        $("#rowIdProvincia").css("display","none");
	        $("#rowIdProvincia").css("visibility","hidden");
	        break;
    } 
	
}
</script>

<?php if( $o_MST_UbigeoBN->getIdUbigeo() ){ $s_Disabled = "disabled"; }else{ $s_Disabled = ""; } ?>
<input type="hidden" name="i_Id" id="i_Id" value="<?=$o_MST_UbigeoBN->getIdUbigeo()?>">
<div id="resultContent" style=""></div>


<div class="card">
    <div class="container">
            <div class="form-row mx-0">
                <div class="col-md-6 mb-1">
                    <label class="col-form-label" for="Estado">Tipo de Ubigeo</label>
                    <select <?=$s_Disabled?> class="form-control" id="TipoUbigeo" name="TipoUbigeo" onchange="LoadCampos(this)">
                        <option value=""> - Seleccione - </option>
                        <option value="DEPT" <?php if($o_MST_UbigeoBN->getTipo() == "DEPT" ){ echo "SELECTED"; } ?> >Departamento </option>
                        <option value="PROV" <?php if($o_MST_UbigeoBN->getTipo() == "PROV" ){ echo "SELECTED"; } ?> >Provincia </option>
                        <option value="DIST" <?php if($o_MST_UbigeoBN->getTipo() == "DIST" ){ echo "SELECTED"; } ?> >Distrito </option>
                    </select>	
                </div>
                <div class="col-md-6 mb-1" >
                    <label class="col-form-label" for="Descripcion">Descripcion</label>
                    <input type="text" class="form-control" name="Descripcion" id="Descripcion" placeholder="" value="<?=$o_MST_UbigeoBN->getDescripcion()?>" onblur="this.value=this.value.toUpperCase();" required>
                </div>
            </div>
            <div class="form-row mx-0">
            	<div class="col-md-6 mb-1" id="rowIdDepartamento" <?php if($TipoUbigeo == "DEPT" || $TipoUbigeo == ""){ ?> style="display: none; visibility: hidden;" <?php }?>>
                    <label class="col-form-label" for="IdDepartamento">Departamento</label>
                    <select <?=$s_Disabled?> class="form-control" id="IdDepartamento" name="IdDepartamento" onchange="$('#IdProvincia')[0].options.length = 1;  publicForSelectAjaxJSON('<?php echo buildUrl( 'ubigeo/request', 'block=001') ?>', $( 'form' ).serialize() + '&TipoProc=LoadProv', 'IdProvincia');">
                        <option value=""> - Seleccione - </option>
                        <?php foreach ( $a_MST_UbigeoFNDepartamentos as $i_Key => $o_MST_UbigeoFNDepartamento ){ ?>
                        <option value="<?=$o_MST_UbigeoFNDepartamento->getIdUbigeo()?>" <?php if( $o_MST_UbigeoFNDepartamento->getIdUbigeo()==$IdDpto ){ echo "SELECTED"; } ?> > <?=$o_MST_UbigeoFNDepartamento->getDescripcion()?> </option>
                        <?php } ?>
                    </select>	
                </div>
                <div class="col-md-6 mb-1" id="rowIdProvincia" <?php if($TipoUbigeo == "PROV" || $TipoUbigeo == "DEPT" || $TipoUbigeo == ""){ ?>style="display: none; visibility: hidden;" <?php }?>>
                    <label class="col-form-label" for="IdProvincia">Provincia</label>
                    <select  <?=$s_Disabled?> class="form-control" id="IdProvincia" name="IdProvincia" >
                        <option value=""> - Seleccione - </option>
                         <?php 
                         if(is_array($a_MST_UbigeoFNProvincias)){
                        	 foreach ( $a_MST_UbigeoFNProvincias as $i_Key => $o_MST_UbigeoFNProvincia ){ ?>
                        		<option value="<?=$o_MST_UbigeoFNProvincia->getIdUbigeo()?>" <?php if( $o_MST_UbigeoFNProvincia->getIdUbigeo()==$IdProv ){ echo "SELECTED"; } ?> > <?=$o_MST_UbigeoFNProvincia->getDescripcion()?> </option>
                        <?php } 
                         }?>
                    </select>	
                </div>
            </div>
            
            <?php if( $o_MST_UbigeoBN->getIdUbigeo() ){ ?>
            <br>
            <button type="button" class="btn btn-primary btn-lg btn-block" id="btnAudi">Auditoria</button>
            
                    <script>
                    $( "#btnAudi" )
                    .click
                    (
                        function()
                        {
                            $( '#myIframe' ).attr( 'src', '<?=buildUrl( 'ubigeo/audi', 'block=001&i_Id='.$o_MST_UbigeoBN->getIdUbigeo() )?>' );
                            $( '#dialog' ).dialog( 'open' );
                        }
                    )
                    </script>
            <br>
            <?php } ?>
           <div class="form-group col-14"><hr style="border-top: 1px solid #8c8b8b;"></div> 
        
    </div>
    
</div>

                                           
<div id="dialog"><iframe id="myIframe" src="" frameborder="0" height="100%" width="100%"></iframe></div>
<div id="dialog2"><iframe id="myIframe2" src="" frameborder="0" height="100%" width="100%"></iframe></div>
<script>
    objMainTab.modalUIDialog( "#dialog", { title : "Auditoria", width : 425, height  : 230 } );
    objMainTab.modalUIDialog( "#dialog2", { title : "Auditoria", width : 425, height  : 230 } );
</script>
                                                    
<script>
$.validator.setDefaults
(
    {
        submitHandler: function( form ) 
        {
            ejecutarGuardar();
        }
    }
);
$( "#form1" ).validate();
</script>
