<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="" prop="project_id">
                    <el-input v-model="formData.project_id" clearable placeholder="请输入" />
                </el-form-item>
                <el-form-item label="项目估算" prop="project_estimation">
                    <el-input v-model="formData.project_estimation" clearable placeholder="请输入项目估算" />
                </el-form-item>
                <el-form-item label="招标项目资金来源" prop="bidding_project_fund_source">
                    <el-radio-group v-model="formData.bidding_project_fund_source" 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-form-item label="投标时间" prop="bidding_time">
                    <el-input v-model="formData.bidding_time" clearable placeholder="请输入投标时间" />
                </el-form-item>
                <el-form-item label="购买标书时间" prop="buy_bid_document_date">
                    <el-input v-model="formData.buy_bid_document_date" clearable placeholder="请输入购买标书时间" />
                </el-form-item>
                <el-form-item label="招标方式" prop="bid_type">
                    <el-radio-group v-model="formData.bid_type" 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-form-item label="竞争对手" prop="competitor">
                    <el-input v-model="formData.competitor" clearable placeholder="请输入竞争对手" />
                </el-form-item>
                <el-form-item label="是否需要保证金" prop="is_margin">
                    <el-radio-group v-model="formData.is_margin" placeholder="请选择是否需要保证金">
                        <el-radio 
                            v-for="(item, index) in dictData.whether_status"
                            :key="index"
                            :label="parseInt(item.value)"
                        >
                            {{ item.name }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="保证金金额" prop="margin_amount">
                    <el-input v-model="formData.margin_amount" clearable placeholder="请输入保证金金额" />
                </el-form-item>
                <el-form-item label="开标日期" prop="bid_opening_date">
                    <el-input v-model="formData.bid_opening_date" clearable placeholder="请输入开标日期" />
                </el-form-item>
                <el-form-item label="保证金退还时间" prop="margin_amount_return_date">
                    <el-input v-model="formData.margin_amount_return_date" clearable placeholder="请输入保证金退还时间" />
                </el-form-item>
                <el-form-item label="有无内部资源" prop="is_internal_resources">
                    <el-radio-group v-model="formData.is_internal_resources" placeholder="请选择有无内部资源">
                        <el-radio 
                            v-for="(item, index) in dictData.whether_status"
                            :key="index"
                            :label="parseInt(item.value)"
                        >
                            {{ item.name }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="项目把握度" prop="project_assurance">
                    <el-input v-model="formData.project_assurance" clearable placeholder="请输入项目把握度" />
                </el-form-item>
                <el-form-item label="招标项目概况" prop="bid_project_overview">
                    <el-input v-model="formData.bid_project_overview" clearable placeholder="请输入招标项目概况" />
                </el-form-item>
                <el-form-item label="项目简介" prop="project_desc">
                    <el-input v-model="formData.project_desc" 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="bidBiddingDecisionEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiBidBiddingDecisionAdd, apiBidBiddingDecisionEdit, apiBidBiddingDecisionDetail } from '@/api/bid_bidding_decision'
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: '',
    project_id: '',
    project_estimation: '',
    bidding_project_fund_source: '',
    bidding_time: '',
    buy_bid_document_date: '',
    bid_type: '',
    competitor: '',
    is_margin: '',
    margin_amount: '',
    bid_opening_date: '',
    margin_amount_return_date: '',
    is_internal_resources: '',
    project_assurance: '',
    bid_project_overview: '',
    project_desc: '',
    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 apiBidBiddingDecisionDetail({
        id: row.id
    })
    setFormData(data)
}


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