<?php
  include('fnc-cajas.php');

  $sqlAdmin = ($_SESSION['CurrentAdmin'] == 0)?" AND UB.ID_USUARIO = ".$_SESSION['CurrentIdUser']:"";

  $sqlVistaCajas = getVistaCajas($DBSegu);
  $sql = "SELECT DISTINCT B.BASE, B.DESCRI AS DESC_BASE, T.TIPO_CAJA, T.descri as DESC_CAJA
          FROM (SELECT XB.BASE, XB.DESCRI 
               FROM BASES XB 
                    INNER JOIN USUARIOSBASES XUB 
                        INNER JOIN USUARIOS XU ON XUB.ID_USUARIO = XU.ID_USUARIO AND XU.USUARIO = 'PIXO_WEB' 
                    ON XB.BASE = XUB.BASE) B  
                INNER JOIN ".$sqlVistaCajas." T  ON B.BASE = T.BASE
                INNER JOIN USUARIOSBASES UB 
                    INNER JOIN USUARIOS U ON UB.ID_USUARIO = U.ID_USUARIO 
                ON B.BASE = UB.BASE ".$sqlAdmin."
          ORDER BY B.DESCRI, T.DESCRI ";
    $RsCajas = $DBSegu->Execute($sql);
    if (!$RsCajas) {
      print $DBSegu->ErrorMsg();
    } 
    if ($RsCajas->RecordCount() < 50) {
?>
                <div class="box box-light">
                  <div class="box-header with-border">
                    <h3 class="titulos-gris box-title">Cajas</h3>
                    <div class="box-tools pull-right">
                      <div class="btn-group">
                        <button type="button" class="btn btn-default" id="btnTodosCajas"><i class="fa fa-plus-square"></i><i class="fa fa-plus-square"></i></button>
                      </div>
                      <button type="button" class="btn btn-default" id='btnSeleccionarCajas' title="Seleccionar Cajas"><i class="fa fa-check-square"></i></button>
                      <button type="button" class="btn btn-default" id='btnLimpiarCajas' title="Eliminar Todas"><i class="fa fa-eraser"></i></button>
                      <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
                    </div>
                  </div>
                  <div class="box-body no-padding" style="">
                    <div class="zero col-sm-12">
                      <div style="margin-top: 15px;" class="form-group">
                        <select data-clear='1' data-tipo='0' id='listaCajas' class="form-control select2 select2-hidden-accessible" multiple="" data-placeholder="Introduzca Caja" style="width: 100%;" tabindex="-1" aria-hidden="true">
<?php
        while (!$RsCajas->EOF) {
            echo "<option value='".$RsCajas->fields['BASE'].'-'.$RsCajas->fields['TIPO_CAJA']."' data-base='".$RsCajas->fields['BASE']."' data-titular='".$RsCajas->fields['TIPO_CAJA']."'>".utf8_encode($RsCajas->fields['DESC_BASE'] . ' - ' . $RsCajas->fields['DESC_CAJA']) ."</option>";
            $RsCajas->MoveNext();
        }
 ?>
                        </select>
                      </div>
                    </div>
                  </div>
                </div>

<?php
    }
    else {
?>
                <div class="box box-light">
                  <div class="box-header with-border">
                    <h3 class="titulos-gris box-title">Cajas</h3>
                    <div class="box-tools pull-right">
                      <div class="btn-group" style="display: none;">
                        <button type="button" class="btn btn-default" id="btnTodosCajas"><i class="fa fa-plus-square"></i><i class="fa fa-plus-square"></i></button>
                      </div>
                      <button type="button" class="btn btn-default" id='btnSeleccionarCajas' title="Seleccionar Cajas"><i class="fa fa-check-square"></i></button>
                      <button type="button" class="btn btn-default" id='btnLimpiarCajas' title="Eliminar Todas"><i class="fa fa-eraser"></i></button>
                      <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
                    </div>
                  </div>
                  <div class="box-body no-padding" style="">
                    <div class="zero col-sm-12">
                      <div style="margin-right: 10px; margin-top: 15px; margin-left: 10px;" class="form-group">
                        <input data-clear='2' data-tipo='1' placeholder="Introduzca Caja"  type="text" value="" id="listaCajas" data-role="tagsinput" />
                      </div>
                    </div>
                  </div>
                </div>

<?php
    }
?>

    <span id="conteSelCajas"></span>
