<?php
session_start();
require_once('../conexion.php');
require_once('../include/sesiones.php');
$clie_id=$_GET['id'];   
$can=mysqli_query($con,"SELECT * from cliente where clie_id='$clie_id'") or die(mysqli_error($con)); 
$dato=mysqli_fetch_array($can); 
$clie_tipo_doc=$dato['clie_tipo_doc'];
$clie_nombre=$dato['clie_nombre'];
$clie_contribuyente=$dato['clie_contribuyente'];
$clie_regimen=$dato['clie_regimen'];
$tarifas_tar_id=$dato['tarifas_tar_id'];
$clie_departamento=$dato['clie_departamento'];
$clie_ciudad=$dato['clie_ciudad'];
$clie_facturador=$dato['facturador'];
$clie_activo=$dato['clie_activo'];

if ($clie_nombre == 'CLIENTE MOSTRADOR' || $clie_nombre == 'CONSUMIDOR FINAL') 
{ 
    $readonly='readonly';
}
else
{
    $readonly='';
}
?>
<form name="form_modificar12" id="form_modificar12" method="post" action="" >
    <div class="modal-body ">
        <div class="tabbable">
            <ul class="nav nav-tabs">
                <li class="active"><a href="#datosm" data-toggle="tab">DATOS PERSONALES </a></li>
                <li class=""><a href="#avanzadom" data-toggle="tab">OPCIONES AVANZADAS </a></li>
            </ul>
            <div class="tab-content">
                <div id="resultado_modificar"></div>
                <div class="tab-pane active" id="datosm">
                    <label>Tipo Documento</label>
                    <select name="tipo" id="tipo" class="form-control name_list input-sm" required >
                        <?php
                            $datos = mysqli_query($api,"SELECT * FROM type_document_identifications where id='$clie_tipo_doc'") ;
                            while( $fila = mysqli_fetch_array( $datos ) )
                            {
                            echo' <option value="'.$fila['id'].'">'.$fila['name'].'</option>';
                            }
                            $datos = mysqli_query($api,"SELECT * FROM type_document_identifications where id<>'$clie_tipo_doc' ") ;
                            while( $fila = mysqli_fetch_array( $datos ) )
                            {
                            echo' <option value="'.$fila['id'].'">'.$fila['name'].'</option>';
                            }
                            
                        ?>
                    </select>
                    <div class=" col-md-9" style="padding:0px;">
                        <label>Documento</label>
                        <input type="hidden" name="id" id="id" value="<?php echo $dato['clie_id']; ?>" >
                        <input type="number"  name="cc" id="cc" onblur="CalcularDv()" class="form-control input-sm" autocomplete="off" value="<?php echo $dato['clie_cc']; ?>"<?php echo $readonly; ?> >
                    </div> <!-- /.form-group -->
                    <div class="col-md-3" style="padding-left:10px"> <!-- /.form-group -->
                        <label>Dv</label>
                        <input type="number" name="dv" id="dv" class="form-control input-sm" autocomplete="off" value="<?php echo $dato['clie_dv']; ?>" <?php echo $readonly; ?> >
                    </div>
                    <label>Nombre Comercial</label>
                    <input type="text" name="nombre" id="nombre" class="form-control input-sm" value="<?php echo $dato['clie_nombre']; ?>" <?php echo $readonly; ?>>
                    <label>Nombre Representante</label>
                    <input type="text" name="empresa" id="empresa" class="form-control input-sm" autocomplete="off" value="<?php echo $dato['clie_empresa']; ?>" autofocus="autofocus">
                    <label>Correo</label>
                    <input type="text" name="correo" id="correo" class="form-control input-sm" autocomplete="off" value="<?php echo $dato['clie_correo']; ?>">
                    <label>Telefono</label>
                    <input type="number" name="telefono" id="telefono" class="form-control input-sm" autocomplete="off" value="<?php echo $dato['clie_telefono']; ?>">
                    <div class="col-md-6" style="padding-left:0px">
                        <label>Departamento</label>
                        <section id="">
                            <select name="departamento" id="departamento" class="form-control name_list input-sm"  >
                                <?php
                                    $datos = mysqli_query($api,"SELECT * FROM departments where id='$clie_departamento'") ;
                                    $fila = mysqli_fetch_array( $datos );
                                    echo '<option value="'.$fila['id'].'">'.$fila['name'].'</option>';

                                    $datos = mysqli_query($api,"SELECT * FROM departments order by name asc") ;
                                    while( $fila = mysqli_fetch_array( $datos ) )
                                    {
                                    echo '<option value="'.$fila['id'].'">'.$fila['name'].'</option>';
                                    }
                                ?>
                            </select>
                        </section>
                    </div>
                    <div class="col-md-6">
                        <label>Ciudad</label>
                        <section id="">
                            <select name="ciudad" id="ciudad" class="form-control name_list input-sm"  >
                                <?php
                                    $datos = mysqli_query($api,"SELECT * FROM municipalities where id='$clie_ciudad' ") ;
                                    $fila = mysqli_fetch_array( $datos );
                                    echo '<option value="'.$fila['id'].'">'.$fila['name'].'</option>';

                                    $datos=mysqli_query($api,"SELECT * FROM municipalities where department_id='$clie_departamento'") ;
                                    while( $fila = mysqli_fetch_array( $datos ) )
                                    {
                                    echo '<option value="'.$fila['id'].'" >'.$fila['name'].'</option>';
                                    }
                                ?>
                            </select>
                        </section>
                    </div>
                    <label>Dirección</label>
                    <input type="text"  name="direccion" id="direccion" class="form-control input-sm" autocomplete="off"  value="<?php echo $dato['clie_direccion']; ?>" >
                    <?php if($negocio_neg_id=='7') { ?>
                    <div>
                        <label>Barrio</label>
                        <input type="text" name="barrio" id="barrio" class="form-control input-sm" autocomplete="off"  value="<?php echo $dato['clie_barrio']; ?>" autofocus="autofocus">
                    </div>
                    <?php } else { ?>
                          <input type="hidden" name="barrio" id="barrio" class="form-control input-sm" autocomplete="off"  value="<?php echo $dato['clie_barrio']; ?>" autofocus="autofocus">
                    <?php } ?>



                   
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                        <button id="btn_modificar" type="submit"  class="btn btn-success waves-effect waves-light"><i class="icon-ok"></i> <strong>Modificar</strong></button> 
                    </div>
                </div><!-- /.datos -->

                <div class="tab-pane" id="avanzadom">
                     <div>
                        <label>Tipo Contribuyente</label>
                        <select name="contribuyente" id="contribuyente" class="form-control name_list input-sm" required >
                            <?php
                                $datos = mysqli_query($api,"SELECT * FROM type_organizations where id='$clie_contribuyente' ") ;
                                while( $fila = mysqli_fetch_array( $datos ) )
                                {
                                 
                                echo' <option value="'.$fila['id'].'">'.$fila['name'].'</option>';
                                }

                                $datos = mysqli_query($api,"SELECT * FROM  type_organizations where id<>'$clie_contribuyente' ") ;
                                while( $fila = mysqli_fetch_array( $datos ) )
                                {
                                echo' <option value="'.$fila['id'].'">'.$fila['name'].'</option>';
                                }
                                ?>
                        </select>
                    </div>
                    <div>
                        <label>Tipo Regimen</label>
                        <select name="regimen" id="regimen" class="form-control name_list input-sm" required >
                            <?php
                                $datos = mysqli_query($api,"SELECT * FROM type_regimes where id='$clie_regimen' ") ;
                                while( $fila = mysqli_fetch_array( $datos ) )
                                {
                                  
                                echo' <option value="'.$fila['id'].'">'.$fila['name'].'</option>';
                                }
                                $datos = mysqli_query($api,"SELECT * FROM  type_regimes where id<>'$clie_regimen' ") ;
                                while( $fila = mysqli_fetch_array( $datos ) )
                                {
                                echo' <option value="'.$fila['id'].'">'.$fila['name'].'</option>';
                                }
                                ?>
                        </select>
                    </div>

                    <?php
                        $per=mysqli_query($con,"SELECT * FROM modulo_permiso where usuario='$usu_id' and mod_id='13' and modp_nombre='Asignar Tarifa'") ;
                        $per=mysqli_fetch_array($per);
                        $estado=$per['estado'];
                        if ($estado =='Activo') {
                          
                    ?>
                        <label>Tarifa</label>
                        <select name="tarifa" id="tarifa" class="form-control name_list input-sm drop" required >
                        <?php
                            $datost=mysqli_query($con,"SELECT * FROM tarifas where tar_id='$tarifas_tar_id'");
                            $filat=mysqli_fetch_array($datost);
                            echo '<option value="'.$filat['tar_id'].'">'.$filat['tar_nombre'].'</option>';

                            $datos=mysqli_query($con,"SELECT * FROM tarifas where empresa='$empresa' and tar_id<>'$tarifas_tar_id'");
                            while($fila=mysqli_fetch_array($datos))
                            {
                                echo '<option value="'.$fila['tar_id'].'">'.$fila['tar_nombre'].'</option>';
                            }
                            ?>
                        </select>
                    <?php    }   else   {    ?>     
                        <input type="hidden" name="tarifa" id="tarifa" value="<?php echo $dato['tarifas_tar_id']; ?>" readonly>
                    <?php    }   ?>



                    <div class="">

                        <?php
                            $per=mysqli_query($con,"SELECT * FROM modulo_permiso where usuario='$usu_id' and mod_id='13' and modp_nombre='Asignar Credito'") ;
                            $per=mysqli_fetch_array($per);
                            $estado=$per['estado'];
                            if ($estado =='Activo') {
                                $readonly='';
                            }
                            else
                            {
                                $readonly='readonly';
                            }
                        ?>


                        <label>Credito</label>
                        <select NAME="credito" id="credito" onChange="xxxOnChange(this)" class="form-control name_list input-sm drop" <?php echo $readonly; ?> >
                            <?php
                                  $datos = mysqli_query($con,"SELECT * FROM cliente where clie_id='$clie_id'") ;
                                  $fila = mysqli_fetch_array( $datos );
                                  $clie_credito=$fila['clie_credito'];
                                  if($clie_credito==1)
                                  {
                                    echo' <option value="1">Activado</option>';
                                    echo' <option value="0">Desactivado</option>';
                                  }
                                  else 
                                  {
                                    echo' <option value="0">Desactivado</option>';
                                    echo' <option value="1">Activado</option>';
                                  } 
                            ?>
                        </select>
                        <div id="1" style="display: <?php if($clie_credito==1){echo'';}else {echo'none';}?>;">
                            <div class="">
                                <label>Cupo Credito</label>
                                <input type="number"  name="cupo" id="cupo" class="form-control input-sm" autocomplete="off"  value="<?php echo$dato['clie_cupo_credito']; ?>"  onKeyUp="Totales()"  <?php echo $readonly; ?> >
                                <input type="hidden"  name="cupo1" id="cupo1" class="form-control input-sm" autocomplete="off"  value="<?php echo $dato['clie_cupo_credito']; ?>" >
                            </div> <!-- /.form-group -->
                            <div class="">
                                <label>Disponible</label>
                                <input type="hidden"  name="disponible1" id="disponible1" class="form-control input-sm" autocomplete="off" value="<?php echo$dato['clie_disponible']; ?>" readonly="readonly" >
                                <input type="number"  name="disponible" id="disponible" class="form-control input-sm" autocomplete="off" value="<?php echo$dato['clie_disponible']; ?>" readonly="readonly" >
                            </div> <!-- /.form-group -->
                        </div>
                        
                        <div id="0" style="display:<?php if($clie_credito==1){echo'none';}else {echo'';}?>;"> 
                            <div class="">
                             <label></label>
                            </div>
                        </div>
                        <div id="00" style="display: "></div>
                            <?php
                                if($facturador=='si') 
                                { ?>
                                 <div class="">
                                <label>Facturador Electronico</label>
                                <select name="facturador" id="facturador" class="form-control name_list input-sm">
                                <?php 
                                    if($clie_facturador=='si')
                                    {
                                        echo' <option value="si">Si</option>';
                                        echo' <option value="no">No</option>';
                                    }
                                      else 
                                    {
                                        echo' <option value="no">No</option>';
                                        echo' <option value="si">Si</option>';
                                    } 
                                ?>
                                </select>
                            </div>
                            <?php } else { ?> 
                                <input type="hidden" name="facturador" id="facturador" value="">
                            <?php }   


                                $per=mysqli_query($con,"SELECT * FROM modulo_permiso where usuario='$usu_id' and mod_id='13' and modp_nombre='Asignar Tarifa'") ;
                                $per=mysqli_fetch_array($per);
                                $estado=$per['estado'];
                                if ($estado =='Activo') {
                                  
                                ?>

                                <label>Activo</label>
                                <select name="activar" id="activar" class="form-control name_list input-sm">
                                <?php 
                                    if($clie_activo=='no')
                                    {
                                        echo' <option value="no">No</option>';
                                        echo' <option value="si">Si</option>';
                                        
                                    }
                                      else 
                                    {
                                        echo' <option value="si">Si</option>';
                                        echo' <option value="no">No</option>';
                                    } 
                                ?>
                                </select>

                                <?php    }   else   {    ?>     
                                <input type="hidden" name="activar" id="activar" value="<?php echo $clie_activo; ?>" readonly>
                                <?php    }   ?>
                            
                    </div> 
              
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                        <button id="btn_modificar2" type="submit" name="btn_modificar2" class="btn btn-success waves-effect waves-light"><i class="icon-ok"></i> <strong>Modificar</strong></button> 
                    </div>
                </div><!-- /.avanzado -->
            </div>
        </div>
    </div><!-- /.modal-content -->
