
class MaquinistaTareaList {
    constructor(domItem) {
        this.item = domItem
        this.maquinaId = this.item.data("id")
        this.datatableItem = $("#maquinista-tarea-table")
        this.datatableTareaAplazadaItem = this.item.find('#maquinista-tarea-aplazada-table')
        this.wrapperDatatableTareaAplazada = this.item.find('#tareas-aplazadas')
        this.btnTareasHoy = $("#btn-tareas-hoy")
        this.todayDate = $('#today-date')
        this.btnAllTareas = $("#btn-all-tareas")
        this.datatableColumns = [
            { data: 'otId', searchable: false, sortable: false },
            { data: 'tareaName', searchable: false, sortable: false },
            { data: 'tareaEstado', searchable: false, sortable: false, render: this.renderStado.bind(this) },
            // { data: 'presupuesto_titulo', searchable: false, sortable: false },
            // { data: 'papel_impresion', searchable: false, sortable: false },
            // { data: 'papel_gramaje', searchable: false, sortable: false },
            { data: 'fecha_impresion', searchable: false, sortable: false },
            { data: 'action', searchable: false, sortable: false, width: "20rem" },
        ]
        this.urlAll = '/produccion/ordentrabajo/maquinista/maquinas/tareas/datatable/all/' + this.maquinaId
        this.urlAplazada = '/produccion/ordentrabajo/maquinista/maquinas/tareas/aplazadas/datatable/' + this.maquinaId
        this.urlToday = '/produccion/ordentrabajo/maquinista/maquinas/tareas/datatable/today/' + this.maquinaId
        this.initTable()
        this.estadoClass = {
            I: 'primary',
            P: 'warning',
            S: 'warning',
            D: 'danger',
            F: 'success'
        }
        this.estadoNames = {
            I: 'Iniciada',
            P: 'Pendiente',
            S: 'Pausada',
            D: 'Aplazada',
            F: 'Finalizada'
        }

    }
    init() {
        this.btnTareasHoy.on('click', this.loadToday.bind(this))
        this.btnAllTareas.on('click', this.loadAll.bind(this))
    }
    initTable() {

        this.datatable = this.datatableItem.DataTable({
            processing: true,
            layout: {
                topStart: 'pageLength',
                topEnd: 'search',
                bottomStart: 'info',
                bottomEnd: 'paging'
            },
            serverSide: true,
            pageLength: 100,
            language: {
                url: "/themes/vuexy/vendor/libs/datatables-sk/plugins/i18n/es-ES.json"
            },
            columns: this.datatableColumns,
            ajax: this.urlToday
        });
        this.datatableAplazada = this.datatableTareaAplazadaItem.DataTable({
            processing: true,
            layout: {
                bottomStart: 'info',
                bottomEnd: 'paging'
            },
            serverSide: true,
            pageLength: 100,
            language: {
                url: "/themes/vuexy/vendor/libs/datatables-sk/plugins/i18n/es-ES.json"
            },
            columns: this.datatableColumns,
            ajax: this.urlAplazada
        });
        this.datatableAplazada.on('draw.dt', this.handleShowTareasAplazadas.bind(this))
    }
    loadToday() {
        this.btnTareasHoy.removeClass('active')
        this.btnTareasHoy.removeAttr('aria-pressed')
        this.btnAllTareas.removeClass('active')
        this.btnAllTareas.removeAttr('aria-pressed')
        this.todayDate.removeClass('d-none')
        this.btnTareasHoy.addClass('active')
        this.btnTareasHoy.attr('aria-pressed', true)
        this.datatable.ajax.url(this.urlToday)
        this.datatable.ajax.reload()


    }
    loadAll() {
        this.btnTareasHoy.removeClass('active')
        this.btnTareasHoy.removeAttr('aria-pressed')
        this.todayDate.addClass('d-none')
        this.btnAllTareas.addClass('active')
        this.btnAllTareas.attr('aria-pressed', true)
        this.datatable.ajax.url(this.urlAll)
        this.datatable.ajax.reload()

    }
    renderStado(d) {

        return `<span class="badge" style="background-color:${d.color};color:white">${d.title}</span>`
    }
    handleShowTareasAplazadas() {
        let totalTareasAplazadas = this.datatableAplazada.page.info().recordsTotal
        console.log(totalTareasAplazadas)
        if (totalTareasAplazadas > 0) {
            this.wrapperDatatableTareaAplazada.removeClass('d-none')
        } else {
            this.wrapperDatatableTareaAplazada.addClass('d-none')
        }
    }

}

export default MaquinistaTareaList;