
$("#btnSave").on('click',function(e){
    e.preventDefault();
    data_id = $('#data-id').val();
    let url = parseInt(data_id) === 0 ? save_url : update_url;
    $.ajax({
        url: url,
        type:"POST",
        data :{
            "_token": csrf_token,
            uid : data_id,
            name: $('#name').val(),
            email: $('#email').val(),
            is_manager: isManager,
            role_id: $('#role-id').val(),
            password: $('#password').val(),
            default_manager: $('#default-manager').val(),
            signature: $('#signature').val(),
            sample_source_id: ($('#role-id').val() == 6 ? $('#sample-source-id').val():0)
        },
        success:function(res){
            handleMessage(res.success, res.message)
            if(res.success) setTimeout(function () {location.reload()}, messageDuration)
        },
    });
});

function delete_lab(uid){
    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,
                    uid : uid,
                },
                success:function(res){
                    handleMessage(res.success, res.message)
                    if(res.success) setTimeout(function () {location.reload()}, messageDuration)
                }
            })

        }
    });
}

function restore_lab(uid){
    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,
                    uid : uid,
                },
                success:function(res){
                    handleMessage(res.success, res.message)
                    if(res.success) setTimeout(function () {location.reload()}, messageDuration)
                }
            })

        }
    });
}


$('#modal-user').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget) // Button that triggered the modal
    var action = button.data('action') // Extract info from data-* attributes
    var modal = $(this);
    if(action === "new"){
        // Clear inputs
        modal.find('.modal-body input').val("");
        modal.find('.modal-body input[name="uid"]').val(0);
        modal.find('.modal-title').html(modal_add_title);
    }else if(action === "edit"){
        uid = button.data('uid');
        modal.find('.modal-body input[name="uid"]').val(uid);
        modal.find('.modal-title').html(modal_edit_title);
        $.ajax({
            url: get_url,
            type:"POST",
            data:{
                "_token": csrf_token,
                uid: uid,
            },
            success:function(res){
                let data = res.data;
                console.log();
                modal.find('.modal-body input[name="name"]').val(data.name);
                modal.find('.modal-body input[name="email"]').val(data.email);
                modal.find('.modal-body input[name="password"]').val('***************');
                modal.find('.modal-body input[name="is_manager"]').prop( "checked", data.is_manager);
                modal.find('.modal-body select[name="role_id"]').val(data.role_id).trigger('change');
                isManager = data.is_manager
                if(data.role_id==6) {
                    $('#physician-id').show();
                    $('#sample-source-id').val(data.sample_source_id).trigger('change')
                } else{
                    $('#physician-id').hide();
                    $('#sample-source-id').val(0).trigger('change')
                }
                if(!res.success) handleMessage(res.success, res.message)
            }
        });
    }
})

$('#modal-user-labs').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget) // Button that triggered the modal
    var action = button.data('action') // Extract info from data-* attributes
    var modal = $(this);
    if(action === "assign_lab"){
        uid = button.data('uid');
        modal.find('.modal-body input[name="uid"]').val(uid);
        $.ajax({
            url: get_url,
            type:"POST",
            data:{
                "_token": csrf_token,
                uid: uid,
            },
            success:function(res){
                let data = res.data;
                lab_ids = []
                if(data.lab_covers.length) {
                    $.each(data.lab_covers, function (key, value) {
                        lab_ids[key] = value.lab_id
                    });
                }
                else{
                    lab_ids[0] = base_lab_id
                }
                modal.find('.modal-body select[name="lab_ids"]').val(lab_ids).trigger('change');
                modal.find('.modal-body input[name="email"]').val(data.email);

            },
            error: function(response) {
            },
        });
    }
})

$("#btnAssignLab").on('click',function(e){
    e.preventDefault();
    data_id = $('#data-user-id').val();
    $.ajax({
        url: assignLabUrl,
        type:"POST",
        data :{
            "_token": csrf_token,
            uid : data_id,
            lab_ids: $('#lab_ids').val(),
        },
        success:function(res){
            handleMessage(res.success, res.message)
            if(res.success) setTimeout(function () {location.reload()}, messageDuration)
        }
    });
});




$('#reset-password-modal').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget) // Button that triggered the modal
    var modal = $(this);
    uid = button.data('uid');
    modal.find('.modal-body input[name="uid"]').val(uid);
})


$("#btn-reset-password").on('click',function(e){
    e.preventDefault();
    $.ajax({
        url: reset_password_url,
        type:"POST",
        data :{
            "_token": csrf_token,
            uid : $('#user-id-to-reset-pwd').val(),
            password: $('#reset-new-password').val()
        },
        success:function(res){
            handleMessage(res.success, res.message)
            if(res.success) setTimeout(function () {location.reload()}, messageDuration)
        },
    });
});
