<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-select class="flex-1" v-model="formData.department_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="project_id">
                    <el-input v-model="formData.project_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="supplier">
                    <el-input v-model="formData.supplier" clearable placeholder="请输入需求提供人" />
                </el-form-item>
                <el-form-item label="提供人联系方式" prop="supplier_contacts">
                    <el-input v-model="formData.supplier_contacts" clearable placeholder="请输入提供人联系方式" />
                </el-form-item>
                <el-form-item label="重要程度1非常重要2重要3一般4不重要" prop="importance">
                    <el-radio-group v-model="formData.importance" placeholder="请选择重要程度1非常重要2重要3一般4不重要">
                        <el-radio v-for="(item, index) in dictData.importance" :key="index" :label="parseInt(item.value)">
                            {{ item.name }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="记录时间" prop="recording_time">
                    <el-date-picker class="flex-1 !flex" v-model="formData.recording_time" clearable type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择记录时间">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="需求内容" prop="demand_content">
                    <el-input v-model="formData.demand_content" 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="customerDemandEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiCustomerDemandAdd, apiCustomerDemandEdit, apiCustomerDemandDetail } from '@/api/customer_demand'
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: '',
    theme: '',
    supplier: '',
    supplier_contacts: '',
    importance: '',
    recording_time: '',
    demand_content: '',
    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.recording_time = timeFormat(formData.recording_time, 'yyyy-mm-dd hh:MM:ss')
}

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


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