<template>
	<view class="content">
		<view class="one">
			<uni-forms ref="valiForm" :rules="rules" :modelValue="form" labelWidth="80px">
				<uni-forms-item label="姓名" required name="name">
					<uni-easyinput v-model="form.name" placeholder="请输入姓名" />
				</uni-forms-item>
				<uni-forms-item label="性别" required name="gender">
					<uni-data-checkbox v-model="form.gender" :localdata="genderList" />
				</uni-forms-item>
				<uni-forms-item label="联系电话" required name="tel">
					<uni-easyinput v-model="form.tel" placeholder="请输入联系电话" />
				</uni-forms-item>
				<uni-forms-item label="角色" required name="role">
					<uni-data-select v-model="form.role" :localdata="roleList" @change="rolechange"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item v-if="disabled" label="所属街道" required name="street">
					<uni-data-select v-model="form.street" :localdata="streetList" @change="streetchange">
					</uni-data-select>
				</uni-forms-item>
				<uni-forms-item v-if="disabled" label="所属社区">
					<uni-data-select v-model="form.community" :localdata="commList" @change="commchange">
					</uni-data-select>
				</uni-forms-item>
			</uni-forms>
			<button class="button" type="primary" @click="submit('valiForm')">修改</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				disabled: true,
				id:'',
				form: {},
				// 校验规则
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '姓名不能为空'
						}]
					},
					tel: {
						rules: [{
							required: true,
							errorMessage: '联系电话不能为空'
						}, {
							format: 'number',
							errorMessage: '联系电话只能输入数字'
						}]
					},
					gender: {
						rules: [{
							required: true,
							errorMessage: '性别不能为空'
						}]
					},
					role: {
						rules: [{
							required: true,
							errorMessage: '角色不能为空'
						}]
					},
				},
				// 字典表
				roleList: [],
				genderList: [],
				streetList: [],
				commList: []
			}
		},
		async onLoad() {
			const that = this;
			await that.searchToken();
			await that.searchOther();
		},
		methods: {
			async searchToken() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: function(res) {
						that.$set(that, `id`, res.data._id);
						that.$set(that, `form`, res.data);
						if (res.data.role == 'cs') that.$set(that, `disabled`, false);
						else that.$set(that, `disabled`, true);
					},
					fail: function(err) {
						uni.showToast({
							title: err.errmsg,
							icon: 'error',
							duration: 2000
						});
					}
				})
			},
			// 角色选择
			rolechange(role) {
				const that = this;
				if (role == 'cs') that.$set(that, `disabled`, false);
				else that.$set(that, `disabled`, true);
			},
			// 所属街道选择
			async streetchange(belong) {
				const that = this;
				that.$set(that.form, `street`, belong);
				//所属社区
				const res = await that.$api('/Office', 'GET', {
					belong,
					type: '1',
					is_use: '0'
				})
				if (res.errcode == '0') {
					let commList = []
					for (let val of res.data) {
						commList.push({
							text: val.name,
							value: val._id
						})
					}
					that.$set(that, `commList`, commList);
				}
			},
			// 所属社区
			commchange(community) {
				const that = this;
				that.$set(that.form, `community`, community);
			},
			// 修改
			submit(ref) {
				const that = this;
				that.$refs[ref].validate().then(async params => {
					that.form.status = '0'
					delete that.form._id;
					delete that.form.iat;
					delete that.form.exp;
					const res = await that.$api(`/User/${that.id}`, 'POST', that.form);
					if (res.errcode == '0') {
						uni.showToast({
							title: '修改信息成功',
							icon: 'none'
						})
						that.toExit()
					} else {
						uni.showToast({
							title: res.errmsg,
							icon: 'none'
						})
					}
				}).catch(err => {
					console.log('err', err);
				})
			},
			// 退出登录
			toExit() {
				uni.removeStorage({
					key: 'token',
					success: function(res) {
						let url = `/pages/index/index`;
						uni.reLaunch({
							url
						})
					}
				});
			},
			async searchOther() {
				const that = this;
				let res;
				//性别
				res = await that.$api('/DictData', 'GET', {
					type: 'gender',
					is_use: '0'
				})
				if (res.errcode == '0') {
					let genderList = []
					for (let val of res.data) {
						genderList.push({
							text: val.label,
							value: val.value
						})
					}
					that.$set(that, `genderList`, genderList);
				}
				//角色
				res = await that.$api('/Role', 'GET', {
					is_use: '0'
				})
				if (res.errcode == '0') {
					let roleList = []
					for (let val of res.data) {
						roleList.push({
							text: val.name,
							value: val.code
						})
					}
					that.$set(that, `roleList`, roleList);
				}
				//所属街道
				res = await that.$api('/Office', 'GET', {
					type: '0',
					is_use: '0'
				})
				if (res.errcode == '0') {
					let streetList = []
					for (let val of res.data) {
						streetList.push({
							text: val.name,
							value: val._id
						})
					}
					that.$set(that, `streetList`, streetList);
				}
			},
		}
	}
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;

		.one {
			padding: 3vw;

			.button {
				margin: 2vw 0 0 0;
				background-color: var(--f3CColor);
				color: var(--mainColor);
				font-size: var(--font14Size);
			}
		}
	}
</style>