</form>
<script type="text/javascript">
  $(document).ready(function(){
    $("#departamento").change(function(){
      $.get("php_clientes/lista_ciudades_m.php","departamento="+$("#departamento").val(), function(data){
        $("#ciudad").html(data);
        console.log(data);
      });
    });
  });


$("#form_modificar12" ).submit(function(event) {
$('#btn_modificar2').attr("disabled", true);
var parametros = $(this).serialize();
$.ajax({
    type: "POST",
    url: "php_clientes/editar_cliente.php",
    data: parametros,
    beforeSend: function(objeto){
     $('#resultado_modificar').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
    },
    
    success: function(datos){
    $("#resultado_modificar").html(datos);
    $('#btn_modificar2').attr("disabled", false);
    $('#edit_cliente').modal('hide');
    $('body').removeClass('modal-open');
    $('.modal-backdrop').remove();
    load(1);
    loadTabla();
    loadcliente();
    loadtotal();

   }
});
event.preventDefault();
})
</script>      

<script type="text/javascript">
function xxxOnChange(sel){
if (sel.value=="1"){
    divC = document.getElementById("1");
    divC.style.display = "";
    divT = document.getElementById("0");
    divT.style.display = "none";
}else {
    divC = document.getElementById("1");
    divC.style.display="none";
    divT = document.getElementById("0");
    divT.style.display = "";
    }
}
function Totales() {
with (document.forms["form_modificar12"]) 
    {
        var resta = Number( cupo1.value ) - Number( disponible1.value );
        var totalResult = Number( cupo.value ) - resta;
        disponible.value = totalResult;
    }
}

