<?php

if(!isset($_SESSION['idCaja']) && !isset($_SESSION['primer_inicio'])){ ?>
 <script> 
    $(document).ready(function(){
      $('#modalApertura').modal('toggle')
    }); 
  </script>
 <?php 
  $_SESSION['primer_inicio'] = 1;
}
?>
<div class="content-wrapper">

  <section class="content-header">
    
    <h1>
      
      Tablero
      
      <small>Panel de Control</small>
    
    </h1>

    <ol class="breadcrumb">
      
      <li><a href="inicio"><i class="fa fa-dashboard"></i> Inicio</a></li>
      
      <li class="active">Tablero</li>
    
    </ol>

  </section>

  <section class="content">

  <div class="box">

        <div class="box-header with-border">

        <button class="btn btn-primary" data-toggle="modal" data-target="#modalAgregarDireccion">
          
          Agregar zona

        </button>

        </div>

        <div class="box-body">
        <table class="detalle table table-bordered table-striped table-hover table-condensed">
        <thead>
         
            <tr>
                <th width="70px">#</th>
                <th>Nombre zona</th>
                <th width="120px">Acción</th>
            </tr> 

        </thead>
            <?php

            $datos = ControladorDirecciones::ctrMostrarZona();
          
            foreach($datos as $key => $data){
            ?>
                <tr>
                    <td><?= $key ?></td>
                    <td><?= $data['nombre_zona'] ?></td>
                    <td>
            <div>
                <button class="btn btn-warning btnEditarZona" idzona="<?= $data['idzona'] ?>" data-toggle="modal" data-target="#modalEditarZona"><i class="fa fa-pencil"></i></button>

                <button class="btn btn-danger btnEliminarZona" idzona="<?= $data['idzona'] ?>" idzona="<?= $data['idzona'] ?>"><i class="fa fa-trash"></i></button>
            </div>  

                    </td>
                </tr>


            <?php
            }
             ?>
             

        <tbody>
          
         
          
        </tbody>
        </table>
        </div>
  </div>

  </section>
 
</div>


<!--=====================================
MODAL AGREGAR DIRECCIONES
======================================-->

<div id="modalAgregarDireccion" class="modal fade" role="dialog">
  
  <div class="modal-dialog">

    <div class="modal-content">

      <form role="form" method="post">

        <!--=====================================
        CABEZA DEL MODAL
        ======================================-->

        <div class="modal-header" style="background:#3c8dbc; color:white">

          <button type="button" class="close" data-dismiss="modal">&times;</button>

          <h4 class="modal-title">Agregar zona</h4>

        </div>

        <!--=====================================
        CUERPO DEL MODAL
        ======================================-->

        <div class="modal-body" style="padding:0px">

          <div class="box-body" style="padding:0px">

                  <!-- ENTRADA PARA EL NOMBRE -->

                  <div class="form-row">

                    <div class="form-group col-md-12">
                      <label for="inputCity">zona:</label>
                      <div class="input-group">
                        <span class="input-group-addon"><i class="fa fa-address-card"></i></span>
                        <input type="text" class="form-control" name="zona" placeholder="Nombres" required>
                      </div>
                    </div>
                 
                  </div>
        </div>
    </div>

        <!--=====================================
        PIE DEL MODAL
        ======================================-->

        <div class="modal-footer">

          <button type="button" class="btn btn-danger" data-dismiss="modal"><i class="fa fa-arrow-circle-left"></i> Cancelar</button>

          <button type="submit" class="btn btn-primary"><i class="fa fa-save"></i> Guardar</button>

        </div>

      </form>

      <?php

        $crearZona = new ControladorDirecciones();
        $crearZona -> ctrCrearZona();

      ?>

    </div>

  </div>

</div>



<!--=====================================
MODAL EDITAR ZONA
======================================-->

<div id="modalEditarZona" class="modal fade" role="dialog">
  
  <div class="modal-dialog">

    <div class="modal-content">

      <form role="form" method="post">

        <!--=====================================
        CABEZA DEL MODAL
        ======================================-->

        <div class="modal-header" style="background:#3c8dbc; color:white">

          <button type="button" class="close" data-dismiss="modal">&times;</button>

          <h4 class="modal-title">Agregar zona</h4>

        </div>

        <!--=====================================
        CUERPO DEL MODAL
        ======================================-->

        <div class="modal-body" style="padding:0px">

          <div class="box-body" style="padding:0px">

                  <!-- ENTRADA PARA EL NOMBRE -->

                  <div class="form-row">

                    <div class="form-group col-md-12">
                      <label for="inputCity">Editar zona:</label>
                      <div class="input-group">
                        <span class="input-group-addon"><i class="fa fa-address-card"></i></span>
                        <input type="hidden" class="form-control" id="updateidzona" name="idzona" required>
                        <input type="text" class="form-control" id="editarZona" name="editarZona" placeholder="Nombres" required>
                      </div>
                    </div>
                 
                  </div>
        </div>
    </div>

        <!--=====================================
        PIE DEL MODAL
        ======================================-->

        <div class="modal-footer">

          <button type="button" class="btn btn-danger" data-dismiss="modal"><i class="fa fa-arrow-circle-left"></i> Cancelar</button>

          <button type="submit" class="btn btn-primary"><i class="fa fa-save"></i> Editar</button>

        </div>

      </form>

      <?php

        $editarZona = new ControladorDirecciones();
        $editarZona -> ctrEditarZona();

      ?>

    </div>

  </div>

</div>

<script>
$('.btnEditarZona').on("click", function(){
    var idzona = new FormData();
    idzona.append("idzona", $(this).attr('idzona'));

    $.ajax({
		    	url:"ajax/direcciones.ajax.php",
		      method: "POST",
		      data: idzona,
		      cache: false,
		      contentType: false,
		      processData: false,
		      dataType:"json",
		    }).done(function(res){
                var id = res[0].idzona;
                var nombrezona = res[0].nombre_zona
               $('#updateidzona').val(id);
               $('#editarZona').val(nombrezona);
            }).fail(function(){
                console.log("error");
            }).always(function(){
                console.log("complete");
            });
})

$('.btnEliminarZona').on("click", function(){
    var idzona = new FormData();
    idzona.append("eliminarzona", $(this).attr('idzona'));

    swal({
    title: '¿Está seguro de borrar la zona?',
    text: "¡Si no lo está puede cancelar la accíón!",
    type: 'warning',
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
      cancelButtonColor: '#d33',
      cancelButtonText: 'Cancelar',
      confirmButtonText: 'Si, borrar Zona!'
    }).then(function(result){

        if(result.value){
            $.ajax({
		    	url:"ajax/direcciones.ajax.php",
		      method: "POST",
		      data: idzona,
		      cache: false,
		      contentType: false,
		      processData: false,
		      dataType:"json",
		    }).done(function(res){
                if(res == "ok"){
                    ejecutarAlerta("Zona eliminada con exito", "zonas");
                } else {
                    ejecutarAlerta("Error al eliminar la zona", "zonas", "error");
                }
            });

        }

    })
  
})
</script>
