<!--
  盛付通： 【参数配置】 页面内容

  @author terrfly
  @site https://www.jeequan.com
  @date 2022/11/03 08:35
-->
<template>
  <AppConfigCommonPage ref="appConfigCommonPageRef">
    <template #wxConfigPage>
      <a-divider class="jeepay-m-divider" orientation="left" :orientationMargin="0">
        <span style="font-size: 16px;font-weight: 600;color: var(--ant-primary-color);">微信配置项</span>
      </a-divider>
      <a-col :span="isMobile ? 24 : 12">
        <a-form-item class="jeepay-valid" label="业务类型" style="margin: 0;">
          <a-select v-model:value="vdata.feeType" placeholder="业务类型">
            <a-select-option value="02">标准费率线下</a-select-option>
            <a-select-option value="01">标准费率线上</a-select-option>
            <a-select-option value="03">非盈利费率</a-select-option>
            <a-select-option value="04">缴费费率</a-select-option>
            <a-select-option value="05">保险费率</a-select-option>
            <a-select-option value="06">行业活动费率</a-select-option>
            <a-select-option value="07">校园餐饮费率</a-select-option>
            <a-select-option value="08">中小幼费率</a-select-option>
            <a-select-option value="09">非在线教培</a-select-option>
          </a-select>
        </a-form-item>
      </a-col>
      
      <a-col :span="isMobile ? 24 : 12">
        <a-form-item class="jeepay-valid" label="微信支付目录" style="margin: 0;">
          <a-input v-model:value.trim="vdata.configWxPayBaseUrl" class="jeepay-tip-text" />
        </a-form-item>
      </a-col>
      
      <a-col :span="isMobile ? 24 : 12">
        <a-form-item class="jeepay-valid" label="公众号appId" style="margin: 0;">
          <a-input v-model:value.trim="vdata.configWxBindAppId" class="jeepay-tip-text" />
        </a-form-item>
      </a-col>
        
      <a-col :span="isMobile ? 24 : 12">
        <a-form-item class="jeepay-valid" label="关联服务商小程序appId：" style="margin: 0;">
          <a-input v-model:value.trim="vdata.configWxBindLiteAppId" class="jeepay-tip-text" />
        </a-form-item>
      </a-col>
        
      <a-col :span="isMobile ? 24 : 12">
        <a-form-item class="jeepay-valid" label="微信渠道号" style="margin: 0;">
          <a-input v-model:value.trim="vdata.bankChannelNo" class="jeepay-tip-text" />
        </a-form-item>
      </a-col>
      <a-col :span="24">
        <a-button class="jeepay-m-btn" data-type="block" type="primary" @click="wxConfigFunc">一键配置微信配置项</a-button>
      </a-col>
    </template>
    <template #advancedConfigPage><div /></template>
  </appconfigcommonpage>
</template>

<script lang="ts" setup>

import { $dgWxSet, $queryInterfaceConfig, req } from '@/api/manage'
import { ref, reactive, inject, getCurrentInstance } from 'vue'
import AppConfigCommonPage from '../../commons/AppConfigCommonPage.vue'
const { $infoBox } = getCurrentInstance()!.appContext.config.globalProperties

const appConfigCommonPageRef = ref()

// 参数注入： 是否手机端
let isMobile : any = inject('isMobile')
// 接收父组件的参数注入： 选择的商户应用
let appConfigModeByMchAppId : any = inject('appConfigModeByMchAppId')

const vdata : any = reactive({
  detailData: {}, // 数据对象
  recordId: null, // 更新对象ID
  visible: false, // 是否显示弹层/抽屉
  mchAppList: [], // 商户app列表
  configAppId: '',
  
  partner: '', // 银联第三方交易识别码

  feeType: '02',  // 业务开通类型
  bankChannelNo: '',  // 渠道号
  configWxPayBaseUrl: '', // 微信支付目录
  configWxBindLiteAppId: '', // 关联小程序
  configWxBindAppId: '', // 公众号appid
})

function wxConfigFunc() {

  let recordId = getSelectedConfigAppId()

  if(!recordId){
    $infoBox.message.error('请选择商户应用')
    return Promise.reject()
  }

  recordId = appConfigModeByMchAppId
  if (!recordId) {
    recordId = getSelectedConfigAppId()
  }

  // 默认为空
  let appConfigModeAndIfCode = 'dgpay'

  $dgWxSet(recordId, 
  { 
    'configWxPayBaseUrl': vdata.configWxPayBaseUrl, 
    'feeType': vdata.feeType, 
    'bankChannelNo': vdata.bankChannelNo, 
    'configWxBindLiteAppId': vdata.configWxBindLiteAppId,
    'configWxBindAppId': vdata.configWxBindAppId,
    'appConfigModeAndIfCode': appConfigModeAndIfCode
  }).then((res) => {

    $infoBox.message.success('配置成功 ')
  })

}

// 对外暴露的 页面渲染函数  也用作初始化
function pageRender (applyId){
  vdata.recordId = applyId

  vdata.mchAppList = []
  vdata.feeType = '02',  // 业务开通类型
  vdata.configWxPayBaseUrl = ''
  vdata.configWxBindLiteAppId = ''
  vdata.configWxBindAppId = ''

  // 查询配置信息 
  commonQueryInterfaceConfigFunc().then((res2) => {
    vdata.configWxPayBaseUrl = res2.paySiteUrl
    vdata.configWxBindAppId = res2.wxAppId
    vdata.configWxBindLiteAppId = res2.wxLiteAppId
  })

  appConfigCommonPageRef.value.pageRender(applyId) // 直接调用通用组件
}

// 获取当前配置的APPID
function getSelectedConfigAppId (){
  return appConfigCommonPageRef.value.getSelectedConfigAppId()
}

// 给diy页面的全局公用函数 （ 请求在一个地方处理 ）
function commonQueryInterfaceConfigFunc(){

  // 查询配置信息参数。 
  let reqApplyId = vdata.recordId
  let applyIdConvertMchAppId = 0
  let convertMchAppIfCode :any = null
  if (!reqApplyId) {
    reqApplyId = appConfigModeByMchAppId
    applyIdConvertMchAppId = 1
    convertMchAppIfCode = 'dgpay'
  }
  // 查询配置信息 
  return $queryInterfaceConfig(reqApplyId, applyIdConvertMchAppId, convertMchAppIfCode)

}


defineExpose({ pageRender, getSelectedConfigAppId })
</script>

<style scoped>

.form-item-content{
  width: 70%
}

</style>