<template>
	<dc-drawer-header v-if="props.id" :detail="formState" />

	<a-form
		:label-align="useUpdateMethod.formLabelAlign"
		:label-col="useUpdateMethod.formLabelCol"
		:model="formState"
		:wrapper-col="useUpdateMethod.formWrapperCol"
		scrollToFirstError
		@finish="onFinish"
		@finishFailed="useUpdateMethod.formFail"
	>
		<base-form-item v-for="item of filedList" :key="item.name" v-model:value="formState[item.name]" v-bind="item" />

		<a-form-item :wrapper-col="useUpdateMethod.formActionCol">
			<a-button html-type="submit" type="primary">提交</a-button>
			<a-button style="margin-left: 10px" @click="useUpdateMethod.handleCancel">关闭</a-button>
		</a-form-item>
	</a-form>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import requestUser from '../../../api/account/user'
import requestDept from '../../../api/account/department'
import requestOrg from '../../../api/account/organization'
import requestRole from '../../../api/account/role'
import { AccountUserInfo } from '../../../entity/account/account-user'
import { QueryObjectType } from '../../../http/request-model'
import { messageMethod } from '../../../utils/message-util'
import { updateMethod } from '../../../components/form/method'
import BaseFormItem from '../../../components/form-item/BaseFormItem.vue'
import fields from './fields'
import { SelectProps, UploadProps } from '../../../components/form-item/props'
import { BaseUtil } from '../../../utils/base-util'

// 操作页面的公用方法
const useUpdateMethod = updateMethod()

// 获取父组件参数
const props = defineProps<{ id?: string }>()
defineEmits<{ (e: 'cancel'): void; (e: 'refresh'): void }>()

const formState = reactive<Partial<AccountUserInfo>>({
	userAccount: '',
	userName: '',
	activeStatus: 1
})

/**
 * 如果是编辑，获取数据
 * 请求列表数据
 */
const filedList = ref(fields)
onMounted(async () => {
	// 所属单位
	const orgTypeValue = filedList.value.find(value => value.name == 'orgIdList')?.typeValue
	if (orgTypeValue) {
		let { data } = await requestOrg.formSelect()
		let temp = orgTypeValue as SelectProps
		temp.options = data
	}

	// 获取部门
	const departmentTypeValue = filedList.value.find(value => value.name == 'deptIdList')?.typeValue
	if (departmentTypeValue) {
		let { data } = await requestDept.formSelect()
		let temp = departmentTypeValue as SelectProps
		temp.options = data
	}

	// 获取角色
	const roleTypeValue = filedList.value.find(value => value.name == 'roleIdList')?.typeValue
	if (roleTypeValue) {
		let { data } = await requestRole.formSelect()
		let temp = roleTypeValue as SelectProps
		temp.options = data
	}
})

onMounted(async () => {
	if (!props.id) return

	const { data } = await requestUser.getById(props.id)
	Object.assign(formState, { ...data.userInfo, ...data })

	// 上级部门
	if (data.orgList) {
		formState.orgIdList = data.orgList.map(value => value.id)
	}

	// 部门
	if (data.deptList) {
		formState.deptIdList = data.deptList.map(value => value.id)
	}

	// 角色
	if (data.roleList) {
		formState.roleIdList = data.roleList.map(value => value.id)
	}

	// 获取角色
	if (data.headPath) {
		let typeValue = filedList.value.find(value => value.name == 'headPath')?.typeValue as UploadProps
		if (typeValue) {
			typeValue.fileList = [
				{
					id: '-1',
					uploadPath: BaseUtil.getUploadPath(data.headPath)
				}
			]
		}
		// console.log(typeValue)
	}
})

/**
 * 提交表单
 * @param value
 */
const onFinish = async (value: AccountUserInfo) => {
	const data = {
		...(value as unknown as QueryObjectType),
		version: formState.version || 0
	}
	if (props.id) {
		const resultModel = await requestUser.update({
			...data,
			id: props.id
		})
		messageMethod.success(resultModel.message)
		useUpdateMethod.handleSave()
	} else {
		const resultModel = await requestUser.add({ ...(data as unknown as QueryObjectType) })
		messageMethod.success(resultModel.message)
		useUpdateMethod.handleSave()
	}
}
</script>

<style scoped></style>
