<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="80%" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="auto" :rules="formRules">
                <el-row :gutter="10">
                    <el-col :span="12">
                        <el-form-item label="客户名称" prop="custom_id">
                            <el-input v-model="formData.custom_id" clearable placeholder="请选择客户" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="项目类型" prop="project_type">
                            <el-select class="flex-1" v-model="formData.project_type" clearable placeholder="请选择项目类型">
                                <el-option v-for="(item, index) in dictData.project_type" :key="index" :label="item.name"
                                    :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="项目名称" prop="name">
                            <el-input v-model="formData.name" clearable placeholder="请输入项目名称" />
                        </el-form-item>
                    </el-col> <el-col :span="12">
                        <el-form-item label="项目编码" prop="project_code">
                            <el-input v-model="formData.project_code" clearable placeholder="请输入项目编码" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="项目内容" prop="project_content">
                            <el-checkbox-group v-model="formData.project_content" placeholder="请选择项目内容">
                                <el-checkbox v-for="(item, index) in dictData.project_content" :key="index"
                                    :label="item.value">
                                    {{ item.name }}
                                </el-checkbox>
                            </el-checkbox-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="项目估算" prop="project_estimation">
                            <el-input v-model="formData.project_estimation" clearable placeholder="请输入项目估算" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="项目所在地" prop="project_address">
                            <el-input v-model="formData.project_address" clearable placeholder="请输入项目所在地" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="预计工期" prop="estimated_construction">
                            <el-input v-model="formData.estimated_construction" clearable placeholder="请输入预计工期" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="投标时间" prop="bidding_time">
                            <el-date-picker class="flex-1 !flex" v-model="formData.bidding_time" clearable type="datetime"
                                value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择投标时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="招标方式" prop="bidding_method">
                            <el-radio-group v-model="formData.bidding_method" placeholder="请选择招标方式">
                                <el-radio v-for="(item, index) in dictData.bidding_method" :key="index"
                                    :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="联系人" prop="contacts">
                            <el-input v-model="formData.contacts" clearable placeholder="请输入联系人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="联系人职位" prop="position">
                            <el-input v-model="formData.position" clearable placeholder="请输入联系人职位" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="联系电话" prop="telephone">
                            <el-input v-model="formData.telephone" clearable placeholder="请输入联系电话" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="联系人部门" prop="department">
                            <el-input v-model="formData.department" clearable placeholder="请输入联系人部门" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="负责人" prop="person">
                            <el-input v-model="formData.person" clearable placeholder="请输入负责人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="关系度" prop="relationship">
                            <el-radio-group v-model="formData.relationship" placeholder="请选择关系度">
                                <el-radio v-for="(item, index) in dictData.relationship" :key="index"
                                    :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="发现时间" prop="discovery_time">
                            <el-date-picker class="flex-1 !flex" v-model="formData.discovery_time" clearable type="datetime"
                                value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择发现时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="信息来源" prop="information_sources">
                            <el-select class="flex-1" v-model="formData.information_sources" clearable
                                placeholder="请选择信息来源">
                                <el-option v-for="(item, index) in dictData.information_sources" :key="index"
                                    :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="竞争对手" prop="competitor">
                            <el-input v-model="formData.competitor" clearable placeholder="请输入竞争对手" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="项目建设资金来源" prop="construction_funds_sources">
                            <el-radio-group v-model="formData.construction_funds_sources" placeholder="请选择项目建设资金来源">
                                <el-radio v-for="(item, index) in dictData.construction_funds_sources" :key="index"
                                    :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="建设方付款方式" prop="construction_payment_method">
                            <el-input v-model="formData.construction_payment_method" clearable placeholder="请输入建设方付款方式" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="建设方财务状况" prop="construction_financial_status">
                            <el-radio-group v-model="formData.construction_financial_status" placeholder="请选择建设方财务状况">
                                <el-radio v-for="(item, index) in dictData.construction_financial_status" :key="index"
                                    :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="建设方对我方认可度" prop="construction_recognition">
                            <el-radio-group v-model="formData.construction_recognition" placeholder="请选择建设方对我方认可度">
                                <el-radio v-for="(item, index) in dictData.construction_recognition" :key="index"
                                    :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="我方对建设方认可度" prop="my_construction_recognition">
                            <el-radio-group v-model="formData.my_construction_recognition" placeholder="请选择我方对建设方认可度">
                                <el-radio v-for="(item, index) in dictData.my_construction_recognition" :key="index"
                                    :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="战略意义" prop="strategic_significance">
                            <el-radio-group v-model="formData.strategic_significance" placeholder="请选择战略意义">
                                <el-radio v-for="(item, index) in dictData.strategic_significance" :key="index"
                                    :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="所属行业" prop="industry">
                            <el-radio-group v-model="formData.industry" placeholder="请选择所属行业">
                                <el-radio v-for="(item, index) in dictData.industry" :key="index"
                                    :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="单位性质" prop="unit_nature">
                            <el-radio-group v-model="formData.unit_nature" placeholder="请选择单位性质">
                                <el-radio v-for="(item, index) in dictData.unit_nature" :key="index"
                                    :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="附件" prop="annex">
                            <el-input v-model="formData.annex" clearable placeholder="请输入附件" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="projectEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiProjectAdd, apiProjectEdit, apiProjectDetail } from '@/api/project'
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: '',
    name: '',
    custom_id: '',
    project_type: '',
    project_code: '',
    project_content: [],
    project_estimation: '',
    project_address: '',
    estimated_construction: '',
    bidding_time: '',
    bidding_method: '',
    contacts: '',
    position: '',
    telephone: '',
    department: '',
    person: '',
    relationship: '',
    discovery_time: '',
    information_sources: '',
    competitor: '',
    construction_funds_sources: '',
    construction_payment_method: '',
    construction_financial_status: '',
    construction_recognition: '',
    my_construction_recognition: '',
    strategic_significance: '',
    industry: '',
    unit_nature: '',
    annex: '',
    status: '',
})


// 表单验证
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
    data.project_content && (formData.project_content = String(data.project_content).split(","))
    //@ts-ignore
    formData.bidding_time = timeFormat(formData.bidding_time, 'yyyy-mm-dd hh:MM:ss')
    //@ts-ignore
    formData.discovery_time = timeFormat(formData.discovery_time, 'yyyy-mm-dd hh:MM:ss')
}

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


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