
<template>
  <div class="jeepay-m-center" style="margin-top: 5px; margin-left: 20px;">
    <a-steps v-show="(vdata.showStyle == showStyleEnum.step) && !isMobile" :current="vdata.currentStep - 1" type="navigation">
      <a-step title="主体信息" />
      <a-step title="店铺信息" />
      <a-step title="结算规则" />
      <a-step title="超级管理员" />
      <a-step title="结算账户" />
    </a-steps>
    <a-divider v-show="!isMobile" />
    <a-divider v-show="isMobile" class="jeepay-m-divider" orientation="left" style="color: #1A66FF;"><span style="color: #2980FD;">{{ vdata.currentStep }}</span>/{{ vdata.steps.length }} {{ vdata.steps[vdata.currentStep - 1] }}</a-divider>
    <!-- 1. 主体信息  -->
    <a-form
      v-show="vdata.showStyle == showStyleEnum.block || vdata.currentStep == 1"
      ref="stepForm1Ref"
      style="margin-top: 20px;"
      :model="applymentDetailInfo"
      :label-col="{ span: 4 }"
      :wrapper-col="{ span: 8 }"
      :rules="vdata.step1Rules"
    >
      <a-form-item label="选择商户类型" name="merchantType">
        <a-radio-group v-model:value="applymentDetailInfo.merchantType" :disabled="isView" @change="changeMerchantType">
          <a-radio :value="1">小微商户</a-radio>
          <a-radio :value="6">个人卖家</a-radio>
          <a-radio :value="2">个体工商户</a-radio>
          <a-radio :value="3">企业</a-radio>
        </a-radio-group>
      </a-form-item>

      <a-col v-if="applymentDetailInfo.merchantType == 2 || applymentDetailInfo.merchantType == 3">
        <a-divider class="jeepay-m-divider" orientation="left" style="color: #1A66FF;">营业执照</a-divider>

        <a-form-item label="营业执照照片" name="licenseImg">
          <JeepayUpload v-model:src="applymentDetailInfo.licenseImg" bizType="applyment" ocrType="license" @ocrScan="ocrScanFunc" />
          <span class="jeepay-tip-text">(上传图片自动识别 营业执照编号)</span>
        </a-form-item>
        <a-form-item label="营业执照编号" name="licenseNo">
          <a-input v-model:value.trim="applymentDetailInfo.licenseNo" :readonly="isView" />
        </a-form-item>
        <a-form-item label="商户名称全称" name="mchFullName">
          <a-input v-model:value.trim="applymentDetailInfo.mchFullName" :readonly="isView" />
        </a-form-item>
        <!-- <a-form-item label="法人身份证姓名" name="idcardName">
          <a-input v-model:value.trim="applymentDetailInfo.idcardName" :readonly="isView" />
        </a-form-item> -->
      </a-col>
      

      <a-divider class="jeepay-m-divider" orientation="left" style="color: #1A66FF;">法定代表人证件</a-divider>

      <a-form-item label="法人身份证人像面照片" name="idcard1Img">
        <JeepayUpload v-model:src="applymentDetailInfo.idcard1Img" bizType="applyment" ocrType="idCard" @ocrScan="ocrScanFunc" />
        <span class="jeepay-tip-text">(上传图片自动识别 身份证名称 身份证号)</span>
      </a-form-item>

      <a-form-item label="法人身份证国徽面照片" name="idcard2Img">
        <JeepayUpload v-model:src="applymentDetailInfo.idcard2Img" bizType="applyment" ocrType="idCard" @ocrScan="ocrScanFunc" />
        <span class="jeepay-tip-text">(上传图片自动识别 有效期)</span>
      </a-form-item>

      <a-form-item label="法人身份证姓名" name="idcardName">
        <a-input v-model:value.trim="applymentDetailInfo.idcardName" :readonly="isView" />
      </a-form-item>

      <a-form-item label="法人身份证号" name="idcardNo">
        <a-input v-model:value.trim="applymentDetailInfo.idcardNo" :readonly="isView" />
      </a-form-item>
      
      <a-form-item v-if="applymentDetailInfo.merchantType == 3" label="法人身份证居住地址" name="idcardAddress">
        <a-input v-model:value.trim="applymentDetailInfo.idcardAddress" :readonly="isView" />
      </a-form-item>

      <a-form-item label="法人身份证起始有效期" name="idcardEffectBegin">
        <a-input v-model:value.trim="applymentDetailInfo.idcardEffectBegin" style="width: 150px;margin-right: 10px;" />
      </a-form-item>

      <a-form-item class="jeepay-valid" label="法人身份证结束有效期" name="idcardEffectEnd">
        <a-input v-model:value.trim="applymentDetailInfo.idcardEffectEnd" :disabled="isView || applymentDetailInfo.idcardEffectEnd == '长期'" style="width: 150px;margin-right: 10px;" />
        <a-checkbox :checked="applymentDetailInfo.idcardEffectEnd == '长期' " :disabled="isView" @change="(e) => applymentDetailInfo.idcardEffectEnd = e.target.checked ? '长期' : '' ">长期</a-checkbox>
      </a-form-item>

      <a-form-item v-if="applymentDetailInfo.merchantType == '3'" label="经营者/法人是否为受益人" name="owner">
        <a-radio-group v-model:value="applymentDetailInfo.owner" :disabled="isView">
          <a-radio :value="true">是</a-radio>
          <a-radio :value="false">不是</a-radio>
        </a-radio-group>
      </a-form-item>

      <template v-if="applymentDetailInfo.merchantType == '3'">
        <a-button class="jeepay-m-btn jeepay-m-btn-sft" data-type="text" style="margin-left:35px" size="small" type="primary" @click="addCompanyBeneficiaryListFunc"><plus-outlined />新增企业受益人卡片</a-button>
      </template>

      <template v-if="applymentDetailInfo.merchantType == '3' && applymentDetailInfo.companyBeneficiaryList">
        <a-card class="jeepay-card-sft" v-for="(item, i) in applymentDetailInfo.companyBeneficiaryList" :key="i" style="margin:35px;">
          <template #title>
            <div style="display:flex;justify-content: space-between;">
              <span>企业受益人信息[{{ i+1 }}]</span>
              <div>
                <!-- <a-button style="margin-right:20px" size="small" type="primary" @click="useBizCompanyBeneficiaryFunc(i)"><partition-outlined />设置法人为受益人</a-button> -->
                <template v-if="(!applymentDetailInfo.owner && applymentDetailInfo.companyBeneficiaryList.length > 1) || applymentDetailInfo.owner">
                  <a-button class="jeepay-m-btn" data-type="text" size="small" type="danger" @click="delCompanyBeneficiaryListFunc(i)"><delete-outlined />删除该企业受益人卡片</a-button>
                </template>
              </div>
            </div>
          </template>


          <div style="padding-top:20px">
            <a-form-item label="受益人身份证人像面照片" :name="`companyBeneficiaryListIdcard1Img_${i}`">
              <JeepayUpload v-model:src="item.idcard1Img" bizType="applyment" ocrType="idCard" @ocrScan="(o) => ocrScanFuncByCompanyBeneficiary(o, i)" />
              <span class="jeepay-tip-text">(上传图片自动识别 身份证名称 身份证号)</span>
            </a-form-item>

            <a-form-item label="受益人身份证国徽面照片" :name="`companyBeneficiaryListIdcard2Img_${i}`">
              <JeepayUpload v-model:src="item.idcard2Img" bizType="applyment" ocrType="idCard" @ocrScan="(o) => ocrScanFuncByCompanyBeneficiary(o, i)" />
              <span class="jeepay-tip-text">(上传图片自动识别 有效期)</span>
            </a-form-item>

            <a-form-item label="受益人身份证姓名" :name="`companyBeneficiaryListIdcardName_${i}`">
              <a-input v-model:value.trim="item.idcardName" :readonly="isView" />
            </a-form-item>

            <a-form-item label="受益人身份证号" :name="`companyBeneficiaryListIdcardNo_${i}`">
              <a-input v-model:value.trim="item.idcardNo" :readonly="isView" />
            </a-form-item>

            <a-form-item label="受益人身份证起始有效期" :name="`companyBeneficiaryListIdcardEffectBegin_${i}`">
              <a-input v-model:value.trim="item.idcardEffectBegin" :readonly="isView" />
            </a-form-item>

            <a-form-item label="受益人身份证结束有效期" :name="`companyBeneficiaryListIdcardEffectEnd_${i}`">
              <a-input v-model:value.trim="item.idcardEffectEnd" :readonly="isView" :disabled="item.idcardEffectEnd == '长期'" style="width: 150px;margin-right: 10px;" />
              <a-checkbox :disabled="isView" :checked="item.idcardEffectEnd == '长期' " @change="(e : any) => item.idcardEffectEnd = e.target.checked ? '长期' : '' ">长期</a-checkbox>
            </a-form-item>

            <a-form-item label="受益人身份证居住地址" :name="`companyBeneficiaryListIdcardAddress_${i}`">
              <a-input v-model:value.trim="item.idcardAddress" :readonly="isView" />
            </a-form-item>
          </div>
        </a-card>
      </template>
    </a-form>

    <!-- 2. 店铺信息  -->
    <a-form
      v-show="vdata.showStyle == showStyleEnum.block || vdata.currentStep == 2"
      ref="stepForm2Ref"
      :model="applymentDetailInfo"
      :label-col="{ span: 4 }"
      :wrapper-col="{ span: 8 }"
      :rules="vdata.step2Rules"
    >
      <a-form-item label="商户简称" name="mchShortName">
        <a-input v-model:value.trim="applymentDetailInfo.mchShortName" :readonly="isView" />
        <span class="jeepay-tip-text">在支付完成页向买家展示</span>
      </a-form-item>

      <a-form-item label="店铺名称" name="storeName">
        <a-input v-model:value.trim="applymentDetailInfo.storeName" :readonly="isView" />
      </a-form-item>

      <a-form-item label="店铺链接" name="storeUrl">
        <a-input v-model:value.trim="applymentDetailInfo.storeUrl" :readonly="isView" />
        <span class="jeepay-tip-text">店铺二维码 or 店铺链接二选一必填。</span>
      </a-form-item>

      <a-form-item label="店铺二维码" name="storeQrCode">
        <JeepayUpload v-model:src="applymentDetailInfo.storeQrCode" bizType="applyment" />
        <span class="jeepay-tip-text">若为电商小程序，可上传店铺页面的小程序二维码</span>
      </a-form-item>

      <a-form-item label="小程序AppID" name="miniProgramSubAppid">
        <a-input v-model:value.trim="applymentDetailInfo.miniProgramSubAppid" :readonly="isView" />
        <span class="jeepay-tip-text">可填写已认证的小程序AppID，认证主体需与二级商户主体一致。</span>
      </a-form-item>
    </a-form>

    <!-- 3. 结算规则  -->
    <a-form
      v-show="vdata.showStyle == showStyleEnum.block || vdata.currentStep == 3"
      ref="stepForm3Ref"
      :model="applymentDetailInfo"
      :label-col="{ span: 4 }"
      :wrapper-col="{ span: 8 }"
      :rules="vdata.step3Rules"
    >
      <a-form-item label="结算规则信息" name="settlementId">
        <a-select v-model:value="applymentDetailInfo.settlementId" placeholder="选择结算规则" @change="changeSettlementIdFunc">
          <template v-for="item in vdata.settlementList" :key="item.settlementId">
            <a-select-option :value="item.settlementId">
              <div style="display: flex;justify-content: space-between;word-break: break-all;">
                <span class="option-left">{{ item.settlementName }}</span>
                <span class="option-right">{{ item.settRule }}</span>
              </div>
            </a-select-option>
          </template>
        </a-select>
        <div class="jeepay-tip-text">请根据实际经营行业选择结算规则</div>
      </a-form-item>

      <a-form-item label="选择所属行业" name="qualificationType">
        <a-select v-model:value="applymentDetailInfo.qualificationType" placeholder="选择所属行业" @change="changeQualificationFunc">
          <template v-for="item in vdata.qualificationOptions" :key="item.qualificationType">
            <a-select-option :value="item.qualificationType">
              <div style="display: flex;flex-direction: column;word-break: break-all;">
                <span class="option-top">{{ item.qualificationType }}</span>
                <span class="option-bottom">{{ item.qualificationDesc }}</span>
              </div>
            </a-select-option>
          </template>
        </a-select>
      </a-form-item>

      <a-form-item v-if="vdata.qualificationDesc != '无需特殊资质'" label="特殊资质" name="qualifications">
        <JeepayUpload v-model:src="applymentDetailInfo.qualifications" bizType="applyment" />
        <div v-if="vdata.qualificationDesc" class="jeepay-tip-text">{{ vdata.qualificationDesc }}</div>
      </a-form-item>
    </a-form>

    <!-- 4. 超级管理员  -->
    <a-form
      v-show="vdata.showStyle == showStyleEnum.block || vdata.currentStep == 4"
      ref="stepForm4Ref"
      :model="applymentDetailInfo"
      :label-col="{ span: 4 }"
      :wrapper-col="{ span: 10 }"
      :rules="vdata.step4Rules"
    >
      <a-form-item label="超管类型" name="contactType">
        <a-radio-group v-model:value="applymentDetailInfo.contactType" :disabled="isView" style="width: 100%;">
          <a-radio value="65">经营者/法人</a-radio>
          <a-radio v-if="applymentDetailInfo.merchantType == 2 || applymentDetailInfo.merchantType == 3" value="66">经办人</a-radio>
        </a-radio-group>
        <div class="jeepay-tip-text">超级管理员将接收开户信息及完成签约，并可进行商户号的日常重要管理和资金操作</div>
      </a-form-item>

      <template v-if="applymentDetailInfo.contactType == '66'">
        <a-form-item label="超管身份证人像面照片" name="contactIdDocCopy">
          <JeepayUpload v-model:src="applymentDetailInfo.contactIdDocCopy" bizType="applyment" ocrType="idCard" @ocrScan="contactOcrScanFunc" />
          <span class="jeepay-tip-text">(上传图片自动识别 身份证名称 身份证号)</span>
        </a-form-item>

        <a-form-item label="超管身份证国徽面照片" name="contactIdDocCopyBack">
          <JeepayUpload v-model:src="applymentDetailInfo.contactIdDocCopyBack" bizType="applyment" ocrType="idCard" @ocrScan="contactOcrScanFunc" />
          <span class="jeepay-tip-text">(上传图片自动识别 有效期)</span>
        </a-form-item>

        <a-form-item label="超管身份证姓名" name="contactName">
          <a-input v-model:value.trim="applymentDetailInfo.contactName" :readonly="isView" />
        </a-form-item>

        <a-form-item label="超管身份证号" name="contactIdNumber">
          <a-input v-model:value.trim="applymentDetailInfo.contactIdNumber" :readonly="isView" />
        </a-form-item>
      
        <a-form-item label="超管身份证起始有效期" name="contactIdDocPeriodBegin">
          <a-input v-model:value.trim="applymentDetailInfo.contactIdDocPeriodBegin" style="width: 150px;margin-right: 10px;" />
        </a-form-item>

        <a-form-item label="超管身份证结束有效期" name="contactIdDocPeriodEnd">
          <a-input v-model:value.trim="applymentDetailInfo.contactIdDocPeriodEnd" :disabled="isView || applymentDetailInfo.contactIdDocPeriodEnd == '长期'" style="width: 150px;margin-right: 10px;" />
          <a-checkbox :checked="applymentDetailInfo.contactIdDocPeriodEnd == '长期' " :disabled="isView" @change="(e) => applymentDetailInfo.contactIdDocPeriodEnd = e.target.checked ? '长期' : '' ">长期</a-checkbox>
        </a-form-item>

        <a-form-item label="业务办理授权函" name="businessAuthorizationLetter">
          <JeepayUpload v-model:src="applymentDetailInfo.businessAuthorizationLetter" bizType="applyment" />
          <span class="jeepay-tip-text">参考：<a href="https://kf.qq.com/faq/220509Y3Yvym220509fQvYR7.html" target="_blank">授权函模板</a></span>
        </a-form-item>
      </template>
      <a-form-item label="手机号" name="contactPhone">
        <a-input v-model:value.trim="applymentDetailInfo.contactPhone" :readonly="isView" />
        <span class="jeepay-tip-text">用于接收微信支付的日常重要管理信息及日常操作验证码</span>
      </a-form-item>
      <a-form-item label="邮箱" name="contactEmail">
        <a-input v-model:value.trim="applymentDetailInfo.contactEmail" :readonly="isView" />
        <span class="jeepay-tip-text">用于接收微信支付的开户邮件及日常业务通知</span>
      </a-form-item>
    </a-form>

    <!-- 5. 结算账户  -->
    <a-form
      v-show="vdata.showStyle == showStyleEnum.block || vdata.currentStep == 5"
      ref="stepForm5Ref"
      :model="applymentDetailInfo"
      :label-col="{ span: 4 }"
      :wrapper-col="{ span: 8 }"
      :rules="vdata.step5Rules"
    >
      <a-form-item label="账户类型" name="settAccountType">
        <a-radio-group v-model:value="applymentDetailInfo.settAccountType" :disabled="isView" @change="() => emit('itemRender')">
          <a-radio v-if="applymentDetailInfo.merchantType == 1 || applymentDetailInfo.merchantType == 6 || applymentDetailInfo.merchantType == 2" value="C">对私</a-radio>
          <a-radio v-if="applymentDetailInfo.merchantType == 2 || applymentDetailInfo.merchantType == 3" value="B">对公</a-radio>
        </a-radio-group>
      </a-form-item>

      <a-form-item label="开户名称" name="settAccountName">
        <a-input v-model:value.trim="applymentDetailInfo.settAccountName" :readonly="isView" />
      </a-form-item>

      <a-form-item label="银行账号" name="settAccountNo">
        <a-input v-model:value.trim="applymentDetailInfo.settAccountNo" :readonly="isView" />
      </a-form-item>

      <a-form-item label="开户行" name="settAccountBankName">
        <JeepaySftpayBankCodeSelect v-model:value="applymentDetailInfo.accountBankNameShow" :settAccountType="applymentDetailInfo.settAccountType" :disabled="isView" @change="changeBankFunc" />
      </a-form-item>

      <a-form-item label="开户行省市区" name="settAccountBankBranchAreaCode">
        <JeepayAreaSelect v-model:value="applymentDetailInfo.settAccountBankBranchAreaCode" :disabled="isView" @change="changeSettAccountBankBranchAreaCodeFunc" />
      </a-form-item>

      <a-col v-if="applymentDetailInfo.needBankBranch">
        <a-divider orientation="left" style="color: #1A66FF;padding-top: 30px;">开户行支行信息</a-divider>

        <a-form-item label="支行所在地区" name="bankBranchAreaCode">
          <JeepayAreaSelectSft v-model:value="applymentDetailInfo.bankBranchAreaCode" :disabled="isView" @changeSft="changeBankBranchAreaCodeFunc" />
        </a-form-item>

        <a-form-item label="支行名称" name="settAccountBankBranchName">
          <a-select v-model:value="applymentDetailInfo.settAccountBankBranchName" :disabled="isView" placeholder="开户支行名称">
            <a-select-option v-for="(item) in vdata.bankBranchList" :key="item.bank_branch_id" :value="item.bank_branch_name">
              {{ item.bank_branch_name }}
            </a-select-option>
          </a-select>
        </a-form-item>
      </a-col>
    </a-form>

    <a-divider />

    <div class="jeepay-btn-box jeepay-m-btn-box" style="margin-top: 50px; margin-bottom: 50px;text-align: center;">
      <a-button v-show="vdata.showStyle == showStyleEnum.step && vdata.currentStep > 1" class="jeepay-m-btn" data-type="inline" type="primary" @click="toPrevStep"><left-circle-outlined /> 上一步 </a-button>
      <a-button v-show="vdata.showStyle == showStyleEnum.step && vdata.currentStep < 5" class="jeepay-m-btn" data-type="inline" type="primary" @click="toNextStep">下一步 <right-circle-outlined /> </a-button>
    </div>
  </div>
