<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="120px" :rules="formRules">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="项目名称" prop="project_id"
                            :rules="[{ required: true, message: '不可为空', trigger: 'change' }]" @click="showDialog = true">
                            <el-input v-model="project_name" readonly clearable placeholder="选择项目" :disabled="project" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目编码" prop="project_id"
                            :rules="[{ required: true, message: '不可为空', trigger: 'change' }]">
                            <el-input v-model="project_code" clearable disabled placeholder="系统自动填写" />
                        </el-form-item></el-col>
                    <el-col :span="8">
                        <el-form-item label="客户名称" prop="project_id"
                            :rules="[{ required: true, message: '不可为空', trigger: 'change' }]">
                            <el-input v-model="matsname" clearable disabled placeholder="系统自动填写" />
                        </el-form-item></el-col><el-col :span="8">
                        <el-form-item label="项目估算" prop="project_estimation"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.project_estimation" clearable placeholder="请输入项目估算" type="number" />
                        </el-form-item></el-col><el-col :span="24">
                        <el-form-item label="招标项目资金来源" label-width="130px" 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-col><el-col :span="8">
                        <el-form-item label="投标时间" prop="bidding_time">
                            <!-- <el-input v-model="formData.bidding_time" clearable placeholder="请输入投标时间" />
                     -->
                            <el-date-picker v-model="formData.bidding_time" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
                                placeholder="请选择投标时间" clearable></el-date-picker>
                        </el-form-item></el-col><el-col :span="8">
                        <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-col><el-col :span="8">
                        <el-form-item label="竞争对手" prop="competitor">
                            <el-input v-model="formData.competitor" clearable placeholder="请输入竞争对手" />
                        </el-form-item></el-col><el-col :span="24">
                        <el-form-item label="是否需要保证金" prop="is_margin"
                            :rules="[{ required: true, message: '不可为空', trigger: 'change' }]">
                            <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-col><el-col :span="8">
                        <el-form-item label="保证金金额" prop="margin_amount">
                            <el-input v-model="formData.margin_amount" clearable placeholder="请输入保证金金额" />
                        </el-form-item></el-col><el-col :span="8">
                        <el-form-item label="开标日期" prop="bid_opening_date">
                            <!-- <el-input v-model="formData.bid_opening_date" clearable placeholder="请输入开标日期" />
                     -->
                            <el-date-picker v-model="formData.bid_opening_date" format="YYYY-MM-DD"
                                value-format="YYYY-MM-DD" placeholder="请输入开标日期" clearable></el-date-picker>
                        </el-form-item></el-col><el-col :span="8">
                        <el-form-item label="保证金退还时间" prop="margin_amount_return_date">
                            <!-- <el-input v-model="formData.margin_amount_return_date" clearable placeholder="请输入保证金退还时间" /> -->
                            <el-date-picker v-model="formData.margin_amount_return_date" format="YYYY-MM-DD"
                                value-format="YYYY-MM-DD" placeholder="请输入保证金退还时间" clearable></el-date-picker>
                        </el-form-item></el-col><el-col :span="24">
                        <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-col>


                    <el-col :span="8">
                        <el-form-item label="项目把握度" label-width="130px" prop="project_assurance">

                            <el-select v-model="formData.project_assurance" clearable placeholder="请选择项目把握度">
                                <el-option v-for="(item, index) in dictData.project_assurance" :key="index"
                                    :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item></el-col>

                    <el-col :span="8">
                        <el-form-item label="招标项目概况" prop="bid_project_overview">
                            <el-input v-model="formData.bid_project_overview" clearable placeholder="请输入招标项目概况" />
                        </el-form-item></el-col><el-col :span="8">
                        <el-form-item label="项目简介" prop="project_desc">
                            <el-input v-model="formData.project_desc" clearable placeholder="请输入项目简介" />
                        </el-form-item></el-col><el-col :span="24">
                        <el-form-item label="附件" prop="field127">
                            <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-col>
                </el-row>
            </el-form>
            <reviewprocess />
        </popup>
        <el-dialog v-model="showDialog" title="选择项目" width="70%">
            <projectTable @customEvent="customEvent"></projectTable>

        </el-dialog>
    </div>
</template>

<script lang="ts" setup name="bidBiddingDecisionEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import reviewprocess from '@/components/reviewprocess/index.vue'
import projectTable from "@/components/project/index.vue"
import { useDictData } from '@/hooks/useDictOptions'
import { apiBidBiddingDecisionAdd, apiBidBiddingDecisionDetail } from '@/api/bid_bidding_decision'
let props = defineProps({
    project: Object
})
const { dictData } = useDictData('construction_funds_sources,bidding_method,whether_status,project_assurance')

const matsname = ref('')
const project_name = ref('')
const project_code = ref('')
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const showDialog = ref(false)
const mode = ref('')
import configs from "@/config"
const base_url = configs.baseUrl + configs.urlPrefix
import useUserStore from "@/stores/modules/user";
const userStore = useUserStore();
const formDataannex = 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: '',
    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: [],
    approve_id: 0
})

//获取值
const customEvent = (e: any) => {
    formData.project_id = e.id;
    matsname.value = e.person
    project_name.value = e.name
    project_code.value = e.project_code
    showDialog.value = false;
};

if (props.project) customEvent(props.project)
// 表单验证
const formRules = reactive<any>({

})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    matsname.value = data.project.person
    project_name.value = data.project.name
    project_code.value = data.project.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)

    }
    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 () => {
    if (formDataannex.length > 0) {
        formData.annex = formDataannex.map((item: any) => item.uri)
    }
    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()
    getlist()
}

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



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