<template>
	<a-modal v-model:visible="openAddModel" :title="title" ok-text="提交" cancel-text="取消" @ok="sumbitForm"
		@cancel="cancelModal" width="50%">
		<a-form :model="createForm" :label-col="{span: 4}" labelAlign="right">
			<a-row gutter="20">
				<a-col span="24">
					<a-form-item required label="姓名" name="name">
						<a-input v-model:value="createForm.name" placeholder="请输入姓名" />
					</a-form-item>
				</a-col>
				<a-col span="24">
					<a-form-item required label="地址" name="address">
						<a-input v-model:value="createForm.address" placeholder="请输入地址" />
					</a-form-item>
				</a-col>
				<a-col span="24">
					<a-form-item required label="电话" name="telephone">
						<a-input v-model:value="createForm.telephone" placeholder="请输入电话" />
					</a-form-item>
				</a-col>
				<a-col span="24">
					<a-form-item required label="传真" name="fax">
						<a-input v-model:value="createForm.fax" placeholder="请输入传真" />
					</a-form-item>
				</a-col>
				<a-col span="24">
					<a-form-item required label="邮政编码" name="postal">
						<a-input v-model:value="createForm.postal" placeholder="请输入邮政编码" />
					</a-form-item>
				</a-col>
				<a-col span="24">
					<a-form-item required label="邮箱" name="email">
						<a-input v-model:value="createForm.email" placeholder="请输入邮箱" />
					</a-form-item>
				</a-col>
				<a-col span="24">
					<a-form-item required label="消息内容" name="content">
						<a-input v-model:value="createForm.content" placeholder="请输入消息内容" />
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
	</a-modal>
</template>

<script setup lang="ts">
	import { ref, onMounted, computed, defineProps, watch, defineEmits } from 'vue';
	import { PostFeedbackAdd, PostFeedbackUpdate } from '@/apis/models';
	import { dataForm, otherDataForm, reset } from '@/views/feedback/list/add/data.js';
	import { useCommon } from '@/hooks/useCommon';
	let { store, openAddModel, hideModal, message, richOption } = useCommon();
	const emit = defineEmits();
	let props = defineProps(['edit_record']);
	let title = ref<String>('新增消息');

	let createForm = ref<FeedbackListType.addFormType>(dataForm)


	const sumbitForm = () => {
		if (!createForm.value.id) {
			PostFeedbackAdd(createForm.value).then(res => {
				message.success('创建消息成功');
				hideModal();
				resetForm();
				emit('successAdd');
			}).catch(err => {
			})
		} else {
			PostFeedbackUpdate(createForm.value).then(res => {
				message.success('修改消息成功');
				hideModal();
				resetForm();
				emit('successAdd');
			}).catch(err => {
			})
		}
	}

	const resetForm = () => {
		createForm.value = reset().dataForm;
	}
	const cancelModal = () => {
		emit('closeAdd');
		resetForm();
		hideModal();
	}


	openAddModel = computed(() => {
		return store.state.openAddModel;
	})

	watch(() => props.edit_record, (newVal) => {
		if (newVal) {
			title.value = '新增消息';

			createForm.value = {
				id: newVal.id,
				name: newVal.name,
				address: newVal.address,
				telephone:newVal.telephone,
				fax: newVal.fax,
				postal: newVal.postal,
				email: newVal.email,
				content: newVal.content,
			}
		} else {
			title.value = '新增消息';
		}
	})
</script>

<style>

</style>