"use strict";

// Class Definition
var KTLogin = function() {
    var _login;

    var _showForm = function(form) {
        var cls = 'login-' + form + '-on';
        var form = 'kt_login_' + form + '_form';

        _login.removeClass('login-forgot-on');
        _login.removeClass('login-signin-on');
        _login.removeClass('login-signup-on');

        _login.addClass(cls);

        KTUtil.animateClass(KTUtil.getById(form), 'animate__animated animate__backInUp');
    }

    var _handleSignInForm = function() {
        var validation;

        // Init form validation rules. For more info check the FormValidation plugin's official documentation:https://formvalidation.io/
        validation = FormValidation.formValidation(
			KTUtil.getById('kt_login_signin_form'),
			{
				fields: {
					username: {
						validators: {
							notEmpty: {
								message: 'Sila masukkan ID Pengguna'
							}
						}
					},
					password: {
						validators: {
							notEmpty: {
								message: 'Sila masukkan Kata laluan'
							}
						}
					}
				},
				plugins: {
                    trigger: new FormValidation.plugins.Trigger(),
                    submitButton: new FormValidation.plugins.SubmitButton(),
                    //defaultSubmit: new FormValidation.plugins.DefaultSubmit(), // Uncomment this line to enable normal button submit after form validation
					bootstrap: new FormValidation.plugins.Bootstrap()
				}
			}
		);

        $('#kt_login_signin_submit').on('click', function (e) {
            e.preventDefault();

            validation.validate().then(function(status) {
		        if (status == 'Valid') {
					$.ajax({
						type: "POST", 
						url: "login_pemohon-check.php", 
						data: {
							'username' : $('#username').val(),
							'password' : $('#password').val(),
						},
						success: function(res){
							var data = JSON.parse(res);
							if(data.status == 1)
							{
								window.location = data.redirect;
							}
							else 
							{
								swal.fire({
									text: data.errorMsg,
									icon: "error",
									buttonsStyling: false,
									confirmButtonText: "OK!",
									customClass: {
										confirmButton: "btn font-weight-bold btn-light-primary"
									}
								}).then(function() {
									KTUtil.scrollTop();
								});
							}
						} 
					});  //end ajax
                    
				} else {
					swal.fire({
		                text: "Maaf, terdapat kesalahan maklumat yang dimasukkan.",
		                icon: "error",
		                buttonsStyling: false,
		                confirmButtonText: "OK!",
                        customClass: {
    						confirmButton: "btn font-weight-bold btn-light-primary"
    					}
		            }).then(function() {
						KTUtil.scrollTop();
					});
				}
		    });
        });

        // Handle forgot button
        $('#kt_login_forgot').on('click', function (e) {
            e.preventDefault();
            _showForm('forgot');
        });

        // Handle signup
        $('#kt_login_signup').on('click', function (e) {
            e.preventDefault();
            _showForm('signup');
        });
    }

    var _handleSignUpForm = function(e) {
        var validation;
        var form = KTUtil.getById('kt_login_signup_form');

        // Init form validation rules. For more info check the FormValidation plugin's official documentation:https://formvalidation.io/
        validation = FormValidation.formValidation(
			form,
			{
				fields: {
					fullname: {
						validators: {
							notEmpty: {
								message: 'Username is required'
							}
						}
					},
					email: {
                        validators: {
							notEmpty: {
								message: 'Email address is required'
							},
                            emailAddress: {
								message: 'The value is not a valid email address'
							}
						}
					},
                    password: {
                        validators: {
                            notEmpty: {
                                message: 'The password is required'
                            }
                        }
                    },
                    cpassword: {
                        validators: {
                            notEmpty: {
                                message: 'The password confirmation is required'
                            },
                            identical: {
                                compare: function() {
                                    return form.querySelector('[name="password"]').value;
                                },
                                message: 'The password and its confirm are not the same'
                            }
                        }
                    },
                    agree: {
                        validators: {
                            notEmpty: {
                                message: 'You must accept the terms and conditions'
                            }
                        }
                    },
				},
				plugins: {
					trigger: new FormValidation.plugins.Trigger(),
					bootstrap: new FormValidation.plugins.Bootstrap()
				}
			}
		);

        $('#kt_login_signup_submit').on('click', function (e) {
            e.preventDefault();

            validation.validate().then(function(status) {
		        if (status == 'Valid') {
                    swal.fire({
		                text: "Pendaftaran berjaya",
		                icon: "success",
		                buttonsStyling: false,
		                confirmButtonText: "OK!",
                        customClass: {
    						confirmButton: "btn font-weight-bold btn-light-primary"
    					}
		            }).then(function() {
						KTUtil.scrollTop();
					});
				} else {
					swal.fire({
		                text: "Maaf, terdapat kesalahan maklumat yang dimasukkan.",
		                icon: "error",
		                buttonsStyling: false,
		                confirmButtonText: "OK!",
                        customClass: {
    						confirmButton: "btn font-weight-bold btn-light-primary"
    					}
		            }).then(function() {
						KTUtil.scrollTop();
					});
				}
		    });
        });

        // Handle cancel button
        $('#kt_login_signup_cancel').on('click', function (e) {
            e.preventDefault();

            _showForm('signin');
        });
    }

    var _handleForgotForm = function(e) {
        var validation;

        // Init form validation rules. For more info check the FormValidation plugin's official documentation:https://formvalidation.io/
        validation = FormValidation.formValidation(
			KTUtil.getById('kt_login_forgot_form'),
			{
				fields: {
					forget_username: {
						validators: {
							notEmpty: {
								message: 'Sila masukkan ID Pengguna'
							}
						}
					},
					forget_emel: {
						validators: {
							notEmpty: {
								message: 'Sila masukkan alamat Email'
							},
                            emailAddress: {
								message: 'Format email tidak tepat'
							}
						}
					},
					forget_jawapan: {
						validators: {
							notEmpty: {
								message: 'Sila masukkan jawapan rahsia'
							}
						}
					}
				},
				plugins: {
					trigger: new FormValidation.plugins.Trigger(),
					bootstrap: new FormValidation.plugins.Bootstrap(),
					excluded: new FormValidation.plugins.Excluded(),
				}
			}
		);

        // Handle submit button
        $('#kt_login_forgot_submit').on('click', function (e) {
            e.preventDefault();

            validation.validate().then(function(status) {
		        if (status == 'Valid') {
                    // Submit form
					$.ajax({
						type: "POST", 
						url: "login_pemohon-forgetpassword.php", 
						data: {
							'username' : $('#forget_username').val(),
							'criteria' : $('#forget_select').val(),
							'email' : $('#forget_emel').val(),
							'jawapan' : $('#forget_jawapan').val(),
						},
						success: function(res){
							var data = JSON.parse(res);
							if(data.status == 1)
							{
								$('#newpwdDiv').html(data.message);
								swal.fire({
									html: data.message,
									icon: "success",
									buttonsStyling: false,
									confirmButtonText: "OK!",
									customClass: {
										confirmButton: "btn font-weight-bold btn-light-primary"
									}
								}).then(function() {
									//window.location = data.redirect;
								});
								
							}
							else 
							{
								swal.fire({
									text: data.errorMsg,
									icon: "error",
									buttonsStyling: false,
									confirmButtonText: "OK!",
									customClass: {
										confirmButton: "btn font-weight-bold btn-light-primary"
									}
								}).then(function() {
									KTUtil.scrollTop();
								});
							}
						} 
					});  //end ajax
				} else {
					swal.fire({
		                text: "Maaf, terdapat kesalahan maklumat pengguna.",
		                icon: "error",
		                buttonsStyling: false,
		                confirmButtonText: "OK!",
                        customClass: {
    						confirmButton: "btn font-weight-bold btn-light-primary"
    					}
		            }).then(function() {
						KTUtil.scrollTop();
					});
				}
		    });
        });

        // Handle cancel button
        $('#kt_login_forgot_cancel').on('click', function (e) {
            e.preventDefault();

            _showForm('signin');
        });
    }

    // Public Functions
    return {
        // public functions
        init: function() {
            _login = $('#kt_login');

            _handleSignInForm();
            _handleSignUpForm();
            _handleForgotForm();
        }
    };
}();

// Class Initialization
jQuery(document).ready(function() {
    KTLogin.init();
});
