<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="120px" :rules="formRules">
                <el-form-item label="客户回访编号" prop="custom_return_visit_code">
                    <el-input v-model="formData.custom_return_visit_code" clearable placeholder="点击选择客户回访编号" readonly
                        @click="showDialog = true" />
                </el-form-item>
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="formData.name" clearable placeholder="请输入姓名" />
                </el-form-item>
                <el-form-item label="岗位" prop="job">
                    <el-input v-model="formData.job" clearable placeholder="请输入岗位" />
                </el-form-item>
                <el-form-item label="意见和建议" prop="opinions">
                    <el-input v-model="formData.opinions" clearable placeholder="请输入意见和建议" type="textarea" />
                </el-form-item>
            </el-form>

            <el-dialog v-model="showDialog" title="选择回访编号" width="70%">
                <dialogTable :config="marketing_custom_return_visit" @customEvent="customEvent">
                </dialogTable>
            </el-dialog>
        </popup>
    </div>
</template>

<script lang="ts" setup name="marketingCustomReturnVisitEvaluateEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiMarketingCustomReturnVisitEvaluateAdd, apiMarketingCustomReturnVisitEvaluateEdit, apiMarketingCustomReturnVisitEvaluateDetail } from '@/api/marketing_custom_return_visit_evaluate'
import { marketing_custom_return_visit } from "@/components/dialogTable/dialogTableConfig"

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


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

// 表单数据
const formData = reactive({
    id: '',
    custom_return_visit_id: '',
    custom_return_visit_code: '',
    name: '',
    job: '',
    opinions: '',
})

const customEvent = (e: any) => {
    formData.custom_return_visit_id = e.id
    formData.custom_return_visit_code = e.code
    showDialog.value = false
}



// 表单验证
const formRules = reactive<any>({
    custom_return_visit_id: [{
        required: true,
        message: '请输入客户回访id',
        trigger: ['blur']
    }],
    name: [{
        required: true,
        message: '请输入姓名',
        trigger: ['blur']
    }],
    job: [{
        required: true,
        message: '请输入岗位',
        trigger: ['blur']
    }],
    opinions: [{
        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]
        }
    }


}

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


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