<template>
	<xn-form-container
		title="咨询信息"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-descriptions bordered title="咨询信息" size="small" :column="2" :labelStyle="{'width': '180px'}">
			<a-descriptions-item label="姓名">{{ formRecord.name }}</a-descriptions-item>
			<a-descriptions-item label="手机号">{{ formRecord.phone }}</a-descriptions-item>
			<a-descriptions-item label="邮箱">{{ formRecord.email }}</a-descriptions-item>
			<a-descriptions-item label="咨询类型">{{ formRecord.typeName }}</a-descriptions-item>
			<a-descriptions-item label="咨询内容" :span="2">{{ formRecord.content }}</a-descriptions-item>
			<a-descriptions-item label="处理状态" :span="2">
				<a-tag :color="formRecord.contactStatus ? Sys.status[1].color : Sys.status[0].color">
					{{ formRecord.contactStatus ? Sys.status[1].label : Sys.status[0].label }}
				</a-tag>
			</a-descriptions-item>
			<a-descriptions-item label="后续备注" :span="2">{{ formRecord.remark }}</a-descriptions-item>
		</a-descriptions>

		<a-divider orientation="left" style="margin-top: 15px">咨询信息处理</a-divider>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="14">
					<a-form-item label="是否处理：" name="contactStatus">
						<a-switch v-model:checked="formData.contactStatus" />
					</a-form-item>
				</a-col>

				<a-col :span="14">
					<a-form-item label="后续备注：" name="remark">
						<a-textarea v-model:value="formData.remark" placeholder="请输入后续备注"
									:auto-size="{ minRows: 2, maxRows: 3 }"/>
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="consultForm">

import {cloneDeep} from 'lodash-es'
import {required} from '@/utils/formRules'
import consultApi from '@/api/website/consultApi'
import {Sys} from "@/utils/constant";
// 抽屉状态
const visible = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
// 表单数据
const formData = ref({})
const formRecord = ref({})
const submitLoading = ref(false)

// 打开抽屉
const onOpen = (record) => {
	visible.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
		formRecord.value = Object.assign({}, recordData)
	}
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	visible.value = false
}
// 默认要校验的
const formRules = {
	contactStatus: [required('请选择处理状态')],
}
// 验证并提交数据
const onSubmit = () => {
	formRef.value.validate().then(() => {
		submitLoading.value = true
		const formDataParam = cloneDeep(formData.value)
		consultApi.save(formDataParam, formDataParam.id)
			.then(() => {
				onClose()
				emit('successful')
			})
			.finally(() => {
				submitLoading.value = false
			})
	})
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
