<!--
 支付宝官方： 【参数配置】 页面内容

  @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="danger"
              :disabled="!vdata.configAppId"
              @click="configMchRateAppIdFunc(null)"
            >
              <save-outlined />配置【费率】到应用
            </a-button>
          </div>
        </a-form-item>
      </a-col>
    </template>
    <template #wxConfigPage><div /></template>
    <template #advancedConfigPage><div /></template>
  </AppConfigCommonPage>
</template>


<script lang="ts" setup>

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

const appConfigCommonPageRef = ref()
// 接收父组件的参数注入： 配置方式 (判断是 运营平台  代理商 还是商户 )
let configMode : any = inject('configMode')
// 接收父组件的参数注入： 进件详细信息
let applyRecord : any = inject('applyRecord')

const vdata : any = reactive({
  detailData: {}, // 进件参数
  rateConfigMode: configMode == 'mgrApplyment' ? 'mgrMch' : configMode == 'agentApplyment' ? 'agentMch' : null
})

// 配置费率到应用
function configMchRateAppIdFunc(paywayFeeList = null){

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

// 自定义费率 or 进件费率 
let reqPaywayFeeList = paywayFeeList || JSON.parse(vdata.detailData.applyDetailInfo).paywayFeeList

let reqObject = {infoId: vdata.configAppId, ifCode: vdata.detailData.ifCode, configMode: vdata.rateConfigMode, MCHRATE: reqPaywayFeeList}
return $saveRateConfig(reqObject).then((res) => {
  $infoBox.message.success('费率保存成功')
})

}

// 对外暴露的 页面渲染函数  也用作初始化
function pageRender (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
    }
  })
  vdata.detailData = applyRecord
}

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


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

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