/**
 *
 * You can write your JS code here, DO NOT touch the default style file
 * because it will make it harder for you to update.
 *
 */

"use strict";

function showMsg(message , type , position = 'center' , gravity = 'top'){
    Toastify({
        text: message,
        className: type,
        duration: 3000,
        // close: true,
        gravity:  gravity, // `top` or `bottom`
        position: position, // `left`, `center` or `right`
        style: {
            background: type === 'success' ? "linear-gradient(to right, #00b09b, #96c93d)" : "linear-gradient(to right, red, brown)",
        }
    }).showToast();
}



const showPopupMsg = (heading, message, type, url = '', newTab = true,btnText="Ok") => {
	if (url != '') {

		Swal.fire({
			title: heading,
			html: message,
			icon: type,
			showCancelButton: true,
			showConfirmButton: url != '' ? true : false,
			focusConfirm: url != '' ? true : false,
			// confirmButtonColor: "green",
			cancelButtonColor: "#800808",
			confirmButtonText: btnText,
			cancelButtonText: "Close",
			confirmButton: 'confirm-button-class',
			showCloseButton: true,
			width: 600,
			// padding: '3em',
			// color: '#716add',
			// background: '#fff url(/images/trees.png)',
			backdrop: `
			  rgba(0,0,123,0.4)
			  no-repeat
			`,
			customClass: {
				confirmButton: 'btn-success',
			}
		}).then((result) => {
			// console.log(result)
			if (url != '' && result.isConfirmed == true) {
				if (newTab == true) {
					var win = window.open(url, '_blank');
					if (win) {
						win.focus();
					} else {
						alert('Please allow popup for this side');
						return false;
					}
				} else {
					window.location.href = url;
				}
			}
			// setTimeout(() => {
			// 	location.reload()
			// }, 1500);
		})

	} else {
		Swal.fire({
			title: heading,
			html: message,
			icon: type,
		})
	}
}


function blockForm(status, btnText = 'Submit', form = null) {
	pageLoader(status == true ? 'show' : 'hide');
	changeButtonStatus(status == true ? 'disable' : 'enable' , btnText);

	// if(form !== null){
	// 	Array.from(form.elements).forEach((field) => (field.disabled = status));
	// }
}

function pageLoader(type){
    if(type == 'show'){
        document.getElementById('divLoading').classList.add('show');
    } else {
        document.getElementById('divLoading').classList.remove('show');
    }
}

function changeButtonStatus(status , text = "Submit"){
	let btn_id = 'submit'
	if(text ==  'Login'){
		btn_id = 'login-btn'
	}
	const button = document.querySelector(`#${btn_id}`);
    if(status == 'disable'){
        button.disabled = true;
        button.innerHTML = '<span class="spinner-border spinner-border-sm mr-2" role="status"></span> Please Wait..';
    } else{
        button.disabled = false;
        button.innerHTML = text;
    }
}

async function user_confirmation() {
	return new Promise((resolve, reject) => {
		Swal.fire({
			title: "Are you sure ?",
			icon: "warning",
			showCancelButton: true,
			confirmButtonColor: "#1aaa0e",
			confirmButtonText: "Yes, Continue!",
			cancelButtonText: "No, Cancel!",
			closeOnConfirm: false,
			closeOnCancel: true,
		}).then((result) => {
			if (result.isConfirmed) {
				resolve(true)
			} else {
				reject("Permission Denied")
			}
		})
	})
}

function sendAjaxRequest(action, formData, Method = 'POST') {
	return $.ajax(action, {
		method: Method,
		data: formData,
		// dataType: 'json',
		contentType: false,
		cache: false,
		processData: false,
	});
}

function deleteRecord(action, data) {
	if (action == '' || data == '') {
		return false;
	}
	Swal.fire({
		title: "Are you sure?",
		text: "You will not be able to recover this data!",
		icon: "warning",
		showCancelButton: true,
		confirmButtonColor: "#c61818",
		confirmButtonText: "Yes, delete it!",
		cancelButtonText: "No, cancel!",
		closeOnConfirm: false,
		closeOnCancel: true,
	}).then((result) => {
		if (result.isConfirmed) {
			pageLoader('show')

            $.ajax({
                url:action,
                type: 'DELETE',
                data: data,
                success: function(response) { //console.log(response);
			        pageLoader('hide')
                    if (response.status == 200) {
                        $("#row_" + data.id + "").hide();
                        showMsg(response.message,'success');
                    } else {
                        showMsg(response.message,'error');
                    }
                },
                error: function(xhr) {
			        pageLoader('hide')
                    showMsg("Something went wrong.",'error');
                }
            });
		}
	})
}