import initCtx from "@/utils/ctx.js";
import md5 from "md5/md5.js"

export default function () {
	const {} = initCtx(this)

	this.username = "";
	this.password = "";
	this.passwordMd5 = "";
	this.encodedFrom = window.encodeURIComponent(this.from);

	if (this.isDemo) {
		this.username = "user"
		this.password = "123456"
	}

	this.showOTP = false

	this.isSubmitting = false

	this.$delay(function () {
		this.changePassword();
	})

	this.usernamePlaceholder = "请输入用户名"
	var loginMethods = []
	if (this.emailCanLogin) {
		loginMethods.push("邮箱")
	}
	if (this.mobileCanLogin) {
		loginMethods.push("手机号码")
	}
	if (loginMethods.length > 0) {
		this.usernamePlaceholder += "/" + loginMethods.join("/")
	}

	this.changeUsername = function () {

	}

	this.changePassword = function () {
		this.passwordMd5 = md5(this.password.trim());
	}

	// 更多选项
	this.moreOptionsVisible = false;
	this.showMoreOptions = function () {
		this.moreOptionsVisible = !this.moreOptionsVisible;
	}

	this.submitBefore = function () {
		this.isSubmitting = true;
	}

	this.submitDone = function () {
		this.isSubmitting = false;
	}

	this.submitSuccess = function (resp) {
		// store information to local
		localStorage.setItem("sid", resp.data.localSid)
		localStorage.setItem("ip", resp.data.ip)

		// redirect back
		this.$delay(function () {
			if (resp.data.requireOTP) {
				window.location = "/login/otp?sid=" + resp.data.sid + "&remember=" + (resp.data.remember ? 1 : 0) + "&from=" + window.encodeURIComponent(this.from)
				return
			}
			if (this.from.length == 0) {
				window.location = "/dashboard";
			} else {
				window.location = this.from;
			}
		})
	}
}