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

  @author terrfly
  @site https://www.jeequan.com
  @date 2022/11/03 08:35
-->
<template>
  <AppConfigCommonPage ref="appConfigCommonPageRef">
    <template #appConfigPage>
      <a-col :span="24">
        <a-form-item label="参数配置到应用：">
          <a-select v-model:value="vdata.configAppId" class="form-item-content" placeholder="选择应用">
            <a-select-option key="">请选择商户应用</a-select-option>
            <a-select-option v-for="(item) in vdata.mchAppList" :key="item.appId">{{ item.appName }} [{{ item.appId }}]</a-select-option>
          </a-select>
          <div class="jeepay-m-pz-box" style="margin-top: 10px;">
            <a-button
              class="jeepay-m-btn"
              data-type="text"
              size="small"
              style="margin-left: 20px;"
              type="primary"
              :disabled="!vdata.configAppId"
              @click="configMchAppIdFunc"
            >
              <save-outlined />配置【支付参数】到应用
            </a-button>
          </div>
        </a-form-item>
      </a-col>
    </template>
    
    <template #wxConfigPage><div /></template>
    <template #advancedConfigPage>
      <a-col :span="24">
        <div class="jeepay-m-pz-box" style="margin-top: 10px;">
          <a-input v-model:value="vdata.smid" addon-before="子商户号SMID: " placeholder="直付通二级商户号" style="width: 300px;" />
          <a-button
            class="jeepay-m-btn"
            data-type="text"
            size="small"
            style="margin-left: 20px;"
            type="primary"
            danger
            :disabled="!vdata.configAppId"
            @click="zftMchDelete"
          >
            <rest-outlined />废弃
          </a-button>
        </div>
      </a-col>
    </template>
  </AppConfigCommonPage>
</template>

<script lang="ts" setup>

import { req, API_URL_MCH_APP, $applymentAppConfig, $zftMchDelete } 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 appConfigModeByMchAppId : any = inject('appConfigModeByMchAppId')

const vdata : any = reactive({
  detailData: {}, // 数据对象
  recordId: null, // 更新对象ID
  mchAppList: [], // 商户app列表
  configAppId: '',
  tableData: [],
  mchNo: '',

  activeKey: '' , //手风琴展开key 
  mchShortName: '', //商户简称
  mchAppPublicKey: 'ISV', //商户应用key
  smid:''
})

// 对外暴露的 页面渲染函数  也用作初始化
function pageRender (applyId){
  vdata.recordId = applyId
  appConfigCommonPageRef.value.pageRender(applyId) // 直接调用通用组件
  req.list(API_URL_MCH_APP, { pageSize: -1, mchNo: getSelectedMchNo() }).then(res2 => {
    vdata.mchAppList = res2.records
    if(vdata.mchAppList.length > 0){
      vdata.configAppId = vdata.mchAppList[0].appId
    }
  })

}

// 配置本系统的app应用配置项
function configMchAppIdFunc(){

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

return $applymentAppConfig(vdata.recordId, vdata.configAppId).then((res) => {
  $infoBox.message.success('商户应用配置完成， 如商户支付方式未配置，请进行方式配置。 ')
  return Promise.resolve()
})

}
// 支付宝商户作废
function zftMchDelete() {
  
  if (!vdata.smid) {
    return $infoBox.message.error('请输入子商户号SMID')
  }
  $infoBox.confirmDanger('确认要废弃所选二级商户吗？废弃后不可恢复，需重新发起进件。', '', () => {
    $zftMchDelete(appConfigModeByMchAppId?appConfigModeByMchAppId:vdata.configAppId, vdata.smid).then((res) => {
      if (res.channelState == 'CONFIRM_SUCCESS') {
        $infoBox.message.success('二级商户 '+vdata.smid+' 已被废弃。')
      }else{
        $infoBox.message.error('废弃失败，失败原因：' + res.channelErrMsg)
      }
    })
  })
}

function getSelectedMchNo (){
  return appConfigCommonPageRef.value.getSelectedMchNo()
}

// 不支持商户应用配置进入的配置（ 仅支持进件完成后的配置，如配置到应用 配置费率这些 ）
function unsupportAppConfigMode(){
  return false
}

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