<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="name">
                    <el-input v-model="formData.name" clearable placeholder="请输入模板名称" />
                </el-form-item>
                <el-form-item label="是否启用 " prop="is_use">
                    <el-select v-model="formData.is_use" class="flex-1">
                        <el-option label="启用" :value="0"></el-option>
                        <el-option label="不启用" :value="1"></el-option>
                    </el-select>
                </el-form-item>
                <CreateUserLable :form-data="formData"></CreateUserLable>
                <formTable :formData="formData.detail" :config="tableConfig" :dictData="dictData">

                </formTable>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="marketingCustomReturnVisitTemplateEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiMarketingCustomReturnVisitTemplateAdd, apiMarketingCustomReturnVisitTemplateEdit, apiMarketingCustomReturnVisitTemplateDetail, apiMarketingCustomReturnVisitTemplateDetailLists, apiMarketingCustomReturnVisitTemplateDetailDelete } from '@/api/marketing_custom_return_visit_template'
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: '',
    is_use: '',
    create_user: '',
    create_time: '',
    detail: []
})

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

            },
            {
                label: "内容说明",
                value: 'survey_content_desc',

            },

        ],
        deleteApi: apiMarketingCustomReturnVisitTemplateDetailDelete,
    }
)


// 表单验证
const formRules = reactive<any>({
    name: [{
        required: true,
        message: '请输入模板名称',
        trigger: ['blur']
    }],
    is_use: [{
        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 apiMarketingCustomReturnVisitTemplateDetailLists({ custom_return_visit_template_id: data.id })
    formData.detail = res.lists


}

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


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