lotesFicha.js 5.47 KB
Newer Older
Luis Gangas committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
/* global Ladda, Promise */

const helpers = {
  ajaxRequest(obj) {
    return $.ajax({
      url: obj.url,
      type: obj.type,
      dataType: obj.dataType,
      data: obj.body,
      beforeSend: (xhr, settings) => {
        load(obj.loadingMessage)
      }, success: (response, textStatus, jqXHR) => {
        unload()
        return Promise.resolve(response)
      }, error: (jqXHR, textStatus, errorThrown) => {
        return Promise.reject({
          status: textStatus,
          message: `Error making the request`,
          request: obj
        })
      }
    })
  }
}
const lotes = {
  listar() {
    defaultConfigDataTable()
    let search = document.querySelector('#txtNumeroLote').value
    return new Promise((resolve) => {
      $(document.querySelector('#tblLotes')).DataTable().destroy()
      $(document.querySelector('#tblLotes')).DataTable({
        processing: true,
        serverSide: true,
        ajax: {
          url: '../LoteFichaServlet',
          dataType: 'json',
          type: 'post',
          data: {
            accion: 'listarLoteDT',
            search: search
          }
        },
        columnDefs: [
          {targets: 0, orderable: false, className: 'text-center'}, // #
          {targets: 1, orderable: false, className: 'text-center'}, // num lote
          {targets: 2, orderable: false, className: 'text-center'}, // fecha registro
          {targets: 3, orderable: false, className: 'text-center'}, // tipo lote
          {targets: 4, orderable: false, className: 'text-center'}, // N° fichas
          {targets: 5, orderable: false, className: 'text-center'}, // estado Lote
          {targets: 6, orderable: false, className: 'text-center'} // acciones
        ],
        columns: [
          {
            data: 'item'
          },
          {
            data: 'numeroLote'
          },
          {
            data: 'fechaRegistro'
          },
          {
            data: "tipoLote",
            className: 'text-center',
            render: (data, type, row) => {
              return data === 'A' ? `<span class="label bg-primary">ADMINISTRATIVO</span>` : `<span class="label bg-success">DOCENTE</span>`
            }
          },
          {
            data: 'numeroFichas',
            render: function (data) {
              return `<span style="font-size:18px;" class="text-bold">${data}</span>`
            }
          },
          {
            data: 'estilo'
          },
          {
            data: null,
            render: function (data) {
              return `<ul class="icons-list">
                        <li class="text-orange">
                            <a href="#" onclick="javascript: return false" class="verDetalleLote" data-toggle="tooltip" data-placement="top" title="Ver detalle lote"><i style="font-size:22px;" class="fa fa-folder-open fa-lg"></i></a>
                        </li>
                      </ul>
                      `
            }
          }
        ],
        fnInitComplete: function (settings, data) {
          $('.verDetalleLote').tooltip({
            template: '<div class="tooltip"><div class="bg-slate-800"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div></div>'
          })
          resolve(data)
        }
      })
    })
  }
}
const formValidation = {
  init() {
    this.rules()
    this.formChange()
  },
  rules() {
    $(document.querySelector('#txtNumeroLote')).rules('add', {
      required: true,
      number: true
    })
  },
  formChange() {
    document.querySelector('#formFiltroLotes').addEventListener('change', (e) => {
      $(e.currentTarget).valid()
    })
  }
}
const DOMComponents = {
  init() {
    this.initializePluginComponents()
    this.configComponents()
  },
  initializePluginComponents() {
    Ladda.bind('#btnLimpiarFiltroLotes', {
      callback: (instance) => {
        let progress = 0
        let interval = setInterval(() => {
          progress = Math.min(progress + Math.random(), 2)
          instance.setProgress(progress)
          if (progress === 2) {
            instance.stop()
            clearInterval(interval)
            document.querySelector('#txtNumeroLote').value = ''
            lotes.listar()
          }
        }, 200)
      }
    })
  },
  configComponents() {
    new Input({el: '#txtNumeroLote', param: 'numbers'}).validate()
  }
}
const DOMEvents = () => {
  $(document.querySelector('#formFiltroLotes')).validate({
    submitHandler: (form, e) => {
      lotes.listar()
      return false
    }
  })

  $('#tblLotes tbody').on('click', '.verDetalleLote', (e) => {
    let data = $(document.querySelector('#tblLotes')).DataTable().row($(e.currentTarget).parents('tr')).data()
    httpRequest.obtenerDetalleLote({id: data.codigoFichaLote, tipoLote: data.tipoLote})
      .then((obj) => {
        data.detalle = obj.data.fichas
        localStorage.setItem('lote', JSON.stringify(data))
        window.location = "detalleLoteDocente.jsp"
      })

  })
}
const httpRequest = {
  obtenerDetalleLote(obj) {
    let accion = ''
    if (obj.tipoLote === 'D') {
      accion = 'listarDetalleLoteFichaDocenteDT'
    } else if (obj.tipoLote === 'A') {
      accion = 'listarDetalleLoteFichaAdministrativoDT'
    } else {
      accion = ''
    }
    return helpers.ajaxRequest({
      url: '../DetalleLoteFichaDocenteServlet',
      dataType: 'json',
      type: 'POST',
      body: {
        codigoFichaLote: obj.id,
        accion: accion
      },
      loadingMessage: 'Obteniendo detalle'
    })
  }
}

lotes.listar()
DOMEvents()
DOMComponents.init()
formValidation.init()
removeLocal('lote')