
$('#repassword').tooltip('hide');
window.onload = function () {
    document.getElementById("password").onchange = validate;
    document.getElementById("repassword").onchange = validate;
    document.getElementById("email").onchange = validateEmail;
    document.getElementById("username").onchange = validateUsername;
}

//isUsernameExist();
function isUsernameExist(){
    var exist = false;
    var username      = document.getElementById("username").value;
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loadusername',
        async: false,
        data: {
            id  : null,
        },
        success: function(result){
            
            var data = result.data;
            let i = 0;
            for(i; i<data.length; i++){
                if(username === data[i].username){
                    exist = true;
                }
            }
            
        },
        error: function(xhr){
            //alert(xhr.status+'-'+xhr.statusText);
        }
    });
    console.log(exist);
    return exist;
}

function validateUsername(){
    var validUsername = /^[a-z0-9_]{3,16}$/;
    var username      = document.getElementById("username").value;
    var usernameExist = 10;
    var usernameWrong = 20;
    var usernameOk    = 0;
    var code;
    
    //console.log(data[0].username);
    //console.log(validUsername.test(username));
    // console.log(exist);
    // if(!exist == validUsername.test(username)){
    //     return true;
    // } else {
    //     return false;
    // }
    return validUsername.test(username);
}

function validate(){
    var pass2=document.getElementById("repassword").value;
    var pass1=document.getElementById("password").value;
    
    if(pass1!=pass2){
        document.getElementById("repassword").setCustomValidity("Passwords Tidak Sama, Coba Lagi");
        return false;
    }else{
        document.getElementById("repassword").setCustomValidity('');
        return true;
    }
}

//validateEmail();
function validateEmail(){
    var validEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    var email = document.getElementById("email").value;
    //console.log(validEmail.test(email));

    return validEmail.test(email);  
}

function loginDirect(){
    window.location.href = "login";
}

$('#btnRegister').on('click', function(){
    if(validateUsername() == false){
        alert("format username salah");
    }else if(validateEmail() == false){
        alert("format email salah");
    }else if(validate() == false){
        alert("password tidak sesuai");
    }else if(validateUsername() == true && validateEmail() == true && validate() == true){
        if(isUsernameExist()){
            alert("username sudah terpakai");
        }else{
            registrasi();
        }
        
    }
    
    // if(validate() == true){
    //     registrasi();
    //     //window.location.href = baseURL+ "/Useranalytics/template";
    // }else{
    //     alert('password tidak sesuai');
    // }
});


function registrasi(){
    let $username    = $('#username').val();
    let $email       = $('#email').val();
    let $password    = $('#password').val();
    let $repassword  = $('#repassword').val();
    //alert($username+$email+$password+$repassword);

    if($username == null || $username ==""){
        alert("username kosong");
        return false
    }else if($email == null || $email==""){
        alert("email kosong");
        return false
    } else if($password == null || $password=="" || $repassword==null || $repassword == "" ){
        alert("password kosong");
        return false
    }

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/registeruser',
        data: {
            username    : $username,
            email       : $email,
            password    : $password,
        },
        success: function(result){
           // alert("suksess regis")
            if(result.code != 0 ){
                //alert('Daftar berhasil!');
                //console.log('berhasil');
                alert(result.info);
            }else{
                loginDirect();
                $('#username').val("");
                $('#email').val("");
                $('#password').val("");
                $('#repassword').val("");
            }
            
        },
        error: function(xhr){
            //alert(xhr.status+'-'+xhr.statusText);
        }
    });
}

//ajax get api user_data_header

    // $.ajax({
    //     type: 'POST',
    //     dataType: 'json',
    //     url: baseURL + '/api/loadusername',
    //     data: {
            
    //     },
    //     success: function(result){
    //         console.log(result.data);
    //     },
    //     error: function(xhr){
    //         //alert(xhr.status+'-'+xhr.statusText);
    //     }
    // });

    