$("#btnSave").on('click', function(e){
    e.preventDefault();
    let box_id = $('#box_id').val();
    url = parseInt(box_id) === 0 ? save_url : update_url;
    $.ajax({
        url: url,
        type: "POST",
        data: {
            "_token": csrf_token,
            box_id: box_id,
            rack_id: $('#rack_id').val(),
            code: $('#code').val(),
            name: $('#name').val(),
            rows: $('#rows').val(),
            columns: $('#columns').val(),
            capacity: $('#capacity').val(),
        },
        success: function(res){
            handleMessage(res.success, res.message)
            if(res.success){ setTimeout(function () {location.reload()}, messageDuration)}
        }
    });
});

function delete_lab(box_id){
    Swal.fire({
        text: confirm_delete_record,
        icon: 'question',
        showCancelButton: true,
        cancelButtonClass: 'danger',
        confirmButtonText: confirm_ok_delete,
        cancelButtonText: confirm_cancel
    }).then((result) => {
        if (result.value) {
            $.ajax({
                url: delete_url,
                method: 'POST',
                data: {
                    "_token": csrf_token,
                    box_id: box_id,
                },
                success: function(res){
                    handleMessage(res.success, res.message)
                    if(res.success) { setTimeout(function () {location.reload()}, messageDuration) }
                }
            })
        }
    });
}

function restore_lab(box_id){
    Swal.fire({
        text: confirm_restore_record,
        icon: 'question',
        showCancelButton: true,
        cancelButtonClass: 'danger',
        confirmButtonText: confirm_ok_restore,
        cancelButtonText: confirm_cancel_restore,
    }).then((result) => {
        if (result.value) {
            $.ajax({
                url: restore_url,
                method: 'POST',
                data: {
                    "_token": csrf_token,
                    box_id: box_id,
                },
                success: function(res){
                    handleMessage(res.success, res.message)
                    if(res.success) { setTimeout(function () {location.reload()}, messageDuration)}
                }
            })
        }
    });
}

$('#modal-box').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget)
    var action = button.data('action')
    var modal = $(this);
    if(action === "new"){
        modal.find('.modal-body input').val("");
        modal.find('.modal-body select[name="rack_id"]').val("").trigger('change');
        modal.find('.modal-body input[name="box_id"]').val(0);
        modal.find('.modal-title').html(modal_add_title);
    }else if(action === "edit"){
        box_id = button.data('box_id');
        modal.find('.modal-body input[name="box_id"]').val(box_id);
        modal.find('.modal-title').html(modal_edit_title);
        $.ajax({
            url: get_url,
            type: "POST",
            data: {
                "_token": csrf_token,
                box_id: box_id,
            },
            success: function(res){
                if(res.success) {
                    let data = res.data;
                    modal.find('.modal-body input[name="code"]').val(data.code);
                    modal.find('.modal-body input[name="name"]').val(data.name);
                    modal.find('.modal-body input[name="rows"]').val(data.rows);
                    modal.find('.modal-body input[name="columns"]').val(data.columns);
                    modal.find('.modal-body input[name="capacity"]').val(data.capacity);
                    modal.find('.modal-body select[name="rack_id"]').val(data.rack_id).trigger('change');
                } else{
                    handleMessage(res.success, res.message)
                }
            },
        });
    }
})
