<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="project_id">
                    <el-input v-model="formData.project_id" clearable placeholder="请输入项目id" />
                </el-form-item>
                <el-form-item label="标书编号" prop="bid_document_no">
                    <el-input v-model="formData.bid_document_no" clearable placeholder="请输入标书编号" />
                </el-form-item>
                <el-form-item label="招标公司名称" prop="invite_tenders_company_name">
                    <el-input v-model="formData.invite_tenders_company_name" clearable placeholder="请输入招标公司名称" />
                </el-form-item>
                <el-form-item label="投标公司名称" prop="bid_company_name">
                    <el-input v-model="formData.bid_company_name" clearable placeholder="请输入投标公司名称" />
                </el-form-item>
                <el-form-item label="购买人员" prop="buyer">
                    <el-input v-model="formData.buyer" clearable placeholder="请输入购买人员" />
                </el-form-item>
                <el-form-item label="购买标书金额" prop="amount">
                    <el-input v-model="formData.amount" clearable placeholder="请输入购买标书金额" />
                </el-form-item>
                <el-form-item label="招标项目资金来源" prop="project_fund_source">
                    <el-radio-group v-model="formData.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="bid_date">
                    <el-date-picker 
                        class="flex-1 !flex"
                        v-model="formData.bid_date"
                        clearable
                        type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        placeholder="选择投标时间">
                    </el-date-picker>
                </el-form-item>
                
                <el-form-item label="购买标书时间" prop="buy_date">
                    <el-date-picker 
                        class="flex-1 !flex"
                        v-model="formData.buy_date"
                        clearable
                        type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        placeholder="选择购买标书时间">
                    </el-date-picker>
                </el-form-item>
                
                <el-form-item label="招标方式" prop="invite_tenders_type">
                    <el-checkbox-group v-model="formData.invite_tenders_type" placeholder="请选择招标方式">
                        <el-checkbox 
                            v-for="(item, index) in dictData.bidding_method"
                            :key="index" 
                            :label="item.value"
                        >
                            {{ item.name }}
                        </el-checkbox>
                    </el-checkbox-group>
                </el-form-item>
                <el-form-item label="投标地址" prop="bid_address">
                    <el-input v-model="formData.bid_address" 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_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="bidBuyBiddingDocumentEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiBidBuyBiddingDocumentAdd, apiBidBuyBiddingDocumentEdit, apiBidBuyBiddingDocumentDetail } from '@/api/bid_buy_bidding_document'
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: '',
    bid_document_no: '',
    invite_tenders_company_name: '',
    bid_company_name: '',
    buyer: '',
    amount: '',
    project_fund_source: '',
    bid_date: '',
    buy_date: '',
    invite_tenders_type: [],
    bid_address: '',
    is_margin: '',
    margin_amount: '',
    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]
        }
    }
    //@ts-ignore
    data.invite_tenders_type && (formData.invite_tenders_type = String(data.invite_tenders_type).split(","))
    //@ts-ignore
    formData.bid_date = timeFormat(formData.bid_date,'yyyy-mm-dd hh:MM:ss') 
    //@ts-ignore
    formData.buy_date = timeFormat(formData.buy_date,'yyyy-mm-dd hh:MM:ss') 
}

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


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