<template>
    <view class="establish-acceptor-container">
        <form class="acceptor-form" @submit.prevent="submitForm">
            <!-- 基本信息 -->
            <view class="form-section">
                <!-- <view class="form-item">
                    <label class="form-label required">商户ID</label>
                    <input type="number" class="form-input" v-model.number="formData.mch_id" placeholder="请输入商户ID"
                        required>
                </view> -->

                <view class="form-item">
                    <label class="form-label required">商户注册名称</label>
                    <input type="text" class="form-input" v-model="formData.merRegName"
                        placeholder="请输入商户注册名称，长度不小于4个汉字，8~40字符" required>
                </view>

                <view class="form-item">
                    <label class="form-label required">商户地区代码</label>
                    <input type="text" class="form-input" v-model="formData.merRegDistCode"
                        placeholder="请输入商户地区代码" required>
                </view>

                <view class="form-item">
                    <label class="form-label required">商户详细地址</label>
                    <input type="text" class="form-input" v-model="formData.merRegAddr"
                        placeholder="请输入去除省、市、区后的详细地址" required>
                </view>

                <view class="form-item">
                    <label class="form-label required">商户MCC编号</label>
                    <input type="text" class="form-input" v-model="formData.mccCode"
                        placeholder="请输入商户MCC编号" required>
                </view>

                <view class="form-item">
                    <label class="form-label">营业执照名称</label>
                    <input type="text" class="form-input" v-model="formData.merBlisName"
                        placeholder="请输入营业执照名称，小微商户可不传">
                </view>

                <view class="form-item">
                    <label class="form-label">营业执照号</label>
                    <input type="text" class="form-input" v-model="formData.merBlis" placeholder="请输入营业执照号，小微商户可不传">
                </view>

                <view class="form-item">
                    <label class="form-label">营业执照开始日期</label>
                    <input type="date" class="form-input" v-model="formData.merBlisStDt" placeholder="请选择营业执照开始日期">
                </view>

                <view class="form-item">
                    <label class="form-label">营业执照有效期</label>
                    <input type="date" class="form-input" v-model="formData.merBlisExpDt" placeholder="请选择营业执照有效期">
                </view>

                <view class="form-item">
                    <label class="form-label required">商户经营内容</label>
                    <input type="text" class="form-input" v-model="formData.merBusiContent"
                        placeholder="请输入商户经营内容" required>
                </view>
            </view>

            <!-- 法人信息 -->
            <view class="form-section">
                <view class="form-item">
                    <label class="form-label required">商户法人姓名</label>
                    <input type="text" class="form-input" v-model="formData.larName" placeholder="请输入商户法人姓名" required>
                </view>

                <view class="form-item">
                    <label class="form-label required">法人证件类型</label>
                    <view class="popup-select" @click="showIdTypePopup = true">
                        <span>{{ selectedIdTypeLabel || '请选择法人证件类型' }}</span>
                    </view>
                </view>

                <view class="form-item">
                    <label class="form-label required">法人身份证号码</label>
                    <input type="text" class="form-input" v-model="formData.larIdcard" placeholder="请输入法人身份证号码"
                        required>
                </view>

                <view class="form-item">
                    <label class="form-label required">法人身份证开始日期</label>
                    <input type="date" class="form-input" v-model="formData.larIdcardStDt" placeholder="请选择法人身份证开始日期"
                        required>
                </view>

                <view class="form-item">
                    <label class="form-label required">法人身份证有效期</label>
                    <input type="date" class="form-input" v-model="formData.larIdcardExpDt" placeholder="请选择法人身份证有效期"
                        required>
                </view>
            </view>

            <!-- 联系人信息 -->
            <view class="form-section">
                <view class="form-item">
                    <label class="form-label required">商户联系人姓名</label>
                    <input type="text" class="form-input" v-model="formData.merContactName" placeholder="请输入商户联系人姓名"
                        required>
                </view>

                <view class="form-item">
                    <label class="form-label required">商户联系人手机号码</label>
                    <input type="number" class="form-input" v-model="formData.merContactMobile"
                        placeholder="请输入商户联系人手机号码" required>
                </view>
            </view>

            <!-- 结算账户信息 -->
            <view class="form-section">
                <view class="form-item">
                    <label class="form-label required">结算账户开户行号</label>
                    <input type="text" class="form-input" v-model="formData.openningBankCode" placeholder="请输入结算账户开户行号"
                        required>
                </view>

                <view class="form-item">
                    <label class="form-label required">结算账户开户行名称</label>
                    <input type="text" class="form-input" v-model="formData.openningBankName" placeholder="请输入结算账户开户行名称"
                        required>
                </view>

                <view class="form-item">
                    <label class="form-label required">结算账户清算行号</label>
                    <input type="text" class="form-input" v-model="formData.clearingBankCode" placeholder="请输入结算账户清算行号"
                        required>
                </view>

                <view class="form-item">
                    <label class="form-label required">结算账户账号</label>
                    <input type="text" class="form-input" v-model="formData.acctNo" placeholder="请输入结算账户账号" required>
                </view>

                <view class="form-item">
                    <label class="form-label required">结算账户名称</label>
                    <input type="text" class="form-input" v-model="formData.acctName" placeholder="请输入结算账户名称" required>
                </view>

                <view class="form-item">
                    <label class="form-label required">结算账户性质</label>
                    <view class="popup-select" @click="showAcctTypePopup = true">
                        <span>{{ selectedAcctTypeLabel || '请选择结算账户性质' }}</span>
                    </view>
                </view>

                <!-- 费率信息 -->
                <view class="form-item">
                    <label class="form-label required">费率类型</label>
                    <input type="text" class="form-input" v-model="formData.feeData[0].feeRateTypeCode" placeholder="请输入费率类型" required>
                </view>

                <view class="form-item">
                    <label class="form-label required">费率类型名称</label>
                    <input type="text" class="form-input" v-model="formData.feeData[0].feeRateTypeName" placeholder="请输入费率类型名称" required>
                </view>

                <view class="form-item">
                    <label class="form-label required">手续费率(%)</label>
                    <input type="number" class="form-input" v-model="formData.feeData[0].feeRatePct" placeholder="请输入手续费率" step="0.01" required>
                </view>
            </view>

            <!-- 附件上传 -->
            <view class="form-section">
                <h3 class="section-title">附件上传（可选）</h3>
                <view class="attachment-section">
                    <view class="attachment-item" v-for="(file, index) in formData.fileData" :key="index">
                        <view class="form-item">
                            <label class="form-label">附件类型</label>
                            <view class="popup-select" @click="showFileAttachTypePopup = index">
                                <span>{{ getAttachTypeLabel(file.attType) || '请选择附件类型' }}</span>
                            </view>
                        </view>
                        <button class="upload-btn" @click="uploadFile(index)">上传文件</button>
                        <view v-if="file.attFileId" class="file-info">
                            <span>{{ file.attachName || '已上传文件' }}</span>
                            <button class="delete-btn" @click="removeFile(index)">删除</button>
                        </view>
                    </view>
                    <button class="add-attachment-btn" @click="addFile">添加附件</button>
                </view>
            </view>

            <!-- 提交按钮 -->
            <view class="form-section submit-section">
                <button type="button" class="submit-btn1" @click="ecQuery">结果查询</button>
                <button type="submit" class="submit-btn" @click="submitForm">提交申请</button>
            </view>
        </form>

        <!-- 地区代码弹窗 -->
        <view class="popup" v-if="showAreaPopup">
            <view class="popup-content">
                <view class="popup-title">选择商户地区代码</view>
                <view class="popup-list">
                    <view class="popup-item" v-for="area in areaOptions" :key="area.value" @click="selectArea(area)">
                        {{ area.label }}
                    </view>
                </view>
                <view class="popup-footer">
                    <button class="popup-cancel-btn" @click="showAreaPopup = false">取消</button>
                </view>
            </view>
        </view>

        <!-- MCC编号弹窗 -->
        <view class="popup" v-if="showMccPopup">
            <view class="popup-content">
                <view class="popup-title">选择商户MCC编号</view>
                <view class="popup-list">
                    <view class="popup-item" v-for="mcc in mccOptions" :key="mcc.value" @click="selectMcc(mcc)">
                        {{ mcc.label }}
                    </view>
                </view>
                <view class="popup-footer">
                    <button class="popup-cancel-btn" @click="showMccPopup = false">取消</button>
                </view>
            </view>
        </view>

        <!-- 经营内容弹窗 -->
        <view class="popup" v-if="showBusiContentPopup">
            <view class="popup-content">
                <view class="popup-title">选择商户经营内容</view>
                <view class="popup-list">
                    <view class="popup-item" v-for="content in busiContentOptions" :key="content.value"
                        @click="selectBusiContent(content)">
                        {{ content.label }}
                    </view>
                </view>
                <view class="popup-footer">
                    <button class="popup-cancel-btn" @click="showBusiContentPopup = false">取消</button>
                </view>
            </view>
        </view>

        <!-- 法人证件类型弹窗 -->
        <view class="popup" v-if="showIdTypePopup">
            <view class="popup-content">
                <view class="popup-title">选择法人证件类型</view>
                <view class="popup-list">
                    <view class="popup-item" v-for="type in idTypeOptions" :key="type.value"
                        @click="selectIdType(type)">
                        {{ type.label }}
                    </view>
                </view>
                <view class="popup-footer">
                    <button class="popup-cancel-btn" @click="showIdTypePopup = false">取消</button>
                </view>
            </view>
        </view>

        <!-- 结算账户性质弹窗 -->
        <view class="popup" v-if="showAcctTypePopup">
            <view class="popup-content">
                <view class="popup-title">选择结算账户性质</view>
                <view class="popup-list">
                    <view class="popup-item" v-for="type in acctTypeOptions" :key="type.value"
                        @click="selectAcctType(type)">
                        {{ type.label }}
                    </view>
                </view>
                <view class="popup-footer">
                    <button class="popup-cancel-btn" @click="showAcctTypePopup = false">取消</button>
                </view>
            </view>
        </view>

        <!-- 文件附件类型弹窗 -->
        <view class="popup" v-if="showFileAttachTypePopup >= 0">
            <view class="popup-content">
                <view class="popup-title">选择附件类型</view>
                <view class="popup-list">
                    <view class="popup-item" v-for="type in fileAttachTypeOptions" :key="type.value"
                        @click="selectFileAttachType(showFileAttachTypePopup, type)">
                        {{ type.label }}
                    </view>
                </view>
                <view class="popup-footer">
                    <button class="popup-cancel-btn" @click="showFileAttachTypePopup = -1">取消</button>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
