<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" :rules="formRules">
                <el-form-item label="项目名称" prop="project_id">
                    <el-input v-model="formData.project_name" @click="showDialog = true" clearable placeholder="点击选择项目" />
                </el-form-item>
                <el-form-item label="执行人" prop="executor">
                    <el-input v-model="formData.executor" clearable placeholder="请输入执行人" />
                </el-form-item>
                <el-form-item label="联系人" prop="contacts">
                    <el-input v-model="formData.contacts" clearable placeholder="请输入联系人" />
                </el-form-item>
                <el-form-item label="联系方式" prop="contact_information">
                    <el-input v-model="formData.contact_information" clearable placeholder="请输入联系方式" />
                </el-form-item>
                <el-form-item label="项目角色" prop="project_role">
                    <el-input v-model="formData.project_role" clearable placeholder="请输入项目角色" />
                </el-form-item>
                <el-form-item label="职位" prop="position">
                    <el-input v-model="formData.position" clearable placeholder="请输入职位" />
                </el-form-item>
                <el-form-item label="跟进日期" prop="follow_date">
                    <el-date-picker class="flex-1 !flex" v-model="formData.follow_date" clearable type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择跟进日期">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="跟进类型" prop="follow_type">
                    <el-select class="flex-1" v-model="formData.follow_type" clearable placeholder="请选择跟进类型">
                        <el-option v-for="(item, index) in dictData.types" :key="index" :label="item.name"
                            :value="parseInt(item.value)" />
                    </el-select>
                </el-form-item>
                <el-form-item label="主题" prop="theme">
                    <el-input v-model="formData.theme" clearable placeholder="请输入主题" />
                </el-form-item>
                <el-form-item label="行动描述" prop="action_description">
                    <el-input v-model="formData.action_description" clearable placeholder="请输入行动描述" />
                </el-form-item>
                <el-form-item label="项目把握度" prop="project_assurance">
                    <el-select class="flex-1" v-model="formData.project_assurance" clearable placeholder="请选择项目把握度">
                        <el-option v-for="(item, index) in dictData.project_assurance" :key="index" :label="item.name"
                            :value="parseInt(item.value)" />
                    </el-select>
                </el-form-item>
                <el-form-item label="跟进状态" prop="follow_status">
                    <el-select class="flex-1" v-model="formData.follow_status" clearable placeholder="请选择跟进状态">
                        <el-option v-for="(item, index) in dictData.follow_status" :key="index" :label="item.name"
                            :value="parseInt(item.value)" />
                    </el-select>
                </el-form-item>
                <el-form-item label="跟进阶段" prop="follow_stage">
                    <el-input v-model="formData.follow_stage" clearable placeholder="请输入跟进阶段" />
                </el-form-item>
                <el-form-item label="备注" prop="notes">
                    <el-input v-model="formData.notes" clearable placeholder="请输入备注" />
                </el-form-item>
                <el-form-item label="下次回访日期" prop="next_follow_up_date">
                    <el-date-picker class="flex-1 !flex" v-model="formData.next_follow_up_date" clearable type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择下次回访日期">
                    </el-date-picker>
                </el-form-item>
                <el-dialog v-model="showDialog" title="选择客户" width="70%">
                    <projectTable @customEvent="customEvent"></projectTable>

                </el-dialog>
                <!-- <el-form-item label="" prop="ceate_time">
                    <el-input v-model="formData.ceate_time" clearable placeholder="请输入" />
                </el-form-item> -->
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="projectFollowUpEdit">
import type { FormInstance } from 'element-plus'
import projectTable from "@/components/tablieDialog/project.vue"
import Popup from '@/components/popup/index.vue'
import { apiProjectFollowUpAdd, apiProjectFollowUpEdit, apiProjectFollowUpDetail } from '@/api/project_follow_up'
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')
// dialog
const showDialog = ref(false)
const customEvent = (e: any) => {
    formData.project_id = e.id;
    formData.project_name = e.name;
    showDialog.value = false;
};

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

// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    project_name: '',
    executor: '',
    contacts: '',
    contact_information: '',
    project_role: '',
    position: '',
    follow_date: '',
    follow_type: '',
    theme: '',
    action_description: '',
    project_assurance: '',
    follow_status: '',
    follow_stage: '',
    notes: '',
    next_follow_up_date: '',
    ceate_time: '',
})


// 表单验证
const formRules = reactive<any>({

})


// 获取详情
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.follow_date = timeFormat(formData.follow_date, 'yyyy-mm-dd hh:MM:ss')
    //@ts-ignore
    formData.next_follow_up_date = timeFormat(formData.next_follow_up_date, 'yyyy-mm-dd hh:MM:ss')
}

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


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiProjectFollowUpEdit(data)
        : await apiProjectFollowUpAdd(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>
