<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">
                <el-row :gutter="10">
                    <el-col :span="8">
                        <el-form-item label="投标编号" prop="bidding_decision_id" @click="showDialog = true"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="document_no" clearable placeholder="请选择投标编号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="申请人" prop="applier">
                            <el-input v-model="formData.applier" clearable disabled placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="项目名称" prop="contract_id">
                            <el-input v-model="project_name" clearable disabled placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目名称" prop="contract_id">
                            <el-input v-model="project_name" clearable disabled placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目编码" prop="contract_id">
                            <el-input v-model="project_code" clearable disabled placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="投标时间" prop="bidding_time">
                            <el-date-picker class="flex-1 !flex" v-model="bidding_time" clearable type="date"
                                value-format="YYYY-MM-DD" disabled placeholder="系统自动填写">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="预计退还时间" prop="refund_date">
                            <el-date-picker class="flex-1 !flex" v-model="formData.refund_date" clearable type="date"
                                value-format="YYYY-MM-DD" placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="保证金金额" prop="refund_amount">
                            <el-input v-model="refund_amount" clearable disabled placeholder="系统自动填写"
                                @input="amountinput" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="金额大写" prop="refund_amount_daxie">
                            <el-input v-model="refund_amount_daxie" readonly clearable disabled placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="付款方式" prop="pay_type">
                            <el-radio-group v-model="formData.pay_type" placeholder="请选择付款方式">
                                <el-radio v-for="(item, index) in dictData.pay_type" :key="index"
                                    :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item> </el-col>


                    <el-col :span="12">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="formData.remark" type="textarea" 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>
                <div style="margin: 20px 0;">收款账户信息</div>

                <el-row :gutter="10">

                    <el-col :span="8">
                        <el-form-item label="账号" prop="account" @click="showDialog2 = true"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.account" clearable placeholder="请输入账号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开户银行" prop="deposit_bank"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.deposit_bank" clearable placeholder="请输入开户银行" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开户名称" prop="account_name"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.account_name" clearable placeholder="请输入开户名称" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-dialog v-model="showDialog" title="选择投标决策" width="70%">
                    <biddingDecisionDialog @customEvent="customEvent" :project_id="project?.id || ''">
                    </biddingDecisionDialog>
                </el-dialog>
                <!-- <el-dialog v-model="showDialog3" title="选择收款人信息" width="70%">
                    <bankaccountDialog @customEvent="customEvent3"></bankaccountDialog>
                </el-dialog> -->
            </el-form>
        </popup>

    </div>
</template>

<script lang="ts" setup name="projectEdit">
import customDialog from '@/components/custom-dialog/index.vue'
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import biddingDecisionDialog from '@/components/biddingDecision/index.vue'
import { bidapplyAdd, bidapplyEdit, bidapplyDetail } from '@/api/bidbbond'
import { toChinesNum } from "@/utils/util";
import configs from "@/config"
import useUserStore from "@/stores/modules/user";
import { useDictData } from '@/hooks/useDictOptions'
const { dictData } = useDictData('pay_type')

const base_url = configs.baseUrl + configs.urlPrefix
const userStore = useUserStore();
const active = reactive(0)


const formDataannex = reactive([])
const project_name = ref('')
const bidding_time = ref('')
const project_code = ref('')
const document_no = ref('')
const userInfo = userStore.userInfo
// 上传文件
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 emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const refund_amount = ref('')
const refund_amount_daxie = ref('')
const showDialog = ref(false)
const showDialog3 = ref(false)

const customEvent = (e: any) => {
    formData.bidding_decision_id = e.id;
    document_no.value = e.code;
    project_name.value = e.project_name
    project_code.value = e.project_code
    bidding_time.value = e.bidding_time;
    refund_amount.value = e.margin_amount
    showDialog.value = false;
    amountinput(e.margin_amount)
};

const props = defineProps({
    project: Object
})

if (props.project) {
    project_name.value = props.project.name
    project_code.value = props.project.project_code
}

//监听输入
const amountinput = (e) => {
    // console.log(e)
    if (e && e > 0) {
        refund_amount_daxie.value = toChinesNum(e)
    }
}
// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑投标保证金表' : '新增投标保证金表'
})

// 表单数据
const formData = reactive({
    id: '',
    "bidding_decision_id": '',
    "applier": "",
    "pay_type": '',
    "refund_date": "",
    "remark": "",
    "annex": "",
    "deposit_bank": "",
    "account_name": "",
    "account": ""
})


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

})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    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]
        }
    }

    project_name.value = data.project_name
    project_code.value = data.project_code
    bidding_time.value = data.bidding_time
    document_no.value = data.security_apply_code

    refund_amount.value = data.margin_amount
    amountinput(data.decision.margin_amount)




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


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

    console.log(formRef.value?.validate(), '22222222')
    await formRef.value?.validate()


    const data = { ...formData }
    mode.value == 'edit'
        ? await bidapplyEdit(data)
        : await bidapplyAdd(data)
    popupRef.value?.close()
    emit('success')
}

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


}

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


formData.applier = userInfo.name
defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