</template>

<script setup lang="ts">

import {reactive, getCurrentInstance, defineEmits, inject, ref} from 'vue'
import ruleGenerator from '@/utils/ruleGenerator'
import { $bankBranchList } from '@/api/manage'
import mccList from './sftMcc.json'

const jeepayPaywayRatePanelRef = ref()

// 获取当前实例的代理对象
const { proxy } : any = getCurrentInstance()

const { $infoBox } = getCurrentInstance()!.appContext.config.globalProperties

// 显示类型枚举值
const showStyleEnum = {
  step: 'step',
  block: 'block'
}

// emit： 向父组件进行通讯。 
const emit = defineEmits(['itemRender'])

// 参数注入： 是否手机端
let isMobile : any = inject('isMobile')

// 接收进件参数（包含 用户号， ifCode ）
let mchApplymentDataRef : any = inject('mchApplymentData')
let mchApplymentData : any = mchApplymentDataRef.value

// 接收父组件的进件详细参数
let applymentDetailInfoInject : any = inject('applymentDetailInfo')
let applymentDetailInfo : any = applymentDetailInfoInject.value

// 注入： 是否预览模式
let isView : any = inject('isView')

// 参数注入： 配置方式
let configMode : any = inject('configMode')

// 响应式数据
const vdata : any = reactive({
  showStyle: 'step', // 显示类型： step-分步,  block-整页显示 
  steps: ['主体信息', '店铺信息', '结算规则', '超级管理员', '结算账户'],
  isTempData: false, // 是否保存草稿
  bankBranchList: [], // 支行列表
  isvDefaultPaywayFee: {} as any, // 服务商默认费率
  currentStep: 1,
  step1Rules: {
                merchantType: [ ruleGenerator.requiredSelect('商户类型', 'number') ],
                licenseImg: [ ruleGenerator.requiredUpload('营业执照图片') ],
                licenseNo: [ ruleGenerator.requiredInput('营业执照编号') ],
                mchFullName: [ ruleGenerator.requiredInput('商户名称全称') ],
                idcard1Img: [ ruleGenerator.requiredUpload('法人身份证人像面照片') ],
                idcard2Img: [ ruleGenerator.requiredUpload('法人身份证国徽面照片') ],
                idcardName: [ ruleGenerator.requiredInput('身份证中的姓名') ],
                idcardNo: [ ruleGenerator.requiredInput('身份证号') ],
                idcardAddress: [ ruleGenerator.requiredInput('身份证居住地址') ],
                idcardEffectBegin: [ ruleGenerator.requiredInput('身份证起始有效期'), ruleGenerator.date ],
                idcardEffectEnd: [ ruleGenerator.requiredInput('身份证有效期截止日期'), ruleGenerator.dateOrForever ],

                owner: [ ruleGenerator.requiredSelect('经营者/法人是否为受益人', 'boolean') ],

                companyBeneficiaryListIdcardName_0: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(0, 'idcardName', '请输入身份证中的姓名') } ],
                companyBeneficiaryListIdcardNo_0: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(0, 'idcardNo', '请输入身份证号') } ],
                companyBeneficiaryListIdcardEffectBegin_0: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(0, 'idcardEffectBegin', '请输入身份证起始有效期') } ],
                companyBeneficiaryListIdcardEffectEnd_0: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(0, 'idcardEffectEnd', '请输入身份证有效期截止日期') } ],
                companyBeneficiaryListIdcardAddress_0: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(0, 'idcardAddress', '请输入身份证居住地址') } ],
                companyBeneficiaryListIdcard1Img_0: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(0, 'idcard1Img', '请上传人像照') } ],
                companyBeneficiaryListIdcard2Img_0: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(0, 'idcard2Img', '请上传国徽面照') } ],

                companyBeneficiaryListIdcardName_1: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(1, 'idcardName', '请输入身份证中的姓名') } ],
                companyBeneficiaryListIdcardNo_1: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(1, 'idcardNo', '请输入身份证号') } ],
                companyBeneficiaryListIdcardEffectBegin_1: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(1, 'idcardEffectBegin', '请输入身份证起始有效期') } ],
                companyBeneficiaryListIdcardEffectEnd_1: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(1, 'idcardEffectEnd', '请输入身份证有效期截止日期') } ],
                companyBeneficiaryListIdcardAddress_1: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(1, 'idcardAddress', '请输入身份证居住地址') } ],
                companyBeneficiaryListIdcard1Img_1: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(1, 'idcard1Img', '请上传人像照') } ],
                companyBeneficiaryListIdcard2Img_1: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(1, 'idcard2Img', '请上传国徽面照') } ],

                companyBeneficiaryListIdcardName_2: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(2, 'idcardName', '请输入身份证中的姓名') } ],
                companyBeneficiaryListIdcardNo_2: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(2, 'idcardNo', '请输入身份证号') } ],
                companyBeneficiaryListIdcardEffectBegin_2: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(2, 'idcardEffectBegin', '请输入身份证起始有效期') } ],
                companyBeneficiaryListIdcardEffectEnd_2: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(2, 'idcardEffectEnd', '请输入身份证有效期截止日期') } ],
                companyBeneficiaryListIdcardAddress_2: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(2, 'idcardAddress', '请输入身份证居住地址') } ],
                companyBeneficiaryListIdcard1Img_2: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(2, 'idcard1Img', '请上传人像照') } ],
                companyBeneficiaryListIdcard2Img_2: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(2, 'idcard2Img', '请上传国徽面照') } ],

                companyBeneficiaryListIdcardName_3: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(3, 'idcardName', '请输入身份证中的姓名') } ],
                companyBeneficiaryListIdcardNo_3: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(3, 'idcardNo', '请输入身份证号') } ],
                companyBeneficiaryListIdcardEffectBegin_3: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(3, 'idcardEffectBegin', '请输入身份证起始有效期') } ],
                companyBeneficiaryListIdcardEffectEnd_3: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(3, 'idcardEffectEnd', '请输入身份证有效期截止日期') } ],
                companyBeneficiaryListIdcardAddress_3: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(3, 'idcardAddress', '请输入身份证居住地址') } ],
                companyBeneficiaryListIdcard1Img_3: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(3, 'idcard1Img', '请上传人像照') } ],
                companyBeneficiaryListIdcard2Img_3: [ {required: true, validator:  (rule, value) => companyBeneficiaryRulesFunc(3, 'idcard2Img', '请上传国徽面照') } ],

              },
  step2Rules: {
                mchShortName: [ ruleGenerator.requiredInput('商户简称') ],
                servicePhone: [ ruleGenerator.requiredInput('客服电话') ],
                storeName: [ ruleGenerator.requiredInput('门店名称') ],
                areaCode: [ ruleGenerator.requiredSelect('位置编码', 'array') ],
                address: [ ruleGenerator.requiredInput('详细地址') ],
                storeInnerImg: [ ruleGenerator.requiredUpload('门头照') ],
                storeOuterImg: [ ruleGenerator.requiredUpload('店内环境照') ],
  },
  step3Rules: {
                // settlementId: [ ruleGenerator.requiredSelect('结算规则') ],
                // qualificationType: [ ruleGenerator.requiredSelect('行业') ],
                qualifications: [{ required: applymentDetailInfo.isNeedQualification, trigger: 'blur' }, {
                  validator: (rule, value) => {
                    if (applymentDetailInfo.isNeedQualification && !value) {
                        return Promise.reject('请上传特殊资质')
                    }
                    return Promise.resolve()
                  }
                }]
  },
  step4Rules: {
            contactType: [ ruleGenerator.requiredSelect('超管身份') ],
            contactIdDocCopy: [ ruleGenerator.requiredUpload('超管身份证人像面照片') ],
            contactIdDocCopyBack: [ ruleGenerator.requiredUpload('超管身份证国徽面照片') ],
            contactName: [ ruleGenerator.requiredInput('超管姓名') ],
            contactIdNumber: [ ruleGenerator.requiredInput('超管身份证号码') ],
            contactIdDocPeriodBegin: [ ruleGenerator.requiredInput('超管身份证起始有效期'), ruleGenerator.date ],
            contactIdDocPeriodEnd: [ ruleGenerator.requiredInput('超管身份证结束有效期'), ruleGenerator.dateOrForever ],
            businessAuthorizationLetter: [ ruleGenerator.requiredUpload('业务办理授权函') ],
            contactPhone: [ ruleGenerator.requiredInput('手机号'), ruleGenerator.mobile ],
            contactEmail: [ ruleGenerator.requiredInput('邮箱') ]
  },
  step5Rules: {
                settAccountType: [ ruleGenerator.requiredSelect('账户类型') ],
                settAccountNo: [ ruleGenerator.requiredInput('银行帐号') ],
                settAccountName: [ ruleGenerator.requiredInput('开户名称') ],
                settAccountBankName: [ ruleGenerator.requiredInput('开户行') ],
                settAccountBankBranchAreaCode: [ ruleGenerator.requiredSelect('开户行省市区', 'array') ],
  }
  
})