import { picUrl, menuButtonInfo, request, NavgateTo, RequsetUrl } from "../../../utils";
import { apiArr } from "../../../api/contract";

export default {
    data() {
        return {
            // 表单数据
            formData: {
                mch_id: uni.getStorageSync('mchId'), // 商户ID
                merRegName: '', // 商户注册名称
                merRegDistCode: '', // 商户地区代码
                merRegAddr: '', // 商户详细地址
                mccCode: '', // 商户MCC编号
                merBlisName: '', // 营业执照名称
                merBlis: '', // 营业执照号
                merBlisStDt: '', // 营业执照开始日期
                merBlisExpDt: '', // 营业执照有效期
                merBusiContent: '', // 商户经营内容
                larName: '', // 商户法人姓名
                larIdType: '', // 法人证件类型
                larIdcard: '', // 法人身份证号码
                larIdcardStDt: '', // 法人身份证开始日期
                larIdcardExpDt: '', // 法人身份证有效期
                merContactMobile: '', // 商户联系人手机号码
                merContactName: '', // 商户联系人姓名
                openningBankCode: '', // 结算账户开户行号
                openningBankName: '', // 结算账户开户行名称
                clearingBankCode: '', // 结算账户清算行号
                acctNo: '', // 结算账户账号
                acctName: '', // 结算账户名称
                acctTypeCode: '', // 结算账户性质
                feeData: [
                    {
                        feeRateTypeCode: "",
                        feeRateTypeName: "",
                        feeRatePct: ""
                    }
                ], // 费率数据
                fileData: [
                    {
                        attType: '',
                        attFileId: '',
                        attachName: ''
                    }
                ] // 文件数据（可选）
            },

            // 弹窗控制
            showAreaPopup: false,
            showMccPopup: false,
            showBusiContentPopup: false,
            showIdTypePopup: false,
            showAcctTypePopup: false,
            showFileAttachTypePopup: -1,

            // 选择项标签
            selectedAreaLabel: '',
            selectedMccLabel: '',
            selectedBusiContentLabel: '',
            selectedIdTypeLabel: '',
            selectedAcctTypeLabel: '',

            // 选项数据（示例数据，实际应从API获取）
            areaOptions: [
                { value: '110101', label: '北京市东城区' },
                { value: '110102', label: '北京市西城区' },
                { value: '310101', label: '上海市黄浦区' },
                { value: '310102', label: '上海市黄浦区' }
            ],

            mccOptions: [
                { value: '5411', label: '大型超市' },
                { value: '5999', label: '其他零售商户' },
                { value: '7011', label: '住宿服务' },
                { value: '7221', label: '美容美发服务' }
            ],

            busiContentOptions: [
                { value: 'GROCERY', label: '食品饮料' },
                { value: 'CLOTHING', label: '服装鞋帽' },
                { value: 'SERVICE', label: '生活服务' },
                { value: 'ENTERTAINMENT', label: '娱乐休闲' }
            ],

            idTypeOptions: [
                { value: '01', label: '身份证' },
                { value: '02', label: '护照' },
                { value: '03', label: '港澳通行证' },
                { value: '04', label: '台胞证' },
                { value: '10', label: '外国人永久居留身份证' },
                { value: '11', label: '港澳居民居住证' },
                { value: '12', label: '台湾居民居住证' },
                { value: '13', label: '执行事务合伙人' },
                { value: '99', label: '其它证件' },
            ],

            acctTypeOptions: [
                { value: '57', label: '对公' },
                { value: '58', label: '对私' }
            ],

            fileAttachTypeOptions: [
                { value: 'FR_ID_CARD_FRONT', label: '法人身份证正面' },
                { value: 'FR_ID_CARD_BEHIND', label: '法人身份证反面' },
                { value: 'ID_CARD_FRONT', label: '结算人身份证正面' },
                { value: 'ID_CARD_BEHIND', label: '结算人身份证反面' },
                { value: 'BANK_CARD', label: '银行卡' },
                { value: 'BUSINESS_LICENCE', label: '营业执照' },
                { value: 'MERCHANT_PHOTO', label: '商户门头照' },
                { value: 'SHOPINNER', label: '商铺内部照片' },
                { value: 'XY', label: '线下纸质协议' },
                { value: 'NETWORK_XY', label: '电子协议' },
                { value: 'HT', label: '租赁合同' },
                { value: 'COOPERATION_QUALIFICATION_PROOF', label: '合作资质证明' },
                { value: 'FOOD_QUALIFICATION_PROOF', label: '食品经营相关资质' },
                { value: 'NO_LEGAL_PERSON_SETT_AUTH_LETTER', label: '非法人结算授权书' },
                { value: 'SPLIT_ENTRUST_FILE', label: '结算授权委托书' },
                { value: 'RENTAL_AGREEMENT', label: '集市方与场地方间的租赁协议' },
                { value: 'SPLIT_COOPERATION_FILE', label: '集市方与摊主间的合作协议' },
                { value: 'OTHERS', label: '其他' }
            ]
        };
    },

    methods: {
        // 添加附件
        addFile() {
            this.formData.fileData.push({
                attType: '',
                attFileId: '',
                attachName: ''
            });
        },

        // 选择商户地区代码
        selectArea(area) {
            this.formData.merRegDistCode = area.value;
            this.selectedAreaLabel = area.label;
            this.showAreaPopup = false;
        },

        // 选择商户MCC编号
        selectMcc(mcc) {
            this.formData.mccCode = mcc.value;
            this.selectedMccLabel = mcc.label;
            this.showMccPopup = false;
        },

        // 选择商户经营内容
        selectBusiContent(content) {
            this.formData.merBusiContent = content.value;
            this.selectedBusiContentLabel = content.label;
            this.showBusiContentPopup = false;
        },

        // 选择法人证件类型
        selectIdType(type) {
            this.formData.larIdType = type.value;
            this.selectedIdTypeLabel = type.label;
            this.showIdTypePopup = false;
        },

        // 选择结算账户性质
        selectAcctType(type) {
            this.formData.acctTypeCode = type.value;
            this.selectedAcctTypeLabel = type.label;
            this.showAcctTypePopup = false;
        },

        // 选择文件附件类型
        selectFileAttachType(index, type) {
            this.formData.fileData[index].attType = type.value;
            this.showFileAttachTypePopup = -1;
        },

        // 获取附件类型标签
        getAttachTypeLabel(typeValue) {
            if (!typeValue) return '';
            const option = this.fileAttachTypeOptions.find(type => type.value === typeValue);
            return option ? option.label : '';
        },

        // 删除附件
        removeFile(index) {
            this.formData.fileData.splice(index, 1);
        },

        // 上传文件
        uploadFile(index) {
            const fileData = this.formData.fileData[index];

            // 检查是否选择了附件类型
            if (!fileData.attType) {
                uni.showToast({
                    title: '请先选择附件类型',
                    icon: 'none'
                });
                return;
            }

            // 调用 uni.chooseMessageFile API 选择文件
            uni.chooseMessageFile({
                count: 1,
                type: 'file',
                extension: ['pdf', 'doc', 'docx', 'jpg', 'jpeg', 'png'], // 支持的文件类型
                success: (res) => {
                    const tempFile = res.tempFiles[0];
                    console.log("选择的文件:", tempFile);

                    // 设置文件名
                    fileData.attachName = tempFile.name;

                    // 显示上传中提示
                    uni.showLoading({
                        title: '上传中',
                        mask: true
                    });

                    // 直接使用uni.uploadFile上传文件
                    uni.uploadFile({
                        url: RequsetUrl + apiArr.upload, // 完整的上传接口URL
                        filePath: tempFile.path, // 文件路径
                        name: 'file', // 文件对应的key，根据后端要求设置
                        formData: {
                            attType: fileData.attType // 附件类型
                        },
                        header: {
                            Authorization: uni.getStorageSync("ctoken") // 添加认证信息
                        },
                        success: (uploadRes) => {
                            // 解析响应数据
                            const res = JSON.parse(uploadRes.data);

                            // 更新附件信息
                            fileData.attFileId = res.data.respData.attFileId;

                            uni.showToast({
                                title: '上传成功',
                                icon: 'success'
                            });
                        },
                        fail: (error) => {
                            console.error('文件上传失败:', error);
                            uni.showToast({
                                title: '上传失败',
                                icon: 'none'
                            });
                        },
                        complete: () => {
                            uni.hideLoading(); // 隐藏加载提示
                        }
                    });
                },
                fail: (err) => {
                    console.error('选择文件失败:', err);
                    uni.showToast({
                        title: '选择文件失败',
                        icon: 'none'
                    });
                }
            });
        },

        // 表单验证
        validateForm() {
            const { formData } = this;

            // 验证必需字段
            if (!formData.mch_id) {
                uni.showToast({ title: '请输入商户ID', icon: 'none' });
                return false;
            }

            if (!formData.merRegName) {
                uni.showToast({ title: '请输入商户注册名称', icon: 'none' });
                return false;
            } else if (formData.merRegName.length < 8 || formData.merRegName.length > 40) {
                uni.showToast({ title: '商户注册名称长度必须在8~40字符之间', icon: 'none' });
                return false;
            }

            if (!formData.merRegDistCode) {
                uni.showToast({ title: '请选择商户地区代码', icon: 'none' });
                return false;
            }

            if (!formData.merRegAddr) {
                uni.showToast({ title: '请输入商户详细地址', icon: 'none' });
                return false;
            } else if (formData.merRegAddr.length < 6 || formData.merRegAddr.length > 200) {
                uni.showToast({ title: '商户详细地址长度必须在6~200字符之间', icon: 'none' });
                return false;
            }

            if (!formData.mccCode) {
                uni.showToast({ title: '请选择商户MCC编号', icon: 'none' });
                return false;
            }

            if (!formData.merBusiContent) {
                uni.showToast({ title: '请选择商户经营内容', icon: 'none' });
                return false;
            }

            if (!formData.larName) {
                uni.showToast({ title: '请输入商户法人姓名', icon: 'none' });
                return false;
            }

            if (!formData.larIdType) {
                uni.showToast({ title: '请选择法人证件类型', icon: 'none' });
                return false;
            }

            if (!formData.larIdcard) {
                uni.showToast({ title: '请输入法人身份证号码', icon: 'none' });
                return false;
            }

            if (!formData.larIdcardStDt) {
                uni.showToast({ title: '请选择法人身份证开始日期', icon: 'none' });
                return false;
            }

            if (!formData.larIdcardExpDt) {
                uni.showToast({ title: '请选择法人身份证有效期', icon: 'none' });
                return false;
            }

            if (!formData.merContactMobile) {
                uni.showToast({ title: '请输入商户联系人手机号码', icon: 'none' });
                return false;
            }

            if (!formData.merContactName) {
                uni.showToast({ title: '请输入商户联系人姓名', icon: 'none' });
                return false;
            }

            if (!formData.openningBankCode) {
                uni.showToast({ title: '请输入结算账户开户行号', icon: 'none' });
                return false;
            }

            if (!formData.openningBankName) {
                uni.showToast({ title: '请输入结算账户开户行名称', icon: 'none' });
                return false;
            }

            if (!formData.clearingBankCode) {
                uni.showToast({ title: '请输入结算账户清算行号', icon: 'none' });
                return false;
            }

            if (!formData.acctNo) {
                uni.showToast({ title: '请输入结算账户账号', icon: 'none' });
                return false;
            }

            if (!formData.acctName) {
                uni.showToast({ title: '请输入结算账户名称', icon: 'none' });
                return false;
            }

            if (!formData.acctTypeCode) {
                uni.showToast({ title: '请选择结算账户性质', icon: 'none' });
                return false;
            }
            
            // 校验费率信息
            if (!formData.feeData[0].feeRateTypeCode) {
                uni.showToast({ title: '请输入费率类型', icon: 'none' });
                return false;
            }
            
            if (!formData.feeData[0].feeRateTypeName) {
                uni.showToast({ title: '请输入费率类型名称', icon: 'none' });
                return false;
            }
            
            if (!formData.feeData[0].feeRatePct) {
                uni.showToast({ title: '请输入手续费率', icon: 'none' });
                return false;
            }

            return true;
        },

        ecQuery() {
            const storeValue = uni.getStorageSync('storeValue');
            if (!storeValue || !storeValue.contractId) {
                uni.showToast({
                    title: '请先提交申请',
                    icon: 'none'
                });
                return;
            }
            const params = {
                contractId: storeValue.contractId
            }
            request(apiArr.queryMerchant, "POST", params).then(res => {
                if (res.respData.contractStatus === 'REVIEW_ING') {
                    uni.showToast({
                        title: '审核通过',
                        icon: 'success'
                    });
                    uni.setStorageSync('storeValue', { merInnerNo: res.respData.merInnerNo });
                    uni.setStorageSync('storeValue', { merCupNo: res.respData.merCupNo });
                } else if (res.respData.contractStatus === 'WAIT_FOR_CONTACT') {
                    uni.showToast({
                        title: '审核驳回',
                        icon: 'none'
                    });
                } else if (res.respData.contractStatus === 'MANUAL_AUDIT') {
                    uni.showToast({
                        title: '审核中',
                        icon: 'none'
                    });
                }
            })
        },

        // 表单提交逻辑
        submitForm() {
            // 验证表单
            if (!this.validateForm()) {
                return;
            }

            uni.showLoading({
                title: '提交中...',
                mask: true
            });

            // 准备请求参数
            const submitData = {
                mch_id: this.formData.mch_id,
                merRegName: this.formData.merRegName,
                merRegDistCode: this.formData.merRegDistCode,
                merRegAddr: this.formData.merRegAddr,
                mccCode: this.formData.mccCode,
                merBlisName: this.formData.merBlisName,
                merBlis: this.formData.merBlis,
                merBlisStDt: this.formData.merBlisStDt,
                merBlisExpDt: this.formData.merBlisExpDt,
                merBusiContent: this.formData.merBusiContent,
                larName: this.formData.larName,
                larIdType: this.formData.larIdType,
                larIdcard: this.formData.larIdcard,
                larIdcardStDt: this.formData.larIdcardStDt,
                larIdcardExpDt: this.formData.larIdcardExpDt,
                merContactMobile: this.formData.merContactMobile,
                merContactName: this.formData.merContactName,
                openningBankCode: this.formData.openningBankCode,
                openningBankName: this.formData.openningBankName,
                clearingBankCode: this.formData.clearingBankCode,
                acctNo: this.formData.acctNo,
                acctName: this.formData.acctName,
                acctTypeCode: this.formData.acctTypeCode,
                feeData: this.formData.feeData,
                fileData: []
            };

            // 只添加有attFileId的文件到fileData
            const validFiles = this.formData.fileData.filter(file => file.attFileId);
            if (validFiles.length > 0) {
                submitData.fileData = validFiles.map(file => ({
                    attType: file.attType,
                    attFileId: file.attFileId,
                    attachName: file.attachName
                }));
            }

            console.log('提交的数据:', submitData);

            // 调用addMer接口提交数据
            request(apiArr.addMer, "POST", submitData).then(res => {
                uni.hideLoading();
                uni.showToast({
                    title: '提交成功',
                    icon: 'success'
                });
                uni.setStorageSync('storeValue', { contractId: res.respData.contractId });
            }).catch(error => {
                uni.hideLoading();
                console.error('提交失败:', error);
                uni.showToast({
                    title: '提交失败',
                    icon: 'none'
                });
            });
        }
    }
}
</script>

<style>
@import url("./index.css");
</style>
