<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose"
            :bottom-btn="false">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form-item label="用户id" prop="uid">
                    <el-input v-model="formData.uid" clearable placeholder="请输入用户id" :readonly="false" />
                </el-form-item>
                <el-form-item label="反馈内容" prop="content">
                    <el-input v-model="formData.content" type="textarea" clearable placeholder="请输入反馈内容"
                        :readonly="false" />
                </el-form-item>
                <el-form-item label="反馈图片" prop="images">
                    <!-- <el-input v-model="formData.images" clearable placeholder="请输入反馈图片" :readonly="false" /> -->

                    <el-image v-for="(item, index) in formData.images" :key="index" style="width: 100px; height: 100px"
                        :src="item" :zoom-rate="1.2" :max-scale="7" :min-scale="0.2" :preview-src-list="formData.images"
                        :initial-index="4" fit="cover" />


                </el-form-item>
                <el-form-item label="联系姓名" prop="name">
                    <el-input v-model="formData.name" clearable placeholder="请输入联系姓名" :readonly="false" />
                </el-form-item>
                <el-form-item label="联系方式" prop="contact">
                    <el-input v-model="formData.contact" clearable placeholder="请输入联系方式" :readonly="false" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="userFeedbackEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiUserFeedbackAdd, apiUserFeedbackEdit, apiUserFeedbackDetail } from '@/api/user_feedback'
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: '',
    uid: '',
    content: '',
    images: '',
    name: '',
    contact: '',
})


// 表单验证
const formRules = reactive<any>({
    uid: [{
        required: true,
        message: '请输入用户id',
        trigger: ['blur']
    }],
    content: [{
        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 apiUserFeedbackDetail({
        id: row.id
    })
    setFormData(data)
}


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