<template>
	<view class="uni-container">
		<uni-nav-bar dark :fixed="true" shadow background-color="#007AFF" status-bar left-icon="left" left-text="返回"
			title="字典子项 编辑" @clickLeft="back" />
			<uni-card>		<uni-forms ref="form" :model="formData" validate-trigger="submit" err-show-type="toast">
			<uni-forms-item name="dictID" label="字典ID">
				<uni-easyinput placeholder="字典ID" v-model="formData.dictID" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="itemText" label="子项文本">
				<uni-easyinput placeholder="子项文本" v-model="formData.itemText" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="itemValue" label="字典项值">
				<uni-easyinput placeholder="字典项值" type="textarea" maxlength="-1" v-model="formData.itemValue"
					trim="both"></uni-easyinput>
			</uni-forms-item>
			<!-- <uni-forms-item name="itemColor" label="子项颜色">
				<uni-easyinput placeholder="字典项颜色" v-model="formData.itemColor" trim="both"></uni-easyinput>
			</uni-forms-item> -->
			<uni-forms-item name="description" label="描述">
				<uni-easyinput placeholder="描述" v-model="formData.description" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="sortOrder" label="排序">
				<uni-easyinput placeholder="排序" v-model="formData.sortOrder" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="status" label="状态">
				<uni-easyinput placeholder="状态" type="number" v-model="formData.status"></uni-easyinput>
			</uni-forms-item>
			<!--  <uni-forms-item name="createBy" label="创建人">
        <uni-easyinput placeholder="创建人" v-model="formData.createBy" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="updateBy" label="更新人">
        <uni-easyinput placeholder="更新人" v-model="formData.updateBy" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="createTime" label="创建时间">
        <uni-datetime-picker v-model="formData.createTime"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="updateTime" label="更新时间">
        <uni-datetime-picker v-model="formData.updateTime"></uni-datetime-picker>
      </uni-forms-item> -->
			<view class="uni-button-group">
				<button type="primary" class="uni-button" @click="submit">提交</button>
			</view>
		</uni-forms>
		</uni-card>
		
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/ngTools_DictItem.js';

	const db = uniCloud.database();
	const dbCollectionName = 'ngTools_DictItem';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.indexOf(key) > -1) {
				result[key] = validator[key]
			}
		}
		return result
	}



	export default {
		data() {
			let formData = {
				"dictID": "",
				"itemValue": "",
				"itemText": "",
				"itemColor": "",
				"description": "",
				"sortOrder": "",
				"status": null,
				"createBy": "",
				"updateBy": "",
				"createTime": null,
				"updateTime": null
			}
			return {
				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({
						icon: 'none',
						title: '修改成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			},
back() {
				uni.navigateBack({
					delta: 1
				})
			},
			/**
			 * 获取表单数据
			 * @param {Object} id
			 */
			getDetail(id) {
				uni.showLoading({
					mask: true
				})
				db.collection(dbCollectionName).doc(id).field(
					"dictID,itemValue,itemText,itemColor,description,sortOrder,status,createBy,updateBy,createTime,updateTime"
					).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>

<style>
	.uni-container {
		padding: 0px;
	}

	.uni-input-border,
	.uni-textarea-border {
		width: 100%;
		font-size: 14px;
		color: #666;
		border: 1px #e5e5e5 solid;
		border-radius: 5px;
		box-sizing: border-box;
	}

	.uni-input-border {
		padding: 0 10px;
		height: 35px;

	}

	.uni-textarea-border {
		padding: 10px;
		height: 80px;
	}

	.uni-button-group {
		margin-top: 50px;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
	}

	.uni-button {
		width: 184px;
	}
</style>