import ClassSelect from "../../components/select2.js";
import Ajax from "../../components/ajax.js";
class TarifaMaquina {
    constructor(domItem, type = "acabado") {
        this.item = domItem
        this.type = type
        this.tarifaId = this.item.data("id")
        this.btnNewTarifaMaquina = this.item.find("#btn-new-tarifa-maquina")
        this.selectTarifaMaquina = new ClassSelect(this.item.find("#select-tarifa-maquina"), `/tarifas/maquinas/${this.type}/select`, "Seleccione una maquina", true);
        this.selectMaquinaTarea = new ClassSelect(this.item.find("#select-maquina-tarea"), `/tarifas/maquinas/tareas/select`, "Seleccione una tarea", true);
        this.datatableItem = this.item.find("#table-tarifa-maquinas")
        this.datatableColumns = [
            { data: 'maquinaNombre', searchable: false, sortable: false },
            { data: 'tareaNombre', searchable: false, sortable: false },
            {
                data: 'action', searchable: false, sortable: false,
                render: (d, t) => {
                    return `<div class="btn-group btn-group-xs">
                    <a type="button" class="btn btn-xs tarifa-maquina-btn-delete" data-id="${d}"><i class="ti ti-trash ti-sm mx-2"></i></a>
                </div>`
                }
            }
        ]

    }
    init() {
        if (this.tarifaId) {

            // this.btnNewTarifaMaquina.prop("disabled",true)
            this.selectMaquinaTarea.init()
            this.selectTarifaMaquina.init()
            this.datatable = this.datatableItem.DataTable({
                processing: true,
                layout: {
                    topStart: 'pageLength',
                    topEnd: 'search',
                    bottomStart: 'info',
                    bottomEnd: 'paging'
                },
                serverSide: true,
                pageLength: 25,
                language: {
                    url: "/themes/vuexy/vendor/libs/datatables-sk/plugins/i18n/es-ES.json"
                },
                columns: this.datatableColumns,
                ajax: `/tarifas/maquinas/${this.type}/datatable/${this.tarifaId}`
            });
            this.events();
        }
    }
    events() {
        this.btnNewTarifaMaquina.on("click", this.handleNewTarifaMaquina.bind(this));
        this.datatable.on("click", ".tarifa-maquina-btn-delete", this.handleDeleteTarifaMaquina.bind(this))
    }
    handleNewTarifaMaquina() {
        let key = `tarifa_${this.type}_id`
        let bodyData = {
            maquina_id: this.selectTarifaMaquina.getVal(),
            maquina_tarea_id: this.selectMaquinaTarea.getVal()
        }
        bodyData[key] = this.tarifaId;

        const ajax = new Ajax(
            `/tarifas/maquinas/${this.type}`,
            bodyData,
            null,
            this.handleNewTarifaMaquinaSuccess.bind(this),
            this.handleNewTarifaMaquinaError.bind(this),
        )
        ajax.post()
    }
    handleNewTarifaMaquinaSuccess(response) {
        this.datatable.ajax.reload()
        this.selectMaquinaTarea.reset()
        this.selectTarifaMaquina.reset()
    }
    handleNewTarifaMaquinaError(error) { }

    handleDeleteTarifaMaquina(event) {
        const tarifaMaquinaId = $(event.currentTarget).data("id")
        const ajax = new Ajax(
            `/tarifas/maquinas/${this.type}/${tarifaMaquinaId}`,
            null,
            null,
            this.handleDeleteTarifaMaquinaSuccess.bind(this),
            this.handleDeleteTarifaMaquinaError.bind(this),
        )
        ajax.delete()
    }
    handleDeleteTarifaMaquinaSuccess() {
        this.datatable.ajax.reload()
    }
    handleDeleteTarifaMaquinaError() { }

}
export default TarifaMaquina