// 初始化进件信息
init()
function init() {

  if (!applymentDetailInfo.merchantType) {
    return
  }

  // 根据商户类型：  个体户/企业 确定结算规则List
  getSettlementList()

  const settlementId = applymentDetailInfo.settlementId
  const qualificationType = applymentDetailInfo.qualificationType

  // 处理结算规则信息
  if (settlementId) {
    changeSettlementIdFunc(settlementId)
  }

  // 处理行业信息
  if (qualificationType) {
    changeQualificationFunc(qualificationType)
  }

}

// 主体类型选中事件
function changeMerchantType() {
  // 清空结算规则
  applymentDetailInfo.settlementId = ''
  vdata.qualificationDesc = ''
  getSettlementList()

  if(applymentDetailInfo.merchantType == '3'){ // 企业

      // 企业类型必传 受益人
      if(!applymentDetailInfo.companyBeneficiaryList || applymentDetailInfo.companyBeneficiaryList.length == 0){
        applymentDetailInfo.companyBeneficiaryList = [{}]
      }
  }
}

  // 根据商户类型：  个体户/企业 确定结算规则List
function getSettlementList() {
  vdata.settlementList = []
    
  if (applymentDetailInfo.merchantType == 1){
      vdata.settlementList = mccList.micro
  }else if (applymentDetailInfo.merchantType == 2){
      vdata.settlementList = mccList.individual
  }else if (applymentDetailInfo.merchantType == 3){
      vdata.settlementList = mccList.enterprise
  }
}