function roundTo(num,pow){
if( isNaN( num ) ){
    num = 0;
}
num *= Math.pow(10,pow);
num = (Math.round(num)/Math.pow(10,pow))+ "" ;
if(num.indexOf(".") == -1)
    num += "." ;
while(num.length - num.indexOf(".") - 1 < pow)
    num += "0" ;
return num;
}

function CalcularDv() {
    var arreglo, x, y, z, i, nit1, dv1;
    nit1 = document.form_modificar12.cc.value;
    if (isNaN(nit1)) {
        document.form_modificar12.dv.value = "X";
        alert('Numero del Nit no valido, ingrese un numero sin puntos, ni comas, ni guiones, ni espacios');
    } else {
        arreglo = new Array(16);
        x = 0;
        y = 0;
        z = nit1.length;
        arreglo[1] = 3;
        arreglo[2] = 7;
        arreglo[3] = 13;
        arreglo[4] = 17;
        arreglo[5] = 19;
        arreglo[6] = 23;
        arreglo[7] = 29;
        arreglo[8] = 37;
        arreglo[9] = 41;
        arreglo[10] = 43;
        arreglo[11] = 47;
        arreglo[12] = 53;
        arreglo[13] = 59;
        arreglo[14] = 67;
        arreglo[15] = 71;
        for (i = 0; i < z; i++) {
            y = (nit1.substr(i, 1));
            x += (y * arreglo[z - i]);
        }
        y = x % 11
        if (y > 1) { dv1 = 11 - y; } else { dv1 = y; }
        document.form_modificar12.dv.value = dv1;
    }
}  
</script>