<?php

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

  echo '<script>

    window.location = "inicio";

  </script>';

  return;

}

?>

<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1>Datos Generales</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="inicio">Inicio</a></li>
              <li class="breadcrumb-item active">Datos Generales</li>
            </ol>
          </div>
        </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" data-toggle="modal" data-target="#modalAgregarSucursal">
          <i class="far fa-copy"></i>  
          Agregar
          </button>

          
        </div>

        <div class="card-body">
          <table class="table table-bordered table-hover dt-responsive tablas" >
          <thead>
                <tr>
                  <th style="width: 10px">#</th>
                  <th>Razon Social</th>
                  <th>RUC</th>
                  <th>Representante</th>
                  <th>Direccion</th>
                  <th>Email</th>
                  <th>Telefono</th>
                  <th>Logo</th>
                  <th>Acciones</th>
                </tr>
                </thead>
                <tbody>

                <?php 

                $item = null;
                $valor = null;
                
                $sucursal = ControladorGeneral:: ctrMostrarSucursal($item, $valor);

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

                  echo '<tr>
                          <td>'.($key+1).'</td>
                          <td>'.$value["razon_social"].'</td>
                          <td>'.$value["num_documento"].'</td>
                          <td>'.$value["representate"].'</td>
                          <td>'.$value["direccion"].'</td>
                          <td>'.$value["email"].'</td>
                          <td>'.$value["telefono"].'</td>';
                          
                          if($value["logo"] != ""){
                            echo '<td><img src="'.$value["logo"].'" class="img-thumbnail" width="40px"></td>';
                          }else{

                            echo '<td><img src="vistas/dist/img/logo/LOGO_SM_AZUL.jpg" class="img-thumbnail" width="60px"></td>';
                          }

                          echo '
                          <td>
                            <div class="btn-group">
                              <button class=" btn btn-warning btnEditarSucursal" idSucursal="'.$value["id"].'" data-toggle="modal" data-target="#modalEditarSucursal"><i class="fas fa-pencil-alt"></i></button>
                              <button class=" btn btn-danger btnEliminarSucursal" idSucursal="'.$value["id"].'" fotoSucursal="'.$value["logo"].'" representante="'.$value["representate"].'"><i class="fa fa-times"></i></button>
                            </div>


                          </td>

                        </tr>';
                }
                ?>
                </tbody>


          </table>
        </div>

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

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

   <!-- MODAL AGREGAR SUCURSAL -->

   <div class="modal fade" id="modalAgregarSucursal">
        <div class="modal-dialog modal-lg" >
          <div class="modal-content">
          <form role="form" method="post" enctype="multipart/form-data">

                <div class="modal-header" style="background: #EAA354; color: white">
                  <h4 class="modal-title">Datos Generales</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="row">
                      <div class="col-12">
                        <div class="form-group">
                          <label>Razón Social (*):</label>
                          <div class="input-group mb-3">
                            <div class="input-group-append">
                              <div class="input-group-text">
                              
                                <span class="far fa-building"></span>
                              </div>
                            </div>
                              <input type="text" class="form-control input-lg"  name="nuevoNombre" placeholder="Ingresar Razon Social" pattern="[a-zA-Z ]+" required>
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Tipo Documento (*):</label>
                                <div class="input-group mb-3">
                                    <div class="input-group-append">
                                        <div class="input-group-text">
                                            <span class="fas fa-file-alt"></span>
                                        </div>
                                    </div>
                                    <select class="form-control input-lg" name="nuevoTipoDocumento" id="nuevoTipoDocumento" required>
                                        <option value="">Seleccionar Tipo Documento</option>
                                        <option value="RUC">RUC</option>
                                        <option value="DNI">DNI</option>
                                        <option value="CEDULA">CEDULA</option>
                                    </select>
                                </div>
                            </div>
                        </div >

                        <div class="col-md-6 documento">
                           
                        </div>
                    </div>
                    <div class="row">
                      <div class="col-md-6">
                        <div class="form-group">
                          <label>Dirección (*):</label>
                          <div class="input-group mb-3">
                            <div class="input-group-append">
                              <div class="input-group-text">
                              <span class="fas fa-map-marked-alt"></span>
                              </div>
                            </div>
                            <input type="text" class="form-control input-lg"  name="nuevoDireccion" placeholder="Ingresar direccion" required>
                          </div>
                        </div>
                      </div>

                      <div class="col-md-6">
                        <div class="form-group">
                        <label>Teléfono (*):</label>
                        <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" name="nuevoTelefono1" id="nuevoTelefono1" placeholder="Ingresar Telefono" data-inputmask="'mask':'(+51) 999-999-999'" data-mask>
                        </div>
                        </div>
                      </div>
                    </div>

                    <div class="row">
                      <div class="col-md-6">
                        <div class="form-group">
                        <label>Email:</label>
                        <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" name="nuevoCorreo" id="nuevoCorreo" placeholder="Ingresar Correo">
                        </div>
                        </div>
                      </div>
                      <div class="col-md-6">
                      <div class="form-group">
                        <label>Representante:</label>
                        <div class="input-group mb-3">
                        <div class="input-group-append">
                        <div class="input-group-text">
                            <span class="fab fa-creative-commons-by"></span>
                        </div>
                        </div>
                            <input type="text" class="form-control input-lg"  name="nuevoRepresentante" id="nuevoRepresentante" placeholder="Ingresar Representante">
                        </div>
                        </div>  
                      </div>
                    </div>

                    <div class="row">
                      <div class="col-12">
                        <div class="form-group">
                              <label>Subir Logo:</label>
                              <input type="file" class="nuevaFoto" name="nuevaFoto">
                              <p class="help-block">Peso maximo de la foto 2 MB</p>
                              <img src="vistas/dist/img/usuarios/anonymous.png" class="img-thumbnail previsualizar" width="100px">
                        </div>
                      </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 
                      
                      $crearGeneral = new ControladorGeneral();
                      $crearGeneral -> ctrCrearGeneral();
                      
                      ?>
            </form>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>
      <!-- /.modal -->

       <!-- MODAL EDITAR Sucursal-->

   <div class="modal fade" id="modalEditarSucursal">
        <div class="modal-dialog modal-lg">
          <div class="modal-content">
              <form role="form" method="post" enctype="multipart/form-data">

                      <div class="modal-header" style="background: #EAA354; color: white">
                        <h4 class="modal-title">Modificar Datos Generales</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="row">
                                <div class="col-12">
                                  <div class="form-group">
                                    <label>Razón Social (*):</label>
                                    <div class="input-group mb-3">
                                      <div class="input-group-append">
                                        <div class="input-group-text">
                                        
                                          <span class="far fa-building"></span>
                                        </div>
                                      </div>
                                        <input type="text" class="form-control input-lg" name="editarNombre"  id="editarNombre" placeholder="Ingresar Razon Social" required>
                                        <input type="hidden"  name="idSucursal" id="idSucursal">
                                    </div>
                                  </div>
                                </div>
                              </div>
                              <div class="row">
                                  <div class="col-6">
                                      <div class="form-group">
                                          <label>Tipo Documento (*):</label>
                                          <div class="input-group mb-3">
                                              <div class="input-group-append">
                                                  <div class="input-group-text">
                                                      <span class="fas fa-file-alt"></span>
                                                  </div>
                                              </div>
                                              <select class="form-control input-lg" name="editarTipoDocumento">
                                                  <option value="" id="editarTipoDocumento"></option>
                                                  <option value="RUC">RUC</option>
                                                  <option value="DNI">DNI</option>
                                                  <option value="CEDULA">CEDULA</option>
                                              </select>
                                          </div>
                                      </div>
                                  </div >

                                  <div class="col-6">
                                      <div class="form-group">
                                          <label>Documento (*):</label>
                                          <div class="input-group mb-3">
                                              <div class="input-group-append">
                                                  <div class="input-group-text">
                                                      <span class="fas fa-file-alt"></span>
                                                  </div>
                                              </div>
                                              <input type="number" class="form-control input-lg" name="editarDocumento" maxlength="11"  minlength="11" id="editarDocumento" pattern="[0-9]+"  placeholder="Ingresar Numero Documento" required>
                                          </div>
                                      </div>
                                  </div>
                              </div>

                              <div class="row">
                                <div class="col-6">
                                  <div class="form-group">
                                    <label>Dirección (*):</label>
                                    <div class="input-group mb-3">
                                      <div class="input-group-append">
                                        <div class="input-group-text">
                                        <span class="fas fa-map-marked-alt"></span>
                                        </div>
                                      </div>
                                      <input type="text" class="form-control input-lg" id="editarDireccion"  name="editarDireccion" placeholder="Ingresar direccion" required>
                                    </div>
                                  </div>
                                </div>

                                <div class="col-6">
                                  <div class="form-group">
                                  <label>Teléfono (*):</label>
                                  <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" name="editarTelefono1" id="editarTelefono1" placeholder="Ingresar Telefono" data-inputmask="'mask':'(+51) 999-999-999'" data-mask>
                                  </div>
                                  </div>
                                </div>
                              </div>

                              <div class="row">
                                <div class="col-6">
                                  <div class="form-group">
                                  <label>Email:</label>
                                  <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" name="editarCorreo" id="editarCorreo" placeholder="Ingresar Correo">
                                  </div>
                                  </div>
                                </div>
                                <div class="col-6">
                                <div class="form-group">
                                  <label>Representante:</label>
                                  <div class="input-group mb-3">
                                  <div class="input-group-append">
                                  <div class="input-group-text">
                                      <span class="fab fa-creative-commons-by"></span>
                                  </div>
                                  </div>
                                      <input type="text" class="form-control input-lg" name="editarRepresentante"  id="editarRepresentante" placeholder="Ingresar Representante">
                                  </div>
                                  </div>  
                                </div>
                              </div>

                              <div class="row">
                                <div class="col-12">
                                  <div class="form-group">
                                        <label>Subir Logo:</label>
                                        <input type="file" class="nuevaFoto" name="editarFoto">
                                        <p class="help-block">Peso maximo de la foto 2 MB</p>
                                        <img src="vistas/dist/img/usuarios/anonymous.png" class="img-thumbnail previsualizar" width="100px">
                                        <input type="hidden" name="fotoActual" id="fotoActual">
                                      </div>
                                </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">Modificar</button>
                      </div>
                      <?php 
                      
                      $editarSucursal= new ControladorGeneral();
                      $editarSucursal-> ctrEditarSucursal();
                      
                      ?>
              </form>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
      <!-- /.modal -->
<?php 

$borrarSucursal= new ControladorGeneral();
$borrarSucursal-> ctrBorrarSucursal();

?>
<script>
        $(function () {
          $("#example1").DataTable();
          $('#example2').DataTable({
            "paging": true,
            "lengthChange": false,
            "searching": false,
            "ordering": true,
            "info": true,
            "autoWidth": false,
          });
        });
</script>
<script src="./vistas/js/plantilla.js"></script>

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

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

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