<?php

if($_SESSION["nombre"] == "Inventario"){

  echo '<script>

    window.location = "inicio";

  </script>';

  return;

}

?>

<style>
.select2-container--default .select2-selection--single {
    background-color: #fff;
    border: 1px solid #aaa;
    border-radius: 4px;
    height: calc(2.25rem + 2px);
}
.row1 {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: nowrap;
}
</style>

<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header" style="padding: 2px;">
      <div class="container-fluid">
        <div class="row mb-2">
          
        </div>
      </div><!-- /.container-fluid -->
    </section>

    
    <!-- Main content -->
    <section class="content">

      <!-- Default box -->
      <div class="card">
        <div class="card-header">
          <button class="btn btn-outline-warning btn-sm" data-toggle="modal" data-target="#modalAgregarProveedor">
            Agregar Proveedor
          </button>
        </div>


        <div class="card-body">
          <table style="font-size:85%" class="table table-hover table-bordered table-sm dt-responsive proveedor">
          <thead>
                <tr>
                  <th>#</th>
                  <th>RUC/DNI</th>
                  <th>Nombre/Razon Social</th>
                  <th>Teléfono</th>
                  <th>Dirección</th>
                  <th>email</th>
                  <th>Acciones</th>
                </tr>
                </thead>
                <tbody>
                <?php 

                  $item = null;
                  $valor = null;

                  $proveedor = ControladorProveedor:: ctrMostrarProveedores($item, $valor);

                  foreach($proveedor as $key => $value){

                    echo '<tr>
                            <td>'.($key+1).'</td>
                            <td>'.$value["ruc_dni"].'</td>
                            <td>'.$value["razon_social"].'</td>
                            <td>'.$value["telefono"].'</td>
                            <td>'.$value["direccion"].'</td>
                            <td>'.$value["email"].'</td>
                            <td>
                              <div class="btn-group">
                                <button class=" btn btn-warning btnEditarProveedor" idProveedor="'.$value["id"].'" data-toggle="modal" data-target="#modalEditarProveedor"><i class="fas fa-pencil-alt"></i></button>
                                <button class=" btn btn-danger btnEliminarProveedor" idProveedor="'.$value["id"].'" ><i class="fa fa-times"></i></button>
                              </div>
                            </td>
                          </tr>';
                  }
                  ?>
                </tbody>
          </table>
          <input type="hidden" value="<?php echo $_SESSION['nombre'];?>" id="perfilOculto">
        </div>

      </div>
      <!-- /.card -->

    </section>
    <!-- /.content -->
  </div>

  <!--=====================================
MODAL AGREGAR CLIENTE
======================================-->

 <div class="modal fade" id="modalAgregarProveedor">
        <div class="modal-dialog">
          <div class="modal-content">
    <form role="form" method="post" >

            <div class="modal-header" style="background: #EAA354; color: white">
              <h4 class="modal-title">Agregar Proveedor</h4>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>

            <div class="modal-body">
              <div class="box-body">

              <div class="form-group">
                <div class="input-group mb-3">
                <div class="input-group-append">
                  <div class="input-group-text">
                    <span class="fas fa-key"></span>
                  </div>
                  </div>
                  <select class="form-control input-lg " name="proveedordoc" style ="width: 10px;" id="proveedordoc" required>
                                        <option value="">Seleccionar Documento</option>
                                        <option value="RUC">RUC</option>
                                        <option value="DNI">DNI</option>
                  </select>
                  </div>
                </div>

                <div class="form-group proveedores">

                </div>

                <div class="form-group">
                <div class="input-group mb-3">
                <div class="input-group-append">
                  <div class="input-group-text">
                    <span class="fas fa-user"></span>
                  </div>
                  </div>
                    <input type="text" class="form-control input-lg focusNext razon_social" tabindex="2" onkeyup="javascript:this.value=this.value.toUpperCase();" onkeypress="return permite(event, 'car')"  name="nuevoNombre" id="nuevoNombre" placeholder="Ingresar Nombre o Razón social" required>
                  </div>
                </div>

                <div class="form-group">
                <div class="input-group mb-3">
                <div class="input-group-append">
                  <div class="input-group-text">
                    <span class="fas fa-envelope"></span>
                  </div>
                  </div>
                    <input type="email" class="form-control input-lg focusNext" tabindex="3"  name="nuevoCorreo" id="nuevoCorreo" placeholder="Ingresar Correo">
                  </div>
                </div>

                <div class="form-group">
                <div class="input-group mb-3">
                <div class="input-group-append">
                  <div class="input-group-text">
                    <span class="fas fa-phone"></span>
                  </div>
                  </div>
                    <input type="text" class="form-control input-lg focusNext" tabindex="4"  name="nuevoTelefono" id="nuevoTelefono" placeholder="Ingresar Telefono" data-inputmask="'mask':'(+51) 999-999-999'" data-mask>
                  </div>
                </div>
                        <div class="form-group">
                          <div class="input-group mb-3">
                            <div class="input-group-append">
                              <div class="input-group-text">
                                <span class="fas fa-map-marker"></span>
                              </div>
                            </div>
                            <input type="text" class="form-control input-lg focusNext direccion" onkeypress="return permite(event, 'num_car')" onkeyup="javascript:this.value=this.value.toUpperCase();" tabindex="5"  name="nuevoDireccion" id="nuevoDireccion" placeholder="Ingresar Direccion" required>
                          </div>
                        </div>
              </div>
            </div>


            <div class="modal-footer justify-content-between">
              <button type="button" class="btn btn-default" data-dismiss="modal">Salir</button>
              <button type="submit" tabindex="10" class="btn btn-warning">Guardar</button>
            </div>
            <?php 
            
            $crearProveedor = new ControladorProveedor();
            $crearProveedor -> ctrCrearProveedor();
            
            ?>
        </form>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>
      <!-- /.modal -->
      
  <!--=====================================
