<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="组织id" prop="org_id">
                    <el-select class="flex-1" v-model="formData.org_id" clearable placeholder="请选择组织id">
                        <!-- <el-option 
                            v-for="(item, index) in dictData."
                            :key="index" 
                            :label="item.name"
                            :value="parseInt(item.value)"
                        /> -->
                    </el-select>
                </el-form-item>
                <el-form-item label="部门id" prop="department_id">
                    <el-input class="flex-1" v-model="formData.department_id" type="textarea" rows="4" clearable
                        placeholder="请输入部门id" />
                </el-form-item>
                <el-form-item label="项目id" prop="project_id">
                    <el-input v-model="formData.project_id" clearable placeholder="请输入项目id" />
                </el-form-item>
                <el-form-item label="客户需求id" prop="customer_demand_id">
                    <el-input v-model="formData.customer_demand_id" clearable placeholder="请输入客户需求id" />
                </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="submission_time">
                    <el-date-picker class="flex-1 !flex" v-model="formData.submission_time" clearable type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择提交时间">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="方案内容" prop="solution_content">
                    <el-input v-model="formData.solution_content" clearable placeholder="请输入方案内容" />
                </el-form-item>
                <el-form-item label="客户反馈" prop="customer_feedback">
                    <el-input v-model="formData.customer_feedback" clearable placeholder="请输入客户反馈" />
                </el-form-item>
                <el-form-item label="附件" prop="annex">
                    <el-input v-model="formData.annex" clearable placeholder="请输入附件" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="customerDemandSolutionEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiCustomerDemandSolutionAdd, apiCustomerDemandSolutionEdit, apiCustomerDemandSolutionDetail } from '@/api/customer_demand_solution'
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: '',
    org_id: '',
    department_id: '',
    project_id: '',
    customer_demand_id: '',
    theme: '',
    submission_time: '',
    solution_content: '',
    customer_feedback: '',
    annex: '',
})


// 表单验证
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.submission_time = timeFormat(formData.submission_time, 'yyyy-mm-dd hh:MM:ss')
}

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


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