<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validateTrigger="bind">
			<uni-forms-item name="name" label="用户名">
				<uni-easyinput v-model="formData.name"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="icon" label="用户头像" @click="avatarChoose">
				<image :src="formData.icon" style="width: 120px; height: 80px;"></image>
			</uni-forms-item>
			<uni-forms-item name="gender" label="性别">
				<uni-data-checkbox :localdata="genders" v-model="formData.gender" />
				<!-- <uni-easyinput v-model="formData.gender"></uni-easyinput> -->
			</uni-forms-item>
			<uni-forms-item name="password" label="密码">
				<uni-easyinput v-model="formData.password"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="phone_num" label="手机号">
				<uni-easyinput v-model="formData.phone_num"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="team_id" label="小组 id">
				<uni-easyinput v-model="formData.team_id"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="school" label="学校">
				<uni-easyinput v-model="formData.school"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="type" label="人员">
					<uni-data-checkbox :localdata="types" v-model="formData.type" />
				<!-- <uni-easyinput v-model="formData.type"></uni-easyinput> -->
			</uni-forms-item>
			<uni-forms-item name="total_running_distance" label="跑步总距离">
				<uni-number-box :value="0.0" :step="0.01" v-model="formData.total_running_distance" />
				<!-- <uni-easyinput type="number" v-model="formData.total_running_distance"></uni-easyinput> -->
			</uni-forms-item>
			<uni-forms-item name="total_walking_distance" label="健走总距离">
				<uni-number-box :value="0.0" :step="0.01" v-model="formData.total_walking_distance" />
				<!-- <uni-easyinput type="number" v-model="formData.total_walking_distance"></uni-easyinput> -->
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../../js_sdk/validator/mustgo-user.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'mustgo-user';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}

	export default {
		data() {
			let formData = {
				"name": "",
				"icon": "",
				"gender": "",
				"password": "",
				"phone_num": "",
				"team_id": "",
				"school": "",
				"type": "",
				"total_running_distance": 0,
				"total_walking_distance": 0
			}
			return {
				// icon: [

				// ],
				genders: [
					{text: '男', value: '男'},
					{text: '女', value: '女'}
				],
				types: [
					{text: '学生', value: '644a64c228064a7587cd79bf'},
					{text: '老师', value: '644a64d2e766bb0085e6048e'}
				],
				formData,
				formOptions: {},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onLoad(e) {
			if (e.id) {
				const id = e.id
				this.formDataId = id
				this.getDetail(id)
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
					uni.showToast({
						title: '修改成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			},
			avatarChoose() {
				let that = this;
				console.log("upload image loading")
				uniCloud.chooseAndUploadFile({
					type: 'image',
					success(res) {
						that.formData.icon = res["tempFiles"][0]["url"]
						console.log(res)
					},
					fail() {
						console.log("upload icon failed")
					},
					complete() {}
				});
			},
			/**
			 * 获取表单数据
			 * @param {Object} id
			 */
			getDetail(id) {
				uni.showLoading({
					mask: true
				})
				db.collection(dbCollectionName).doc(id).field(
					"name,icon,gender,password,phone_num,team_id,school,type,total_running_distance,total_walking_distance"
				).get().then((res) => {
					const data = res.result.data[0]
					if (data) {
						this.formData = data

					}
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>