<template>
	<a-form
		:label-col="useUpdateMethod.formLabelCol"
		:model="formState"
		:wrapper-col="useUpdateMethod.formWrapperCol"
		autocomplete="off"
		name="basic"
		@finish="onFinish"
		@finishFailed="useUpdateMethod.formFail"
	>
		<base-form-item v-for="item of fields" :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 class="formButton" @click="useUpdateMethod.handleCancel()">取消 </a-button>
		</a-form-item>
	</a-form>
</template>

<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import { messageMethod } from '../../../utils/message-util'
import requestMethod from '../../../api/system-log/version'
import { LogVersionFormDTO } from '../../../entity/system-log/log-version'
import { QueryObjectType } from '../../../http/request-model'
import { updateMethod } from '../../../components/form/method'
import BaseFormItem from '../../../components/form-item/BaseFormItem.vue'
import fields from './fields'

/**
 * 定义传入参数
 */
const props = defineProps<{
	id?: string
}>()

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

/**
 * 表单对象
 */
const formState = ref<LogVersionFormDTO>({
	versionCode: '',
	updateIntro: ''
})

/**
 * 信息初始化表单
 */
onMounted(async () => {
	if (props.id) {
		const { data } = await requestMethod.getById(props.id)
		formState.value = { ...data }
	}
})

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

<style scoped>
.formButton {
	margin-left: 0.625rem;
}
</style>