// 结算规则选中事件
function changeSettlementIdFunc(e: any) {
  const selectItem = vdata.settlementList.find( item => item.settlementId == e)
  if (!selectItem) {
    // 清空结算规则
    applymentDetailInfo.settlementId = ''
    return
  }

  vdata.qualificationOptions = selectItem.qualification

  // 结算规则变化，重置行业/特殊资质信息
  applymentDetailInfo.settlementId = e
  applymentDetailInfo.qualificationType = ''
  applymentDetailInfo.isNeedQualification = ''
  vdata.qualificationDesc = ''
}

// 行业特殊资质选择事件
function changeQualificationFunc(e){

  if (!vdata.qualificationOptions) {
    return
  }

  const selectItem = vdata.qualificationOptions.find(item => item.qualificationType == e)
  if (!selectItem) {
    return
  }

  applymentDetailInfo.qualificationType = selectItem.qualificationType
  applymentDetailInfo.isNeedQualification = selectItem.isNeedQualification

  vdata.qualificationDesc = selectItem.qualificationDesc
}

// 开户银行切换事件
function changeBankFunc(e) {
  applymentDetailInfo.bankAliasCode = e.bank_alias_code // 银行别名编码，请求银行分支时使用
  applymentDetailInfo.needBankBranch = e.need_bank_branch // 是否需要填写分支
  applymentDetailInfo.settAccountBankName = e.account_bank // 开户行

  vdata.bankBranchList = []
  applymentDetailInfo.settAccountBankBranchName = ''
  applymentDetailInfo.bankBranchAreaCode = []
}

