<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="120px" :rules="formRules">
                <el-form-item label="标书审查编码" prop="bid_document_examination_id" @click="showDialog = true">
                    <el-input v-model="bid_document_no" clearable placeholder="点击选择标书评审(可以为空)" />
                </el-form-item>
                <el-form-item label="项目名称" prop="bid_document_examination_id">
                    <el-input v-model="project_name" clearable disabled placeholder="系统自动填写" />
                </el-form-item>
                <el-form-item label="项目编码" prop="bid_document_examination_id">
                    <el-input v-model="project_code" clearable disabled placeholder="系统自动填写" />
                </el-form-item>
                <el-form-item label="客户名称" prop="bid_document_examination_id">
                    <el-input v-model="custom_name" clearable disabled placeholder="系统自动填写" />
                </el-form-item>

                <el-form-item label="是否中标" prop="is_successful">
                    <el-select v-model="formData.is_successful" clearable placeholder="请选择是否中标" class="w-[400px]">
                        <el-option v-for="(item, index) in dictData.is_successful" :key="index" :label="item.name"
                            :value="parseInt(item.value)" />
                    </el-select>
                </el-form-item>


                <el-form-item label="中标单位" prop="bidder_company">
                    <el-input v-model="formData.bidder_company" clearable placeholder="请输入中标单位" />
                </el-form-item>
                <el-form-item label="中标金额" prop="bidder_amount">
                    <el-input v-model="formData.bidder_amount" clearable placeholder="请输入中标金额" />
                </el-form-item>

                <el-form-item label="投标总结" prop="bid_summary">
                    <el-input v-model="formData.bid_summary" clearable placeholder="请输入投标总结" />
                </el-form-item>
                <el-form-item label="附件" prop="annex">

                    <el-upload
                        accept="doc, docx, xls, xlsx, ppt, pptx, pdf, txt, zip, rar, tar, jpg, png, gif, jpeg, webp, wmv, avi, mpg, mpeg, 3gp, mov, mp4, flv, f4v, rmvb, mkv"
                        class="upload-demo" :show-file-list="false" aria-hidden="true" :headers="{ Token: userStore.token }"
                        :action="base_url + '/upload/file'" :on-success="handleAvatarSuccess_four" ref="upload">
                        <el-button type="primary">
                            上传
                        </el-button>
                    </el-upload>

                    <div>
                        <div v-for="(item, index) in formDataannex" style="margin-left: 5px;display: block;">
                            <a style="margin-left: 10px; color: #4a5dff; align-self: flex-start" :href="item.uri"
                                target="_blank">{{ item.name }}</a>
                            <span style="cursor: pointer;margin-left: 5px;" @click="delFileFn(index)">x</span>
                        </div>
                    </div>


                </el-form-item>
            </el-form>
        </popup>
        <el-dialog v-model="showDialog" title="选择标书审查" width="70%">
            <biddocumentTable @customEvent="customEvent"></biddocumentTable>

        </el-dialog>

    </div>
</template>

<script lang="ts" setup name="bidResultEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import biddocumentTable from "@/components/document_examination/index.vue"
import { apiBidResultAdd, apiBidResultEdit, apiBidResultDetail } from '@/api/bid_result'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import configs from "@/config"
import useUserStore from "@/stores/modules/user";

const base_url = configs.baseUrl + configs.urlPrefix
const userStore = useUserStore();
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 custom_name = ref('')
const project_name = ref('')
const project_code = ref('')
const bid_document_no = ref('')
const formDataannex = reactive([])
const showDialog = ref(false)
const list1 = reactive([])
const list2 = reactive([])
// 上传文件
const handleAvatarSuccess_four = (
    response,
    uploadFile
) => {
    if (response.code == 0) {
        ElMessage.error(response.msg);
        return;
    }
    formDataannex.push(
        { uri: response.data.uri, name: response.data.name }


    );
};

// 删除上传的文件
const delFileFn = (index: number) => {
    formDataannex.splice(index, 1)
}
// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑投标结果' : '新增投标结果'
})

// 表单数据
const formData = reactive({
    id: '',
    bid_document_examination_id: '',
    project_id: '',
    is_successful: '',
    bidder_company: '',
    bidder_amount: '',
    bid_summary: '',
    annex: [],
})

// 表单验证
const formRules = reactive<any>({

})

//获取值
const customEvent = (e: any) => {
    formData.bid_document_examination_id = e.id;
    bid_document_no.value = e.code
    custom_name.value = e.custom_name
    project_name.value = e.project_name
    project_code.value = e.project_code

    showDialog.value = false


};


// 获取详情
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]
        }
    }
    if (data.dept_id) {
        getlist1(data.org_id)
    }
    bid_document_no.value = data.bid_document_examination_code
    custom_name.value = data.custom_name
    project_name.value = data.project_name
    project_code.value = data.project_code
    if (data.annex && data.annex.length > 0) {

        const arry1 = data.annex.map((item: any, index: any) => {
            return {
                name: `文件${index + 1}`,
                uri: item
            };
        });
        Object.assign(formDataannex, arry1)

    }

}

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


// 提交按钮
const handleSubmit = async () => {
    if (formDataannex.length > 0) {
        formData.annex = formDataannex.map((item: any) => item.uri)
    }

    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiBidResultEdit(data)
        : await apiBidResultAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
    getlist()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}



defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
