<template>
  <div style="background: #fff">
    <div class="account-settings-info-view">
      <a-form ref="configInfoFormModelRef" :model="vdata.configData" layout="vertical">
        <a-row>
          <a-col :span="24" :offset="1">
            <a-form-item label="可开启的权限" name="">
              <a-checkbox v-model:checked="vdata.subMchWxConfigIsUsable">开启子商户微信配置参数权限</a-checkbox>
              <a-checkbox v-model:checked="vdata.subMchPayInterfaceConfigIsUsable" style="margin-left: 20px;">开启子商户接口配置权限</a-checkbox>
            </a-form-item>
          </a-col>
        </a-row>

        <a-form-item class="bottom-btn" style="margin-top: 20px">
          <a-button type="primary" :loading="vdata.btnLoading" @click="confirm"><check-circle-outlined />确认更新</a-button>
        </a-form-item>
      </a-form>
    </div>
  </div>
</template>
<script setup lang="ts">
import { req, API_URL_AGENT_CONFIG } from '@/api/manage'
import { ref, reactive, onMounted, getCurrentInstance, nextTick } from 'vue'

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

// 组件注入
const props: any = defineProps({
  currentAgentNo: { type: String, default: '' }, // 当前代理商No
})


const configInfoFormModelRef = ref()


const vdata : any = reactive ({
  btnLoading: false,
  subMchWxConfigIsUsable: false,
  subMchPayInterfaceConfigIsUsable: false,
  groupKey: 'mchAuthorityConfig', // 运营平台系统配置的的OEM 配置key 
  configData: {} as any, // 配置保存对象

}) as any


onMounted(()=>{

  // 查询代理商的配置信息
  req.list(API_URL_AGENT_CONFIG, {groupKey: vdata.groupKey, agentNo: props.currentAgentNo}).then(res => {

    if(!res){
      return false
    }
    res.forEach(configItem => {
      // 配置信息
      if(configItem.configKey == 'subMchWxConfigIsUsable'){
        vdata.subMchWxConfigIsUsable = configItem.configVal?configItem.configVal == 'true':false
      }
      if(configItem.configKey == 'subMchPayInterfaceConfigIsUsable'){
        vdata.subMchPayInterfaceConfigIsUsable = configItem.configVal?configItem.configVal == 'true':false
      }
      
    })

    nextTick( () => {

    })
        
  })

})


// 更新配置
async function confirm () {

  // 配置信息 <K - V> 格式  注意： V是String类型 
  let configObject : any = { subMchWxConfigIsUsable: vdata.subMchWxConfigIsUsable, subMchPayInterfaceConfigIsUsable: vdata.subMchPayInterfaceConfigIsUsable }

  // 发送数据的格式类型
  let configArray : any = []
  Object.keys(configObject).forEach( k => {
    configArray.push({ groupKey: vdata.groupKey, configKey: k, configVal : configObject[k] })
  })

  vdata.btnLoading = true
  req.updateById(API_URL_AGENT_CONFIG, vdata.groupKey, {agentNo: props.currentAgentNo, configData: configArray}).then(res => {
    $infoBox.message.success('更新成功')
  }).finally(res => {
    vdata.btnLoading = false
  })
 
}

</script>
<style lang="less" scoped>
  .bottom-btn{
    /deep/ div{
      display: flex;
      justify-content: center;
    }
  }
</style>