// 开户行省市编码切换事件
function changeSettAccountBankBranchAreaCodeFunc() {
  vdata.bankBranchList = []
  applymentDetailInfo.settAccountBankBranchName = ''
  applymentDetailInfo.bankBranchAreaCode = []
}

// 开户银行分支行所在地区切换事件
function changeBankBranchAreaCodeFunc(e) {
  $bankBranchList(mchApplymentData.mchNo, mchApplymentData.ifCode, { cityCode: e[1].value, bankAliasCode: applymentDetailInfo.bankAliasCode }).then((res) => {
    vdata.bankBranchList = res
    applymentDetailInfo.settAccountBankBranchName = ''
  })
}


// 下一步
function toNextStep() {
  // 验证是否通过
  proxy.$refs['stepForm'+vdata.currentStep+'Ref'].validate().then( () => {
      vdata.currentStep ++
  })
}

// 上一步
function toPrevStep(){
  vdata.currentStep --
}

function validateAllForm(step: number){
  return proxy.$refs['stepForm'+step+'Ref'].validate().then( () => {
     if(step >= 4){
      return Promise.resolve()
     }
     return validateAllForm(++step)
     
  })
}

// 图片识别的回调函数
function ocrScanFunc(orcObject: any){
  if(orcObject){
    Object.assign(applymentDetailInfo, orcObject)
  }
}

