<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="approve_id">
                    <el-input v-model="formData.approve_id" clearable placeholder="请输入审批id" />
                </el-form-item>
                <el-form-item label="购买标书id" prop="buy_bidding_document_id">
                    <el-input v-model="formData.buy_bidding_document_id" clearable placeholder="请输入购买标书id" />
                </el-form-item>
                <el-form-item label="是否需要保证金" prop="is_need_deposit">
                    <el-radio-group v-model="formData.is_need_deposit" 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="bid_opening_date">
                    <el-date-picker 
                        class="flex-1 !flex"
                        v-model="formData.bid_opening_date"
                        clearable
                        type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        placeholder="选择开标日期">
                    </el-date-picker>
                </el-form-item>
                
                <el-form-item label="保证金退还时间" prop="deposit_refund_time">
                    <el-date-picker 
                        class="flex-1 !flex"
                        v-model="formData.deposit_refund_time"
                        clearable
                        type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        placeholder="选择保证金退还时间">
                    </el-date-picker>
                </el-form-item>
                
                <el-form-item label="招标项目概况" prop="bidding_project_overview">
                    <el-input v-model="formData.bidding_project_overview" clearable placeholder="请输入招标项目概况" />
                </el-form-item>
                <el-form-item label="项目简介" prop="project_introduction">
                    <el-input v-model="formData.project_introduction" clearable placeholder="请输入项目简介" />
                </el-form-item>
                <el-form-item label="附件" prop="annex">
                    <el-input v-model="formData.annex" clearable placeholder="请输入附件" />
                </el-form-item>
                <el-form-item label="技术协议偏差" prop="technical_protocol_deviation">
                    <el-input v-model="formData.technical_protocol_deviation" clearable placeholder="请输入技术协议偏差" />
                </el-form-item>
                <el-form-item label="协议偏差处理方案" prop="protocol_deviation_handling_plan">
                    <el-input v-model="formData.protocol_deviation_handling_plan" clearable placeholder="请输入协议偏差处理方案" />
                </el-form-item>
                <el-form-item label="技术审查附件" prop="technical_review_annex">
                    <el-input v-model="formData.technical_review_annex" clearable placeholder="请输入技术审查附件" />
                </el-form-item>
                <el-form-item label="总金额" prop="business_review_total_amount">
                    <el-input v-model="formData.business_review_total_amount" clearable placeholder="请输入总金额" />
                </el-form-item>
                <el-form-item label="税率" prop="tax_rate">
                    <el-select class="flex-1" v-model="formData.tax_rate" clearable placeholder="请选择税率">
                        <el-option 
                            v-for="(item, index) in dictData.tax_rate"
                            :key="index" 
                            :label="item.name"
                            :value="parseInt(item.value)"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="付款方式" prop="pay_type">
                    <el-select class="flex-1" v-model="formData.pay_type" clearable placeholder="请选择付款方式">
                        <el-option 
                            v-for="(item, index) in dictData.pay_type"
                            :key="index" 
                            :label="item.name"
                            :value="parseInt(item.value)"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="付款比例" prop="pay_rate">
                    <el-input v-model="formData.pay_rate" clearable placeholder="请输入付款比例" />
                </el-form-item>
                <el-form-item label="商务合同偏差" prop="business_contract_deviation">
                    <el-input v-model="formData.business_contract_deviation" clearable placeholder="请输入商务合同偏差" />
                </el-form-item>
                <el-form-item label="偏差处理方案" prop="business_contract_deviation_handling_plan">
                    <el-input v-model="formData.business_contract_deviation_handling_plan" clearable placeholder="请输入偏差处理方案" />
                </el-form-item>
                <el-form-item label="商务审查附件" prop="business_contract_deviation_annex">
                    <el-input v-model="formData.business_contract_deviation_annex" clearable placeholder="请输入商务审查附件" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="bidDocumentExaminationEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiBidDocumentExaminationAdd, apiBidDocumentExaminationEdit, apiBidDocumentExaminationDetail } from '@/api/bid_document_examination'
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: '',
    approve_id: '',
    buy_bidding_document_id: '',
    is_need_deposit: '',
    bid_opening_date: '',
    deposit_refund_time: '',
    bidding_project_overview: '',
    project_introduction: '',
    annex: '',
    technical_protocol_deviation: '',
    protocol_deviation_handling_plan: '',
    technical_review_annex: '',
    business_review_total_amount: '',
    tax_rate: '',
    pay_type: '',
    pay_rate: '',
    business_contract_deviation: '',
    business_contract_deviation_handling_plan: '',
    business_contract_deviation_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.bid_opening_date = timeFormat(formData.bid_opening_date,'yyyy-mm-dd hh:MM:ss') 
    //@ts-ignore
    formData.deposit_refund_time = timeFormat(formData.deposit_refund_time,'yyyy-mm-dd hh:MM:ss') 
}

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


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