<template>
	<view class="content">
		<view class="one">
			{{basicInfo.name}}
		</view>
		<view class="two">
			<uni-forms ref="form" :model="form" :rules="rules" label-width="auto">
				<uni-forms-item name="account">
					<uni-easyinput v-model="form.account" placeholder="请输入登录账号" />
				</uni-forms-item>
				<uni-forms-item name="password">
					<uni-easyinput type="password" v-model="form.password" placeholder="请输入登录密码" />
				</uni-forms-item>
				<uni-forms-item name="phone">
					<uni-easyinput v-model="form.phone" placeholder="请输入联系电话" />
				</uni-forms-item>
				<uni-forms-item name="nick_name">
					<uni-easyinput v-model="form.nick_name" placeholder="请输入账号昵称" />
				</uni-forms-item>
				<uni-forms-item name="gender">
					<uni-data-checkbox v-model="form.gender" :localdata="genderList"
						:map="{text:'label',value:'value'}" />
				</uni-forms-item>
				<uni-forms-item name="logo_url">
					<upload :list="form.logo_url" name="logo_url" :count="1" @uplSuc="uplSuc" @uplDel="uplDel"></upload>
				</uni-forms-item>
			</uni-forms>
			<view class="agree">
				<checkbox-group @change="changeAgree">
					<label>
						<checkbox :checked="agree" />
						<text @tap.stop="toAgree()">我已阅读并同意“用户协议”和“隐私政策”</text>
					</label>
				</checkbox-group>
			</view>
			<view class="btn">
				<button size="mini" @tap="toSubmit('form')">提交注册</button>
			</view>
		</view>
	</view>
</template>

<script>
	import upload from "@/components/upload/index.vue";
	export default {
		components: {
			upload,
		},
		data() {
			return {
				// 基本信息
				basicInfo: {},
				form: {
					logo_url: [],
				},
				// 校验规则
				rules: {
					account: {
						rules: [{
							required: true,
							errorMessage: '请输入登录账号'
						}]
					},
					password: {
						rules: [{
							required: true,
							errorMessage: '请输入登录密码'
						}]
					},
					phone: {
						rules: [{
							required: true,
							errorMessage: '请输入联系电话'
						}]
					},
				},
				// 用戶协议
				agree: true,
				// 性别
				genderList: []
			};
		},
		onLoad() {

		},
		onShow() {
			const that = this;
			that.searchBasic();
			that.searchOther();
		},
		methods: {
			searchBasic() {
				const that = this;
				uni.getStorage({
					key: 'basicInfo',
					success: (res) => {
						let data = res.data
						that.$set(that, `basicInfo`, data);
					}
				})
			},
			// 图片上传
			uplSuc(e) {
				const that = this;
				that.$set(that.form, `${e.name}`, [...that.form[e.name], e.data]);
			},
			// 图片删除
			uplDel(e) {
				const that = this;
				let data = that.form[e.name];
				let arr = data.filter((i, index) => index != e.data.index);
				that.$set(that.form, `${e.name}`, arr);
			},
			// 提交注册
			toSubmit(ref) {
				const that = this;
				let agree = that.agree;
				that.$refs[ref].validate().then(async parmas => {
					if (agree) {
						let res = await that.$api('user', 'POST', parmas)
						if (res.errcode == '0') {
							if (res.errcode == '0') {
								uni.showToast({
									title: '账号注册成功',
									icon: 'none'
								})
								uni.redirectTo({
									url: '/pagesAccount/login/index'
								})
							}
						} else {
							uni.showToast({
								title: res.errmsg,
								icon: 'none'
							})
						}
					} else {
						uni.showToast({
							title: '请阅读并同意用户协议和隐私政策',
							icon: 'none'
						})
					}
				}).catch(err => {
					console.log('err', err);
				})
			},
			// 同意隐私协议
			changeAgree() {
				const that = this;
				let agree = true;
				if (that.agree) agree = false;
				that.$set(that, `agree`, agree);
			},
			// 查看隐私协议
			toAgree() {
				const that = this;
				uni.navigateTo({
					url: `/pagesAccount/other/agree`
				})
			},
			// 查询其他信息
			async searchOther() {
				const that = this;
				let res;
				res = await that.$api('dictdata', 'GET', {
					type: 'gender'
				})
				if (res.errcode == '0') {
					that.$set(that, `genderList`, res.data)
				}
			}
		}
	}
</script>

<style lang="scss">
	.content {
		background-color: var(--rgb000);
		padding: 0 2vw;
		overflow-y: auto;

		.one {
			text-align: center;
			font-size: 30px;
			font-family: monospace;
			color: var(--rgbfff);
			padding: 6vw 0;
		}

		.two {
			margin: 0 0 4vw 0;

			.btn {
				padding: 2vw 0 0 0;
				text-align: center;

				button {
					width: 80%;
					background-color: var(--rgbfa4);
					color: var(--rgbfff);
					padding: 1vw 0;
				}
			}
		}
	}

	.agree {
		text-align: center;
		font-size: 12px;
		margin: 0 0 2vw 0;
		color: var(--rgbfff);
	}
</style>