// 超管身份证识别的回调函数
function contactOcrScanFunc(orcObject: any){
  if(orcObject.idcardName) {
    applymentDetailInfo.contactName = orcObject.idcardName
  }
  if(orcObject.idcardNo) {
    applymentDetailInfo.contactIdNumber = orcObject.idcardNo
  }
  if(orcObject.idcardEffectBegin) {
    applymentDetailInfo.contactIdDocPeriodBegin = orcObject.idcardEffectBegin
  }
  if(orcObject.idcardEffectEnd) {
    applymentDetailInfo.contactIdDocPeriodEnd = orcObject.idcardEffectEnd
  }
}


// 点击【保存】的前置事件, 需要返回Promise对象
function saveDataPreCallback(isTemp){

  // 保存草稿只校验当前步骤数据
  if (isTemp) {
    return Promise.resolve()
  }

  // 验证所有表单
  return validateAllForm(1).then(() => {
    return Promise.resolve()
  }).catch((params: any) => {
    $infoBox.message.error('数据填写不完整， 请检查后提交')
    return Promise.reject()
  })
}

// 新增受益人
function addCompanyBeneficiaryListFunc(){
  
  if(!applymentDetailInfo.companyBeneficiaryList){
    applymentDetailInfo.companyBeneficiaryList = [{}]
  }else{

    if(applymentDetailInfo.owner && applymentDetailInfo.companyBeneficiaryList.length >= 3){
      return $infoBox.message.error('最多新增三个企业受益人！')
    } else if(!applymentDetailInfo.owner && applymentDetailInfo.companyBeneficiaryList.length >= 4){
      return $infoBox.message.error('最多新增四个企业受益人！')
    }

    applymentDetailInfo.companyBeneficiaryList.push({})
  }
  
}

