<template>
	<div class="edit-popup">
		<popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
			@close="handleClose">
			<el-form ref="formRef" :model="formData" label-width="90px">
				<el-form-item label="名称" prop="name">
					<el-input v-model="formData.name" clearable placeholder="请输入名称" />
				</el-form-item>
				<el-form-item label="地址" prop="address">
					<el-input v-model="formData.address" clearable placeholder="请输入地址" />
				</el-form-item>
				<el-form-item label="开始时间" prop="start_time">
					<daterange-picker v-model:startTime="formData.start_time"
						v-model:endTime="formData.end_time" />
				</el-form-item>
				<el-form-item label="联系人" prop="linkman">
					<el-input v-model="formData.linkman" clearable placeholder="请输入联系人" />
				</el-form-item>
				<el-form-item label="联系部门" prop="contacts_dept">
					<el-input v-model="formData.contacts_dept" clearable placeholder="请输入联系部门" />
				</el-form-item>
				<el-form-item label="联系电话" prop="contact_number">
					<el-input v-model="formData.contact_number" clearable placeholder="请输入联系电话" />
				</el-form-item>
				<el-form-item label="区域经理" prop="area_manager_id">
					<el-input v-model="formData.area_manager_id" clearable placeholder="请输入区域经理" />
				</el-form-item>
				<el-form-item label="状态" prop="project_status">
					<el-select class="flex-1" v-model="formData.project_status" clearable placeholder="请选择状态">
						<el-option v-for="(item, index) in dictData.system_disable" :key="index" :label="item.name"
							:value="parseInt(item.value)" />
					</el-select>
				</el-form-item>
			</el-form>
		</popup>
	</div>
</template>

<script lang="ts" setup name="projectEdit">
	import type { FormInstance } from 'element-plus'
	import Popup from '@/components/popup/index.vue'
	import { apiProjectAdd, apiProjectEdit, apiProjectDetail } from '@/api/project'
	import { timeFormat } from '@/utils/util'
	import type { PropType } from 'vue'
	defineProps({
		dictData: {
			type: Object as PropType<Record<string, any[]>>,
			default: () => ({})
		}
	})
	const emit = defineEmits(['success', 'close'])
	const formRef = shallowRef<FormInstance>()
	const popupRef = shallowRef<InstanceType<typeof Popup>>()
	const mode = ref('add')

	// 弹窗标题
	const popupTitle = computed(() => {
		return mode.value == 'edit' ? '编辑项目' : '新增项目'
	})

	// 表单数据
	const formData = reactive({
		id: '',
		name: '',
		address: '',
		project_status: '',
		start_time: '',
		end_time: '',
		linkman: '',
		contacts_dept: '',
		contact_number: '',
		area_manager_id: '',
	})

	// 获取详情
	const setFormData = async (data : Record<any, any>) => {
		for (const key in formData) {
			if (data[key] != null && data[key] != undefined) {
				//@ts-ignore
				formData[key] = data[key]
			}
		}

		//@ts-ignore
		formData.start_time = timeFormat(formData.start_time, 'yyyy-mm-dd hh:MM:ss')
		//@ts-ignore
		formData.end_time = timeFormat(formData.end_time, 'yyyy-mm-dd hh:MM:ss')
	}

	const getDetail = async (row : Record<string, any>) => {
		const data = await apiProjectDetail({
			id: row.id
		})
		setFormData(data)
	}

	// 提交按钮
	const handleSubmit = async () => {
		await formRef.value?.validate()
		const data = { ...formData, }
		mode.value == 'edit'
			? await apiProjectEdit(data)
			: await apiProjectAdd(data)
		popupRef.value?.close()
		emit('success')
	}

	//打开弹窗
	const open = (type = 'add') => {
		mode.value = type
		popupRef.value?.open()
	}

	// 关闭回调
	const handleClose = () => {
		emit('close')
	}

	defineExpose({
		open,
		setFormData,
		getDetail
	})
</script>