<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="60vw" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">

                <el-row>
                    <el-col :span="8">
                        <el-form-item label="合同名称" prop="contract_id">
                            <el-input v-model="formData.contract_name" clearable placeholder="点击选择合同" readonly
                                @click="showDialog1 = true" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="回访编号">
                            <el-input v-model="formData.code" clearable placeholder="系统自动生成" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="回访方式" prop="return_visit_type">
                            <el-select v-model="formData.return_visit_type" placeholder="请选择回访方式" class=" flex-1">
                                <el-option :label="item.name" :value="parseInt(item.value)"
                                    v-for="item in dictData.return_visit_type">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="回访表模板">
                            <el-input v-model="formData.template_name" clearable placeholder="点击选择回访模版"
                                @click="showDialog = true" readonly />
                        </el-form-item>
                    </el-col>
                    <CreateUserLable :form-data="formData" flag name="回访"></CreateUserLable>
                    <el-col :span="24">
                        <el-form-item label="附件" prop="annex">
                            <UploadAnnex :form-data="formData"></UploadAnnex>
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-dialog v-model="showDialog" title="选择项目" width="70%">
                    <dialogTable :config="marketing_custom_return_visit_template" @customEvent="customEvent">
                    </dialogTable>
                </el-dialog>

                <formTable :formData="formData.survey" :config="tableConfig" />
                <formTable :formData="formData.evaluate" :config="tableConfig1" />

            </el-form>

            <el-dialog v-model="showDialog1" title="选择合同" width="70%">
                <dialogTable :config="marketing_contract" @customEvent="customEvent1">
                </dialogTable>
            </el-dialog>
        </popup>
    </div>
</template>

<script lang="ts" setup name="marketingCustomReturnVisitEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiMarketingCustomReturnVisitAdd, apiMarketingCustomReturnVisitEdit, apiMarketingCustomReturnVisitDetail, apimarketing_custom_return_visit_surveyLists, apimarketing_custom_return_visit_surveytDelete, apimarketing_custom_return_visit_evaluateLists, apimarketing_custom_return_visit_evaluateDelete } from '@/api/marketing_custom_return_visit'
import { marketing_custom_return_visit_template } from "@/components/dialogTable/dialogTableConfig"
import type { PropType } from 'vue'
import { marketing_contract } from "@/components/dialogTable/dialogTableConfig";


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 showDialog = ref(false)
const showDialog1 = ref(false)


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑市场经营--客户回访列表' : '新增市场经营--客户回访列表'
})

// 表单数据
const formData = reactive({
    id: '',
    contract_id: '',
    contract_name: '',
    code: '',
    return_visit_type: '',
    custom_return_visit_template_id: '',
    template_name: "",
    annex: '',
    create_user: '',
    create_time: '',
    survey: [],
    evaluate: []

})


const tableConfig = reactive(
    {
        title: "调查内容",
        tableConfig: [
            {
                label: "调查内容",
                value: 'survey_content',

            },
            {
                label: "内容说明",
                value: 'survey_content_desc',
            },
            {
                label: "评价内容",
                value: 'evaluate_content',
            },
            {
                label: "备注",
                value: 'remark',
            },
        ],
        deleteApi: apimarketing_custom_return_visit_surveytDelete,
    }
)

const tableConfig1 = reactive(
    {
        title: "项目成员评价",
        tableConfig: [
            {
                label: "姓名",
                value: 'name',

            },
            {
                label: "岗位",
                value: 'job',
            },
            {
                label: "意见和建议",
                value: 'opinions',
            },
        ],
        deleteApi: apimarketing_custom_return_visit_evaluateDelete,
    }
)



const customEvent = (e: any) => {
    formData.custom_return_visit_template_id = e.id
    formData.template_name = e.name
    showDialog.value = false
}

const customEvent1 = (e: any) => {
    formData.contract_id = e.id
    formData.contract_name = e.contract_name
    showDialog1.value = false
}


// 表单验证
const formRules = reactive<any>({
    contract_id: [{
        required: true,
        message: '请选择合同',
        trigger: ['blur']
    }],
    code: [{
        required: true,
        message: '请输入回访编号',
        trigger: ['blur']
    }],
    return_visit_type: [{
        required: true,
        message: '请输入回访方式',
        trigger: ['blur']
    }],
    custom_return_visit_template_id: [{
        required: true,
        message: '请输入回访表模板',
        trigger: ['blur']
    }],
    create_user: [{
        required: true,
        message: '请输入回访人',
        trigger: ['blur']
    }],
    create_time: [{
        required: true,
        message: '请输入回访日期',
        trigger: ['blur']
    }]
})


// 获取详情
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]
        }
    }
    let res = await apimarketing_custom_return_visit_surveyLists({ custom_return_visit_id: data.id })
    formData.survey = res.lists
    let res1 = await apimarketing_custom_return_visit_evaluateLists({ custom_return_visit_id: data.id })
    formData.evaluate = res1.lists

}

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


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