import Ajax from '../../../components/ajax.js';
import ConfirmDeleteModal from '../../../components/ConfirmDeleteModal.js';

class listServiciosAcabado {

    constructor() {

        this.deleteModal = null;
    }

    init() {
        const self = this;

        this.table = $('#tableOfServiciosAcabado');

        this.datatableColumns = [
            { data: 'id', searchable: true, sortable: true, width: "10%" },
            { data: 'nombre', searchable: true, sortable: true, width: "40%" },
            { data: 'acabado_cubierta', sortable: true, width: "10%", render: this.#renderBoolCell.bind(this) },
            { data: 'acabado_sobrecubierta', sortable: false, width: "10%", render: this.#renderBoolCell.bind(this) },
            { data: 'mostrar_en_presupuesto_cliente', searchable: false, sortable: true, width: "1exit0%", render: this.#renderBoolCell.bind(this) },
            { data: 'user_updated', searchable: true, sortable: true },
            { data: 'updated_at', searchable: false, sortable: true },
            {
                data: 'action', searchable: false, sortable: false, width: "10%"
            },
        ]

        this.initDatatableTareas();

        this.deleteModal = new ConfirmDeleteModal('tarifascliente');
        this.deleteModal.init();

        this.table.on('click', '.btn-delete', function (e) {

            self.deleteModal.setData($(this).attr('data-id'));
            self.deleteModal.show(() => {
                if ($.isNumeric(self.deleteModal.getData())) {
                    new Ajax(
                        '/serviciosacabado/delete/' + self.deleteModal.getData(),
                        {},
                        {},
                        (response) => {
                            if (response.id) {
                                self.datatableTareas.ajax.reload();
                                popSuccessAlert(response.msg);
                            }
                        }
                    ).get();
                }
            });
        });
        this.table.on('click', '.btn-edit', function (e) {
            window.location.href = '/serviciosacabado/edit/' + $(this).attr('data-id');
        });
    }

    initDatatableTareas() {

        this.datatableTareas = this.table.DataTable({
            processing: true,
            //paging: true,
            layout: {
                topStart: 'pageLength',
                topEnd: 'search',
                bottomStart: 'info',
                bottomEnd: 'paging'
            },
            serverSide: true,
            responsive: true,
            pageLength: 25,
            language: {
                url: "/themes/vuexy/vendor/libs/datatables-sk/plugins/i18n/es-ES.json"
            },
            columns: this.datatableColumns,
            ajax: '/serviciosacabado/datatable'
        });
    }


    #renderBoolCell(d, t) {

        let cell = d == 1 ? '<i class="ti ti-check"></i>' : '';
        return cell;
    }
}



$(function () {
    new listServiciosAcabado().init();
})

export default listServiciosAcabado;