<!--
  盛付通： 【签约开通】 页面内容

  @author terrfly
  @site https://www.jeequan.com
  @date 2022/11/03 08:35
-->
<template>
  <a-result :title="vdata.idCardPhoneTxt">
    <template #icon>
      <smile-twoTone />
    </template>
  </a-result>
</template>

<script lang="ts" setup>

import { ref, inject, nextTick } from 'vue'
import NextBizsCommonPage from '../../commons/NextBizsCommonPage.vue'
import ruleGenerator from '@/utils/ruleGenerator'
import { req, $dgpayConfigOpen, $dgpayConfigOpenQuery } from '@/api/manage'
import { getCurrentInstance, reactive } from 'vue'
const { $infoBox } = getCurrentInstance()!.appContext.config.globalProperties

// 接收父组件的参数注入： 进件详细欣欣
let applyRecord : any = inject('applyRecord')
const nextBizsCommonPage = ref()

const vdata : any = reactive({
  bizSignInfoBtnLoading: false,
  state: '',
  idCardPhone:"",
  idCardPhoneTxt:"",
  recordId: null, // 更新对象ID
  applyData: {} as any, // 进件资料对象
  visible: false, // 是否显示弹层/抽屉
  rules: {
    applyRatio: [ ruleGenerator.requiredInput('分账比例', 'number') ],
  },
  queryResult: {} as any,
  configJson: {} as any,
})

function onSubmit() {

  if (!vdata.applyData.applyRatio) {
    return $infoBox.message.error('请填写分账比例')
  }

  vdata.btnLoading = true

  $dgpayConfigOpen(vdata.recordId, {applyRatio: vdata.applyData.applyRatio}).then(() => {
    $infoBox.message.success('审核中')
    vdata.btnLoading = false
  }).catch(() => {
    vdata.btnLoading = false
  })

}

function selectTabs (key) { // 清空必填提示
  if (key) {
    vdata.groupKey = key
  }
}

function queryFun() {
  if (vdata.state == 2) {
    vdata.bizSignInfoBtnLoading = true
    $dgpayConfigOpenQuery(vdata.recordId).then((item) => {
      console.log(item,'itemitemitemitemitem')
      vdata.queryResult = JSON.parse(item)

      if (vdata.queryResult.split_bill_result) {
        vdata.configJson = JSON.parse(vdata.queryResult.split_bill_result)
        vdata.configJson = vdata.configJson[0]
      }

      vdata.bizSignInfoBtnLoading = false
      vdata.btnLoading = false
    }).catch(() => {
      vdata.bizSignInfoBtnLoading = false
      vdata.btnLoading = false
    })
  }
}

function pageRender (recordId) { // 弹层打开事件
  vdata.detailData = applyRecord
  const applyDetailInfo = JSON.parse(vdata.detailData.applyDetailInfo)
  console.log(applyDetailInfo ,'pageRenderpageRenderapplyRecordapplyRecordapplyRecord')
  vdata.recordId = recordId
  vdata.idCardPhone =  hidePhoneNumber(applyDetailInfo.idCardPhone)

  vdata.idCardPhoneTxt =  "请查看联系人手机"+vdata.idCardPhone+" 短信，点击短信链接进行商户变更协议签约";
  console.log(vdata.idCardPhoneTxt,'vdataidCardPhoneTxt')
  // 状态
  vdata.state = vdata.detailData.state
  // 单号
  vdata.recordId = vdata.detailData.applyId
  // 类型
  vdata.ifCode = vdata.detailData.ifCode
  // 用户号
  vdata.mchNo = vdata.detailData.mchNo

  if (vdata.detailData.channelVar1) {
    let channelVar1 = JSON.parse(vdata.detailData.channelVar1)
    if (channelVar1.applyRatio) {
      vdata.applyData.applyRatio = channelVar1.applyRatio
    }
  }

  queryFun()

  nextTick(() => {
    nextBizsCommonPage.value.pageRender(vdata.recordId) // 直接调用通用组件
  })
}
function hidePhoneNumber(phone) {
  return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2'); // 将前三位保持不变，后面四位改为"*"
}

defineExpose({ pageRender })
</script>