<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="auto" :rules="formRules" inline>
                <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="选择项目" />
                        </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="invite_tenders_company_name"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.invite_tenders_company_name" clearable placeholder="请输入招标公司名称" />
                        </el-form-item></el-col>
                    <el-col :span="8">
                        <el-form-item label="投标公司名称" prop="bid_company_name"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.bid_company_name" clearable placeholder="请输入投标公司名称" />
                        </el-form-item></el-col>

                    <el-col :span="8">
                        <el-form-item label="投标决策信息" prop="bid_decision_id"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]" @click="showDialog2 = true">
                            <el-input v-model="formData.bid_document_no" clearable placeholder="请选择" />
                        </el-form-item></el-col>
                    <el-col :span="8">
                        <el-form-item label="购买人员" prop="buyer" @click="userclick">
                            <el-input v-model="buyer_name" clearable placeholder="请输入购买人员" />
                        </el-form-item></el-col>
                    <el-col :span="8">
                        <el-form-item label="购买标书金额" prop="amount">
                            <el-input v-model="formData.amount" clearable placeholder="请输入购买标书金额" />
                        </el-form-item></el-col>
                    <!-- <el-col :span="24">
                        <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-col><el-col :span="8">
                        <el-form-item label="投标时间" prop="bid_date">
                            <el-date-picker class="flex-1 !flex" v-model="formData.bid_date" clearable   value-format="YYYY-MM-DD " placeholder="选择投标时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col> -->
                    <el-col :span="8">
                        <el-form-item label="购买标书时间" prop="buy_date">
                            <el-date-picker class="flex-1 !flex" v-model="formData.buy_date" clearable
                                value-format="YYYY-MM-DD" placeholder="选择购买标书时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <!-- <el-col :span="24">
                        <el-form-item label="招标方式" prop="invite_tenders_type">
                            <el-radio-group v-model="formData.invite_tenders_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="bid_address">
                            <el-input v-model="formData.bid_address" clearable placeholder="请输入投标地址" />
                        </el-form-item></el-col><el-col :span="24">
                        <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-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_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="8">
                        <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>
        <personnelselector ref="personnel" @confirm="submituser" type="1"></personnelselector>

        <el-dialog v-model="showDialog" title="选择项目" width="70%">
            <projectTable @customEvent="customEvent"></projectTable>
        </el-dialog>
        <div v-if="showDialog2">
            <el-dialog v-model="showDialog2" title="选择投标决策" width="70%">
                <biddingDecisionSelector @customEvent="customEvent2" :project_id="formData.project_id">
                </biddingDecisionSelector>
            </el-dialog>
        </div>

    </div>
</template>

<script lang="ts" setup name="bidBuyBiddingDocumentEdit">
import type { FormInstance } from 'element-plus'
import reviewprocess from '@/components/reviewprocess/index.vue'
import Popup from '@/components/popup/index.vue'
import personnelselector from '@/components/personnelselector/index.vue'
import biddingDecisionSelector from '@/components/biddingDecision/index.vue'

import projectTable from "@/components/biddingDecision/index.vue"
import { apiBidBuyBiddingDocumentAdd, 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 matsname = ref('')
const project_name = ref('')
const project_code = ref('')
const personnel = ref<any>()
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const list1 = reactive([])
const list2 = reactive([])
import configs from "@/config"
const base_url = configs.baseUrl + configs.urlPrefix
import useUserStore from "@/stores/modules/user";
const showDialog = ref(false)
const showDialog2 = ref(false)
const buyer_name = ref('')
// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑购买标书' : '新增购买标书'
})

// 表单数据
const formData = reactive({
    "project_id": 0,
    "bid_decision_id": 0,
    bid_decision_code: "",
    "bid_document_no": "",
    "invite_tenders_company_name": "",
    "bid_company_name": "",
    "buyer": "",
    "amount": 0,
    "buy_date": 0,
    "bid_address": ""
})

//打开弹窗

const userclick = () => {
    // console.log(personnel.value, '1111111')
    personnel.value.open()

}
//确认
const submituser = (e: any) => {
    formData.buyer = e.id;
    buyer_name.value = e.name;
}
// 表单验证
const formRules = reactive<any>({

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

const customEvent2 = (e: any) => {
    console.log(e, 'e')
    formData.bid_document_no = e.code;
    formData.bid_decision_id = e.id;
    showDialog2.value = false;
};

// 获取详情
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
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
    // 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 apiBidBuyBiddingDocumentDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {

    await formRef.value?.validate()
    const data = { ...formData }
    // mode.value == 'edit'
    //     ? await apiBidBuyBiddingDocumentEdit(data)
    //     : 
    await apiBidBuyBiddingDocumentAdd(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>
