import Ajax from "../ajax.js";
import { alertConfirmationDelete, alertSuccessMessage } from "../alerts/sweetAlert.js";

class MaquinaTareaDatatable {
    constructor(domItem) {
        this.datatableItem = domItem
        this.datatableColumns = [
            { data: 'name', searchable: true, sortable: true },
            { data: 'description', searchable: true, sortable: true },
            { data: 'created_at', searchable: true, sortable: true },
            {
                data: 'action', searchable: false, sortable: false,
                render: (d, t) => {
                    return `<div class="btn-group btn-group-sm">
                    <a href="/configuracion/maquina-tareas/edit/${d}" class="maquina-tarea-edit"><i class="ti ti-eye ti-sm mx-2"></i></a>
                    <a type="button" data-id="${d}" class="maquina-tarea-delete"><i class="ti ti-trash ti-sm mx-2"></i></a>

                </div>`
                }
            }
        ]



    }
    init() {
        this.datatableItem.on('click','.maquina-tarea-delete',this.onDelete.bind(this))
        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: '/configuracion/maquina-tareas/datatable'
        });
    }
    onDelete(event){
        const modelId = $(event.currentTarget).data("id");
        alertConfirmationDelete("¿Estás seguro de realizar esta acción?").then(result => {
            if(result.isConfirmed){
                this.handleDeleteMaquinaTarea(modelId)
            }
        }
        )

    }
    handleDeleteMaquinaTarea(modelId){
        const ajax = new Ajax('/configuracion/maquina-tareas/' + modelId,
            null,
            null,
            (response) => {
                alertSuccessMessage(response.message)
                this.datatable.ajax.reload()
            },
            null
        )
        ajax.delete()
    
    }


}

export default MaquinaTareaDatatable;