<?php 
include_once('../../../php/sessionStart.php'); 
include_once('../../../php/db/dbExec.php');

$empCod = $_SESSION['empCod'];

# QUERYS
$sEmpresas = "SELECT * FROM empresas ORDER BY emp_nom ASC";

# EJECUCION DE QUERYS
$db = new dbExec();
$select["empresas"] = $db->query($sEmpresas);
$db->close();

?>
<?php include_once('../includes/htmlOpen.php'); ?>
    <main class="main" style="">
      <?php 
        $li[0] = '<i class="fas fa-file-invoice"></i>';
        $li[1] = $_GET["opcNom"];
        include_once('../../../php/includes/ol.php');
        $modalImp = "modalImp";
        include_once('../../../php/includes/modalImp.php');
      ?>

      <div class="container-as">
        <div class="row">
          <div class="col">
            <ul class="nav nav-pills mb-1" id="pills-tab" role="tablist">
              <li class="nav-item">
                <a class="nav-link active" id="pills-0-tab" data-toggle="pill" href="#pills-0" role="tab" aria-controls="pills-0" aria-selected="true">Cambio Empresa</a>
              </li>
            </ul>
            <div class="tab-content bg-transparent border-0" id="pills-tabContent">
              <div class="tab-pane fade show active" id="pills-0" role="tabpanel" aria-labelledby="pills-0-tab">
                <form action="" name="thisform" autocomplete="off" enctype="multipart/form-data">
                  <div class="form-group">
                    <div class="form-row">
                      <div class="col-md-7">
                        <label for="selEmp">Empresa a seleccionar:</label>
                        <select class="form-control form-control-sm form-control-as" id="selEmp" name="selEmp">
                          <?php
                            while ($rec = $db->fetchAssoc($select['empresas'])) {
                              echo  '<option value="'.$rec['emp_cod'].'">'.utf8_encode($rec['emp_nom']).'</option>';
                            }
                          ?>
                        </select>
                      </div>
                    </div>
                    <div class="form-row">
                    </div>
                  </div>

                  <div class="form-group">
                    <div class="form-row">
                      <div class="form-group col-7">
                        <button class="btn btn-success btn-lg btn-block" id="btnGuardar" type="button">Cambiar</button>
                      </div>
                    </div>
                  </div>
                </form>

              </div>
            </div>
          </div>
        </div>
      </div>
    </main>
<?php include_once('../../../php/includes/htmlClose.php'); ?>

<script>
  var pressEnter = false;
  var inputVendedor = "txtCod"
  var nuevo = true;
  
  const selEmp = document.thisform.selEmp;

  $(document).ready(function() {
    $(this).find('select:visible:first').focus();
    selEmp.value = '<?php echo $empCod; ?>';

    // SUBMIT FORM
    $('form').submit(function() {
      return false;
    });

    // VALID PRECIO UNIT., DESCUENTO %
    $("#selEmp")
    .focus(function (){
      selEmp.value = '<?php echo $empCod; ?>';
      console.log(selEmp.value);
    });
    
    // CLICK GUARDAR
    $("#btnGuardar").click(function (){
      console.log("codigo : "+selEmp.value);

      const parametros = new FormData();
      parametros.append("action", "guardar");

      parametros.append("nuevo", nuevo);
      parametros.append("selEmp", selEmp.value);
      
      $.ajax({
        data:  parametros,
        url:   window.location.origin+window.location.pathname+"actions.php",
        type:  "post",
        contentType: false,
        processData: false,
        beforeSend: function () {
          alLoad("Cargando...")
        },
        success: function (jsonPhp) {
          console.log(jsonPhp);
          const jsonJS = JSON.parse(jsonPhp);

          if(jsonJS.error==true){
            alError(jsonJS);
          } else {
            location.reload(true);
          }
        }
      });
    });
  });


  $('#modalImp').on('hidden.bs.modal', function (e) {
    txtCod.focus();
  });

  // FOCUS PILL
  $('a[data-toggle="pill"]').on('shown.bs.tab', function (e) {
    if (!isMobile()) {
      const target = e.target.attributes.href.value;
      const relatedTarget = e.relatedTarget.attributes.href.value;

      if ((target == "#pills-0" && isNullPer(txtCod.value)) || target != "#pills-0") {
        $(target+' select:visible:first').focus();
      } else {
        (relatedTarget == "#pills-1") ? txtNom.focus() : 0;
      }
    }
  });

</script>