let notyf = new Notyf({
    duration: 5000,
    types: [{
        type: 'info',
        background: '#009ffb',
        icon: false
    }]
});

function generatePassword() {

    let length = 7;
    let charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$#^!*#$+_#%$#$";
    let retVal = "";
    for (let i = 0, n = charset.length; i < length; ++i) {
        retVal += charset.charAt(Math.floor(Math.random() * n));
    }

    length = 1;
    charset = "0123456789";
    for (let i = 0, n = charset.length; i < length; ++i) {
        retVal += charset.charAt(Math.floor(Math.random() * n));
    }

    length = 8;
    charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$#^!*_##$#%$";
    for (let i = 0, n = charset.length; i < length; ++i) {
        retVal += charset.charAt(Math.floor(Math.random() * n));
    }

    return retVal;
}

function copyToClipboard(text) {
    let $temp = $("<input>");
    $("body").append($temp);
    $temp.val(text).select();
    document.execCommand("copy");
    $temp.remove();

    notyf.open({
        type: 'info',
        message: "رمزعبور با موفقیت کپی شد"
    });
    
}


$(function () { // start of document ready:

    $('#random_password').click(function (e) {
        let pass = generatePassword();
        $('#primary_admin_pass').val(pass);
    });

    $('#copy_password').click(function (e) {
        copyToClipboard($('#show_password').text());
        $('#copy_notif').show();
    });


    $('#start_installer_btn').click(function (e) {

        $('#start_installer_btn').hide();

        let _email = $.trim($('#primary_admin_email').val());
        let _password = $.trim($('#primary_admin_pass').val());
        let sendData = {
            name: $.trim($('#primary_admin_name').val()),
            email: _email,
            password: _password,
            packagename: $.trim($('#package_name').val().toLowerCase())
        }

        $.ajax({

            type: "POST",
            url: mpPanelBaseUrl + "/core/_requests/create_primary_admin.php", //we cant use routes here
            data:sendData,
            cache: false,
            timeout: 25000,
            success: function (returndata) {
                
                if (returndata == "ok") {
                    $('#show_email').text(_email);
                    $('#show_password').text(_password);
                    notyf.success("اطلاعات مدیر اصلی سیستم با موفقیت ثبت شد");
                    $('#step1').hide();
                    $('#step2').show();
                    
                } else {
                    $('#start_installer_btn').show();
                    notyf.error(returndata);
                }

            },
            error: function(xhr, status, error) {
                notyf.error('Error: ' + xhr.status + ' ' + xhr.statusText);
            }

        });

    });


    
    $('#enter_login_page').click(function (e) {

        window.location.href = mpPanelBaseUrl;

    });



}); //end of jqury doc ready