// 删除受益人
function delCompanyBeneficiaryListFunc(i){
  applymentDetailInfo.companyBeneficiaryList.splice(i, 1)
}


// 图片识别的回调函数
function ocrScanFuncByCompanyBeneficiary(orcObject, i){

  if(orcObject){
    Object.assign(applymentDetailInfo.companyBeneficiaryList[i], orcObject)
  }

}


// 图片识别的回调函数
function ocrScanFuncByContact(orcObject){

  if(orcObject){
    orcObject.idcardNo && (applymentDetailInfo.contactIdcardNo = orcObject.idcardNo)
    orcObject.idcardAddress && (applymentDetailInfo.contactIdcardAddress = orcObject.idcardAddress)
    orcObject.idcardEffectBegin && (applymentDetailInfo.contactIdcardEffectBegin = orcObject.idcardEffectBegin)
    orcObject.idcardEffectEnd && (applymentDetailInfo.contactIdcardEffectEnd = orcObject.idcardEffectEnd)
  }

}


// 使用法人信息
function useBizCompanyBeneficiaryFunc(i){
  applymentDetailInfo.companyBeneficiaryList[i].idcardName = applymentDetailInfo.idcardName
  applymentDetailInfo.companyBeneficiaryList[i].idcardNo = applymentDetailInfo.idcardNo
  applymentDetailInfo.companyBeneficiaryList[i].idcard1Img = applymentDetailInfo.idcard1Img
  applymentDetailInfo.companyBeneficiaryList[i].idcard2Img = applymentDetailInfo.idcard2Img
  applymentDetailInfo.companyBeneficiaryList[i].idcardAddress = applymentDetailInfo.idcardAddress
  applymentDetailInfo.companyBeneficiaryList[i].idcardEffectBegin = applymentDetailInfo.idcardEffectBegin
  applymentDetailInfo.companyBeneficiaryList[i].idcardEffectEnd = applymentDetailInfo.idcardEffectEnd
}


// 自定义校验规则
function companyBeneficiaryRulesFunc(index, field, msg){

  // 不存在时不校验
  if(!applymentDetailInfo.companyBeneficiaryList || applymentDetailInfo.companyBeneficiaryList.length < 0){
    return Promise.resolve()
  }

  if(!applymentDetailInfo.companyBeneficiaryList[index][field]){
    return Promise.reject(msg)
  }

  return Promise.resolve()
}


defineExpose({ saveDataPreCallback })

</script>

<style lang="less" scoped>
.option-left {
  width: 50%;
  word-wrap: break-word; 
  white-space: normal;
  word-break: break-all;
}
.option-right {
  color: rgb(153, 153, 153);
}
.option-bottom {
  word-wrap: break-word; 
  white-space: normal;
  word-break: break-all;
  color: rgb(153, 153, 153);
}
.jeepay-btn-box {
  margin: 15px -12px !important;
}
.jeepay-btn-box .ant-btn {
  margin: 0 10px;
}
</style>