MODAL EDITAR CLIENTE
======================================-->

<div class="modal fade" id="modalEditarProveedor">
        <div class="modal-dialog">
          <div class="modal-content">
    <form role="form" method="post" >

            <div class="modal-header" style="background: #EAA354; color: white">
              <h4 class="modal-title">Editar Proveedor</h4>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>

            <div class="modal-body">
              <div class="box-body">

              <div class="form-group">
                <div class="input-group mb-3">
                <div class="input-group-append">
                  <div class="input-group-text">
                    <span class="fas fa-key"></span>
                  </div>
                  </div>
                    <input type="number" class="form-control input-lg focusNext" tabindex="1" name="editarRuc" id="editarRuc" required>
                    <input type="hidden"  name="idProveedor" id="idProveedor">
                  </div>
                </div>

                <div class="form-group">
                <div class="input-group mb-3">
                <div class="input-group-append">
                  <div class="input-group-text">
                    <span class="fas fa-user"></span>
                  </div>
                  </div>
                    <input type="text" class="form-control input-lg focusNext" onkeyup="javascript:this.value=this.value.toUpperCase();" tabindex="2" name="editarNombre" id="editarNombre"  required>
                  </div>
                </div>

                <div class="form-group">
                <div class="input-group mb-3">
                <div class="input-group-append">
                  <div class="input-group-text">
                    <span class="fas fa-envelope"></span>
                  </div>
                  </div>
                    <input type="email" class="form-control input-lg focusNext" tabindex="3" name="editarCorreo" id="editarCorreo" >
                  </div>
                </div>

                <div class="form-group">
                <div class="input-group mb-3">
                <div class="input-group-append">
                  <div class="input-group-text">
                    <span class="fas fa-phone"></span>
                  </div>
                  </div>
                    <input type="text" class="form-control input-lg focusNext" tabindex="4" name="editarTelefono" id="editarTelefono"  data-inputmask="'mask':'(+51) 999-999-999'" data-mask >
                  </div>
                </div>
                
                  
                    <div class="form-group">
                      <div class="input-group mb-3">
                        <div class="input-group-append">
                          <div class="input-group-text">
                            <span class="fas fa-map-marker"></span>
                          </div>
                        </div>
                        <input type="text" class="form-control input-lg focusNext" onkeyup="javascript:this.value=this.value.toUpperCase();" name="editarDireccion" tabindex="5" id="editarDireccion" required>
                      </div>
                    </div>
   
              </div>
            </div>


            <div class="modal-footer justify-content-between">
              <button type="button" class="btn btn-default" data-dismiss="modal">Salir</button>
              <button type="submit" class="btn btn-warning">Guardar</button>
            </div>
            <?php 
            
          $editarProveedor = new ControladorProveedor();
          $editarProveedor -> ctrEditarProveedor();
            
            ?>
        </form>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>
      <!-- /.modal -->
      <?php 
      
$borrarProveedor = new ControladorProveedor();
$borrarProveedor -> ctrBorrarProveedor();

?>
  <script src="./vistas/js/proveedor.js"></script>
  <script>
  $(function () {
    //Initialize Select2 Elements
    $('.select2bs4').select2({
      theme: 'bootstrap4'
    })

    //Initialize Select2 Elements
    $('.select2').select2()


  })

</script>
<script>
    document.addEventListener('keypress', function(evt) {

// Si el evento NO es una tecla Enter
if (evt.key !== 'Enter') {
  return;
}

let element = evt.target;

// Si el evento NO fue lanzado por un elemento con class "focusNext"
if (!element.classList.contains('focusNext')) {
  return;
}

// AQUI logica para encontrar el siguiente
let tabIndex = element.tabIndex + 1;
var next = document.querySelector('[tabindex="'+tabIndex+'"]');

// Si encontramos un elemento
if (next) {
  next.focus();
  event.preventDefault();
}
});
  </script>

<script>
       var elemento = document.getElementById("compra");
      elemento.className += " active";

      var elemento = document.getElementById("com");
      elemento.className += " menu-open";

      var elemento = document.getElementById("proveedor");
      elemento.className += " active";

  function permite(elEvento, permitidos) {
  // Variables que definen los caracteres permitidos
  var numeros = "0123456789";
  var caracteres = " aábcdeéfghiíjklmnñoópqrstuvwxyzAÁBCDEÉFGHIÍJKLMNÑOÓPQRSTUVWXYZ";
  var numeros_caracteres = numeros + caracteres;
  var teclas_especiales = [8, 37, 39, 46];
  // 8 = BackSpace, 46 = Supr, 37 = flecha izquierda, 39 = flecha derecha
 
 
  // Seleccionar los caracteres a partir del parámetro de la función
  switch(permitidos) {
    case 'num':
      permitidos = numeros;
      break;
    case 'car':
      permitidos = caracteres;
      break;
    case 'num_car':
      permitidos = numeros_caracteres;
      break;
  }
 
  // Obtener la tecla pulsada 
  var evento = elEvento || window.event;
  var codigoCaracter = evento.charCode || evento.keyCode;
  var caracter = String.fromCharCode(codigoCaracter);
 
  // Comprobar si la tecla pulsada es alguna de las teclas especiales
  // (teclas de borrado y flechas horizontales)
  var tecla_especial = false;
  for(var i in teclas_especiales) {
    if(codigoCaracter == teclas_especiales[i]) {
      tecla_especial = true;
      break;
    }
  }
 
  // Comprobar si la tecla pulsada se encuentra en los caracteres permitidos
  // o si es una tecla especial
  return permitidos.indexOf(caracter) != -1 || tecla_especial;
}


</script>
