<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="客户需求id" prop="customer_demand_id">
                    <el-input v-model="formData.customer_demand_id" clearable placeholder="请输入客户需求id" />
                </el-form-item>
                <el-form-item label="竞争对手名称" prop="competitor_name">
                    <el-input v-model="formData.competitor_name" clearable placeholder="请输入竞争对手名称" />
                </el-form-item>
                <el-form-item label="竞争对手联系人" prop="competitor_contacts">
                    <el-input v-model="formData.competitor_contacts" clearable placeholder="请输入竞争对手联系人" />
                </el-form-item>
                <el-form-item label="联系人电话" prop="competitor_contacts_phone">
                    <el-input v-model="formData.competitor_contacts_phone" clearable placeholder="请输入联系人电话" />
                </el-form-item>
                <el-form-item label="竞争能力" prop="competitive_power">
                    <el-input v-model="formData.competitive_power" clearable placeholder="请输入竞争能力" />
                </el-form-item>
                <el-form-item label="竞争对手优势" prop="competitor_advantages">
                    <el-input v-model="formData.competitor_advantages" clearable placeholder="请输入竞争对手优势" />
                </el-form-item>
                <el-form-item label="竞争对手劣势" prop="competitor_disadvantages">
                    <el-input v-model="formData.competitor_disadvantages" clearable placeholder="请输入竞争对手劣势" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formData.remark" 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="competitorEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiCompetitorAdd, apiCompetitorEdit, apiCompetitorDetail } from '@/api/competitor'
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: '',
    competitor_name: '',
    competitor_contacts: '',
    competitor_contacts_phone: '',
    competitive_power: '',
    competitor_advantages: '',
    competitor_disadvantages: '',
    remark: '',
    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]
        }
    }


}

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


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