<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<uni-forms ref="form" :modelValue="form" :rules="rules" label-width="auto">
					<uni-forms-item label="姓名" required name="name">
						<uni-easyinput type="text" v-model="form.name" placeholder="请输入真实姓名" />
					</uni-forms-item>
					<uni-forms-item label="身份证号" required name="card">
						<uni-easyinput type="text" v-model="form.card" placeholder="请输入身份证号" />
					</uni-forms-item>
					<uni-forms-item label="手机号" required name="phone">
						<uni-easyinput type="text" v-model="form.phone" placeholder="请输入手机号" />
					</uni-forms-item>
					<uni-forms-item>
						<uni-data-checkbox v-model="affirm" multiple :localdata="affirmList" @change="affirmChange" />
					</uni-forms-item>
				</uni-forms>
				<view class="btn">
					<button type="primary" @click="onSubmit('form')" size="mini"
						:disabled="is_affirm=='0'?true:false">提交</button>
					<button type="primary" @click="toApply" size="mini">申请记录</button>
				</view>
				<view class="config">
					<rich-text :nodes="config&&config.leader&&config.leader.rule"></rich-text>
				</view>
			</view>
		</view>

	</mobile-frame>
</template>

<script>
	export default {
		data() {
			return {
				// 系统设置
				config: {},
				form: {},
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '请填写真实姓名',
						}]
					},
					card: {
						rules: [{
							required: true,
							errorMessage: '请填写身份证',
						}]
					},
					phone: {
						rules: [{
							required: true,
							errorMessage: '请填写手机号码',
						}]
					},

				},
				affirmList: [],
				affirm: [],
				is_affirm: 0
			};
		},
		onLoad: function(e) {
			const that = this;
			that.watchLogin();
			that.searchConfig();
		},
		methods: {
			watchLogin() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: async (res) => {
						let user = that.$jwt(res.data);
						if (user) {
							that.$set(that, `form`, {
								user_id: user.id,
								openid: user.openid,
								phone: user.phone
							})
						}
					},
					fail: (err) => {}
				})
			},
			searchConfig() {
				const that = this;
				uni.getStorage({
					key: 'config',
					success: async (res) => {
						let data = res.data;
						if (data.leader && data.leader.rule) data.leader.rule = data.leader.rule.replace(
							/\<img/gi,
							'<img class="rich-img"');
						that.$set(that, `config`, data);
						// 整理复选框
						if (data.leader && data.leader.explain) {
							let list = [ //
								{
									text: data.leader.explain,
									value: 0
								}
							]
							that.$set(that, `affirmList`, list)
						}
					},
					fail: (err) => {}
				})
			},
			// 选择
			affirmChange(e) {
				const that = this;
				const length = e.detail.value.length;
				that.$set(that, `is_affirm`, length)
			},
			// 提交保存
			onSubmit(ref) {
				const that = this;
				that.$refs[ref].validate().then(async params => {
					let is_data = await that.getData();
					if (is_data) {
						uni.showToast({
							title: `已有申请数据正在审核中，不可重复申请`,
							icon: 'none',
						});
					} else {
						uni.getStorage({
							key: 'shop',
							success: async function(res) {
								that.$set(that.form, `shop`, res.data);
								const arr = await that.$api(`/userleader`, 'POST', that.form);
								if (arr.errcode == '0') {
									uni.showToast({
										title: `会员身份申请成功`,
										icon: 'success',
									});
									that.back();
								} else {
									uni.showToast({
										title: arr.errmsg,
									})
								}
							}
						})
					}
				})
			},
			async getData() {
				const that = this;
				let res = await that.$api('/userleader', 'GET', {
					user_id: that.form.user_id,
				})
				if (res.errcode == '0') {
					if (res.total > 0) {
						let data = res.data.find(i => i.status == '0');
						if (data) {
							return true
						} else {
							return false
						}

					} else {
						return false
					}
				}
			},
			// 会员记录
			toApply() {
				uni.navigateTo({
					url: `/pagesMy/apply/index`
				})
			},
			// 返回
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
		},
	}
</script>

<style lang="scss">
	.main {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			padding: 2vw;

			.uni-input {
				border: #f1f1ff 1px solid;
				padding: 2vw 2vw;
				border-radius: 1vw;
			}

			.btn {
				text-align: center;
				margin: 0 0 1vw 0;

				button {
					margin: 0 2vw 2vw 2vw;
					background-color: #23B67A;
					color: var(--fffColor);
				}

				.name {
					color: var(--f85Color);
					font-size: var(--font14Size);
				}

			}

			.config {
				.rich-img {
					width: 100% !important;
					display: block;
				}
			}
		}
	}

	.uni-forms-item {
		margin-bottom: 6vw !important;
		display: flex;
		flex-direction: row;
	}

	.uni-data-checklist .checklist-group .checklist-box .checklist-content {
		width: 91vw;
	}
</style>