<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="父级部门" label="父级部门">
					<depart-select :selectID="formData.parent_id" @change="onselectionchange"></depart-select>
				</uni-forms-item>
				<uni-forms-item name="depart_name" label="部门名称">
					<uni-easyinput placeholder="部门名称" v-model="formData.depart_name" trim="both"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="level" label="部门层级">
					<uni-easyinput placeholder="部门层级，为提升检索效率而作的冗余设计" type="number"
						v-model="formData.level"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="sort" label="显示顺序">
					<uni-easyinput placeholder="部门在当前层级下的顺序，由小到大" type="number" v-model="formData.sort"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="manager_uid" label="部门主管">
					<user-select v-model="formData.manager_uid"></user-select>
					<uni-easyinput placeholder="部门主管的userid， 参考`uni-id-users` 表"
						v-model="formData.manager_uid"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="status" label="部门状态">
					<uni-easyinput placeholder="部门状态，0-正常、1-禁用" type="number" v-model="formData.status"></uni-easyinput>
				</uni-forms-item>
			</uni-forms>
			
		</uni-card><view class="uni-button-group">
				<button type="primary" class="uni-button" @click="submit">提交</button>
			</view>
	</view>
</template>

<script>
	import userSelect from './user_select.vue';
	import {
		validator
	} from '../../js_sdk/validator/ngTools_depart.js';
	import departSelect from './depart_select.vue';
	const db = uniCloud.database();
	const dbCollectionName = 'ngTools_depart';

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



	export default {
		components: {
			departSelect,
			userSelect
		},
		data() {
			let formData = {
				"parent_id": "",
				"depart_name": "",
				"level": null,
				"sort": null,
				"manager_uid": "",
				"status": null
			}
			return {
				parentID: "", 
				selectDepartID:[],
				formData,
				formOptions: {},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onLoad(e) {
			console.log(e.pNode)
			
			if (e.pNode) {
				var pNode=JSON.parse(e.pNode)
				const id = pNode._id
				this.selectDepartID.push(pNode.parent_id)
				console.log(this.selectDepartID)
				this.formDataId = id
				this.getDetail(id)
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			
			onselectionchange(e) {
				// console.log(selectDepartID)
				this.parentID = e;
				// console.log(this.formData)
			},
			
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					res.parent_id = this.parentID;
					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
					})
				})
			},

			/**
			 * 获取表单数据
			 * @param {Object} id
			 */
			getDetail(id) {
				uni.showLoading({
					mask: true
				})
				db.collection(dbCollectionName).doc(id).field("parent_id,depart_name,level,sort,manager_uid,status").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>