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

export default function () {
	const {utils, Tea} = initCtx(this)

	this.frontPreviewURL = ""
	this.isSubmitting = false

	this.realName = ""
	this.number = ""

	if (this.identity != null) {
		this.realName = this.identity.realName
		this.number = this.identity.number
	}
	this.canEdit = (this.identity == null || this.identity.status == "none" || this.identity.status == "rejected")

	this.selectFile = function (targetId) {
		let selector = document.getElementById(targetId)
		selector.click()
	}

	this.changeFrontFile = function (e) {
		let files = e.target.files
		if (files.length > 0) {
			this.frontPreviewURL = URL.createObjectURL(files[0])
		}
	}

	this.beforeSubmit = function () {
		this.isSubmitting = true
	}

	this.doneSubmit = function () {
		this.isSubmitting = false
	}

	this.success = function () {
		utils.success("保存成功", function () {
			utils.reload()
		})
	}

	this.submit = function () {
		if (this.identity == null) {
			utils.fail("请先保存后再提交审核")
			return
		}
		this.$post(".submit")
			.params({
				identityId: this.identity.id
			})
			.success(function () {
				utils.success("提交成功", function () {
					utils.reload()
				})
			})
	}

	this.cancel = function () {
		if (this.identity == null) {
			utils.fail("请先保存后再提交审核")
			return
		}
		utils.confirm("确定要取消提交审核吗？", function () {
			this.$post(".cancel")
				.params({
					identityId: this.identity.id
				})
				.success(function () {
					utils.success("取消成功", function () {
						utils.reload()
					})
				})
		})
	}
}