<template>
  <a-drawer
    v-model:visible="vdata.visible"
    :mask-closable="false"
    title="商户高级配置"
    :body-style="{ paddingBottom: '80px' }"
    width="60%"
    class="drawer-width"
    @close="vdata.visible = false"
  >
    <div style="background: #fff">
      <a-tabs v-if="vdata.visible" @change="selectTabs">
        <a-tab-pane key="mchApiEnt" tab="接口权限">
          <a-table :dataSource="vdata.mchApiEntTableData" :columns="vdata.mchApiEntTableColumns" :pagination="false" size="small">
            <!-- header 标题 插槽 -->
            <template #headerCell="{ column }">
              <template v-if="column.key === 'batch'">
                <a-checkbox @change="(e) => mchApiEntAllCehcked(e.target.checked)" />
              </template>
            </template>

            <template #bodyCell="{ column, record }">
              <template v-if="column.key === 'batch'"><a-checkbox v-model:checked="record.checked" :disabled="record.disabled" /></template>
            </template>

            <template #title>商户可自调用接口</template>
          </a-table>


          <a-form-item class="bottom-btn">
            <a-button type="primary" :loading="vdata.btnLoading" @click="confirmByApiEnt"><check-circle-outlined />确认更新</a-button>
          </a-form-item>
        </a-tab-pane>
      </a-tabs>
    </div>
  </a-drawer>
</template>
<script setup lang="ts">
import { API_URL_MCH_CONFIG, API_URL_MCH_LIST, req, API_URL_MCH_APP, API_URL_MCH_STORE_LIST, } from '@/api/manage'
import { ref, reactive, getCurrentInstance } from 'vue'

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

  const configFormModel = ref()
  const payTestBarCode =ref()


  const vdata : any = reactive ({

    recordId: null, // 更新对象ID
    visible: false, // 是否显示弹层/抽屉

    mchAppList: [] as any, // app列表
    mchStoreList: [] as any, // 门店列表
    appId: '', // 已选择的appId
    storeId: '', // 已选择的门店ID
    cashierUrl: '',  // 收银台地址
    isShowCashier: true as any,

    btnLoading: false,
    groupKey: 'orderConfig',
    mchConfigData: [] as any, // 配置保存对象
    defaultConfig: [{
      configKey: 'appVoice',
      configName: '是否启用app订单语音播报',
      configVal: '1',
      type: 'radio'
    },
    {
      configKey: 'qrcEscaping',
      configName: '是否启用码牌防逃单功能',
      configVal: '1',
      type: 'radio'
    },
    {
    configKey: 'weChatVoice',
    configName: '小程序语音推送',
    configVal: '0',
    type: 'radio'
    }
  ], // 默认配置项

    mchPayNotifyUrl: '', // 商户支付回调地址
    mchRefundNotifyUrl: '', // 商户退款回调地址

    mchNotifyFlag: {
      payNotifyFlag: '1',
      refundNotifyFlag: '1'
    },  // 商户回调开关

    mchNotifyPostType: 'POST_BODY', // 发送商户通知的方式  POST_BODY   POST_QUERYSTRING  POST_JSON
    
    notifyTableColumns: [
      {key: 'batch', title: '批量选择' },
      {key: 'key', dataIndex: 'key', title: '参数KEY' },
      {key: 'name', dataIndex: 'name', title: '参数名称' },
      // {key: 'desc', dataIndex: 'desc', title: '参数描述' }
    ],

    notifyTableData: [
      {key: 'payOrderId', name: '支付订单号', desc: '', checked: true, disabled: true },
      {key: 'mchNo', name: '用户号', desc: '用户号', checked: true, disabled: true },
      {key: 'appId', name: '应用ID', desc: '', checked: true, disabled: true },
      {key: 'mchOrderNo', name: '商户订单号', desc: '', checked: true, disabled: true },
      {key: 'ifCode', name: '支付接口', desc: '', checked: true, disabled: true },
      {key: 'wayCode', name: '支付方式', desc: '', checked: true, disabled: true },
      {key: 'amount', name: '支付金额', desc: '', checked: true, disabled: true },
      {key: 'currency', name: '货币代码', desc: '', checked: true, disabled: true },
      {key: 'state', name: '订单状态', desc: '', checked: true, disabled: true },
      {key: 'clientIp', name: '客户端IP', desc: '', checked: true, disabled: true },
      {key: 'subject', name: '商品标题', desc: '', checked: true, disabled: true },
      {key: 'body', name: '商品描述', desc: '', checked: true, disabled: true },
      {key: 'channelOrderNo', name: '渠道订单号', desc: '', checked: true, disabled: true },
      {key: 'errCode', name: '渠道错误码', desc: '', checked: true, disabled: true },
      {key: 'errMsg', name: '渠道错误描述', desc: '', checked: true, disabled: true },
      {key: 'extParam', name: '扩展参数', desc: '', checked: true, disabled: true },
      {key: 'successTime', name: '支付成功时间', desc: '', checked: true, disabled: true },
      {key: 'createdAt', name: '创建时间', desc: '', checked: true, disabled: true },
      {key: 'sign', name: '签名', desc: '', checked: true, disabled: true },

      {key: 'storeId', name: '门店ID', desc: '', checked: false, disabled: false },
      {key: 'lng', name: '经度', desc: '', checked: false, disabled: false },
      {key: 'lat', name: '纬度', desc: '', checked: false, disabled: false },
      {key: 'qrcId', name: '码牌ID', desc: '', checked: false, disabled: false },
      {key: 'wayCodeType', name: '支付方式代码分类', desc: '', checked: false, disabled: false },
      {key: 'mchFeeRate', name: '商户手续费费率快照', desc: '', checked: false, disabled: false },
      {key: 'mchFeeAmount', name: '商户手续费,单位分', desc: '', checked: false, disabled: false },
      {key: 'channelUser', name: '渠道用户标识,如微信openId,支付宝账号', desc: '', checked: false, disabled: false },
      {key: 'divisionMode', name: '订单分账模式', desc: '', checked: false, disabled: false },
      {key: 'buyerRemark', name: '买家备注', desc: '买家备注', checked: false, disabled: false },
      {key: 'sellerRemark', name: '卖家备注', desc: '卖家备注', checked: false, disabled: false },
      {key: 'expiredTime', name: '订单失效时间', desc: '', checked: false, disabled: false },
      {key: 'platformOrderNo', name: '支付凭证交易单号', desc: '', checked: false, disabled: false },
      {key: 'platformMchOrderNo', name: '支付凭证商户单号', desc: '', checked: false, disabled: false },
    ],

    mchApiEntTableColumns: [
      {key: 'batch', title: '批量选择' },
      {key: 'title', dataIndex: 'title', title: '名称' },
      {key: 'key', dataIndex: 'key', title: 'KEY' },
      {key: 'apiPath', dataIndex: 'apiPath', title: '路径' },
    ],

    mchApiEntTableData: [
      {key: 'API_PAY_ORDER', title: '统一下单', apiPath: '/api/pay/unifiedOrder', checked: false },
      {key: 'API_PAY_ORDER_QUERY', title: '查询支付订单', apiPath: '/api/pay/query', checked: false },
      {key: 'API_PAY_ORDER_CLOSE', title: '支付订单关闭', apiPath: '/api/pay/close', checked: false },
      {key: 'API_CHANNEL_USER', title: '获取渠道用户ID', apiPath: '/api/channelUserId/jump', checked: false },
      {key: 'API_REFUND_ORDER', title: '发起支付退款', apiPath: '/api/refund/refundOrder', checked: false },
      {key: 'API_REFUND_ORDER_QUERY', title: '查询退款订单', apiPath: '/api/refund/query', checked: false },
      {key: 'API_TRANS_ORDER', title: '发起转账订单', apiPath: '/api/transferOrder', checked: false },
      {key: 'API_TRANS_ORDER_QUERY', title: '查询转账订单', apiPath: '/api/transfer/query', checked: false },
      {key: 'API_TRANS_BALANCE_QUERY', title: '查询转账可用余额', apiPath: '/api/transfer/balance/query', checked: false },
      {key: 'API_DIVISION_BIND', title: '绑定分账用户', apiPath: '/api/division/receiver/bind', checked: false },
      {key: 'API_DIVISION_EXEC', title: '发起订单分账', apiPath: '/api/division/exec', checked: false },
      {key: 'API_DIVISION_CHANNEL_BALANCE', title: '查询分账用户可用余额', apiPath: '/api/division/receiver/channelBalanceQuery', checked: false },
      {key: 'API_DIVISION_CHANNEL_CASHOUT', title: '对分账用户的渠道余额发起提现', apiPath: '/api/division/receiver/channelBalanceCashout', checked: false },
    ],

    cashierWxH5Data: [
      { label: '微信H5', value: 'WX_H5', },
      { label: '微信小程序', value: 'WX_LITE', },
    ],
    cashierAliWebData: [ 
      { label: '支付宝WAP', value: 'ALI_WAP', },
      { label: '支付宝生活号', value: 'ALI_JSAPI', },
      { label: '支付宝小程序', value: 'ALI_LITE', },
    ],


    // 分账管理菜单 
    divisionConfig: { overrideAutoFlag: 0, autoDivisionRules: { amountLimit: 0, delayTime: 120  }, mchDivisionEntFlag: 1, calBaseAmountType: 'INCOME_AMOUNT' },

    // 广告配置
    advertConfig: { advertFlag: 0 },

    // 便捷收银台配置
    selfCashierState : { configKey: 'selfCashierState', configName: '便捷收银台权限开关', configVal: '0', type: 'radio' }, 
    selfCashierSiteInfoType: { configKey: 'selfCashierSiteInfoType', configName: '便捷收银台logo/底部显示配置', configVal: 'DEFAULT', type: 'text' }, 
    selfCashierWxH5Config: { configKey: 'selfCashierWxH5Config', configName: '便捷收银台微信H5配置', configVal: ['WX_H5'], type: 'text' }, 
    selfCashierAliWapConfig: { configKey: 'selfCashierAliWapConfig', configName: '便捷收银台支付宝WAP配置', configVal: ['ALI_WAP'], type: 'text'},
    
    // 商户web收银台配置
    wabCashierState: { configKey: 'wabCashierState', configName: 'WEB收银台权限开关', configVal: '0', type: 'radio' },
    wabCashierSiteInfoType: { configKey: 'wabCashierSiteInfoType', configName: 'WEB收银台logo/底部显示配置', configVal: 'DEFAULT', type: 'text' },
    wabCashierWxH5Config: { configKey: 'wabCashierWxH5Config', configName: 'WEB收银台微信H5配置', configVal: ['WX_H5'], type: 'text' },
    wabCashierAliWapConfig: { configKey: 'wabCashierAliWapConfig', configName: 'WEB收银台支付宝WAP配置', configVal: ['ALI_WAP'], type: 'text' },

    // 会员配置
    memberConfigData: [
      { configKey: 'memberModelState', configName: '会员模块状态开关', configVal: '1', type: 'radio' },
      { configKey: 'memberPayState', configName: '会员支付开关', configVal: '0', type: 'radio' },
      { configKey: 'memberCustomAmountState', configName: '充值自定义金额', configVal: '1', type: 'radio' },
      { configKey: 'mbrMaxBalance', configName: '会员最大储值余额(元), 0表示依据系统配置', configVal: '0', type: 'text' },
    ],
  })


  function show (recordId) { // 弹层打开事件

      vdata.recordId = recordId
      selectTabs('mchApiEnt') //初始化数据
      vdata.visible = true
  }


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

      if(key == 'orderConfig'){

        req.list(API_URL_MCH_CONFIG, {groupKey: vdata.groupKey, mchNo: vdata.recordId}).then(res => {
          if (res != null && res.length > 0) {
            vdata.mchConfigData = res
            // 遍历总配置列表；如果configKey不存在则push进该条记录
            for (var key in vdata.defaultConfig) {
              // configKey是否存在 默认为不存在
              var isdefault = true
              vdata.mchConfigData.forEach((item) => {
                // 遍历到相同key时，key存在，赋值false
                if (item.configKey == vdata.defaultConfig[key].configKey) {
                  isdefault = false
                }
              })
              // 如果不存在 true 添加该条默认记录
              if(isdefault) {
                vdata.mchConfigData.push(vdata.defaultConfig[key])
              }
            }

          }else {
            vdata.mchConfigData = vdata.defaultConfig
          }
        })
      }else if(key == 'payOrderNotifyExtParams'){

         // 恢复初始值
         vdata.notifyTableData.filter(r => !r.disabled).forEach(r => {
             r. checked = false
         })
        
         vdata.mchNotifyPostType = 'POST_BODY'
         vdata.mchPayNotifyUrl = ''
         vdata.mchRefundNotifyUrl = ''

        req.list(API_URL_MCH_CONFIG, {groupKey: vdata.groupKey, mchNo: vdata.recordId}).then(res => {
          if (res != null && res.length > 0) {
            res.forEach(item => {
              if (item.configKey == 'payOrderNotifyExtParams') {
                let arr = JSON.parse(item.configVal)
                vdata.notifyTableData.filter(r => !r.disabled).forEach(r => {
                  r.checked =  arr.indexOf(r.key) >= 0
                })
              }else if (item.configKey == 'mchPayNotifyUrl') {
                vdata.mchPayNotifyUrl = item.configVal
              }else if (item.configKey == 'mchRefundNotifyUrl') {
                vdata.mchRefundNotifyUrl = item.configVal
              }else if (item.configKey == 'mchNotifyPostType') {
                vdata.mchNotifyPostType = item.configVal
              }else if (item.configKey == 'mchNotifyFlag') {
                if (item.configVal) {
                  vdata.mchNotifyFlag = JSON.parse(item.configVal)
                }
              }

            })
          }
        })
      }else if(key == 'divisionManage'){

        // 分账管理恢复默认
        vdata.divisionConfig = { overrideAutoFlag: 0, autoDivisionRules: { amountLimit: 0, delayTime: 120  }, mchDivisionEntFlag: 1, calBaseAmountType: 'INCOME_AMOUNT' }

        req.list(API_URL_MCH_CONFIG, {groupKey: vdata.groupKey, mchNo: vdata.recordId}).then(res => {
          if (res != null && res.length > 0) {
            vdata.divisionConfig = JSON.parse(res[0].configVal)
            if(!vdata.divisionConfig.calBaseAmountType){
              vdata.divisionConfig.calBaseAmountType = 'INCOME_AMOUNT' // 存量数据为空， 默认为： 入账金额。 
            }
            vdata.divisionConfig.autoDivisionRules.amountLimit = Number.parseFloat((vdata.divisionConfig.autoDivisionRules.amountLimit / 100).toFixed(2))
          }
        })
      }else if(key == 'mchApiEnt'){ // 接口权限 


        // 恢复初始值
        vdata.mchApiEntTableData.forEach(r => {
            r. checked = false
        })

        req.list(API_URL_MCH_CONFIG, {groupKey: vdata.groupKey, mchNo: vdata.recordId}).then(res => {
          if (res != null && res.length > 0) {
            res.forEach(item => {
              if (item.configKey == 'mchApiEntList') { // 商户接口权限集合 
                let arr = JSON.parse(item.configVal)
                vdata.mchApiEntTableData.forEach(r => {
                  r.checked = arr.indexOf(r.key) >= 0
                })
              }
            })
          }
        })
        
      }else if(key == 'advertConfig'){ // 广告权限 


        // 恢复初始值
        vdata.mchApiEntTableData.forEach(r => {
            r. checked = false
        })

        req.getById(API_URL_MCH_LIST, vdata.recordId).then(res => {
          vdata.advertConfig = res
        })

      }else if(key == 'selfCashier'){ // 便捷收银台配置

        // 初始化配置
        vdata.selfCashierState = { configKey: 'selfCashierState', configName: '便捷收银台权限开关', configVal: '0', type: 'radio' }, 
        vdata.selfCashierSiteInfoType = { configKey: 'selfCashierSiteInfoType', configName: '便捷收银台logo/底部显示配置', configVal: 'DEFAULT', type: 'text' }, 
        vdata.selfCashierWxH5Config = { configKey: 'selfCashierWxH5Config', configName: '便捷收银台微信H5配置', configVal: ['WX_H5'], type: 'text' }, 
        vdata.selfCashierAliWapConfig = { configKey: 'selfCashierAliWapConfig', configName: '便捷收银台支付宝WAP配置', configVal: ['ALI_WAP'], type: 'text'},
    
        
        req.list(API_URL_MCH_CONFIG, {groupKey: vdata.groupKey, mchNo: vdata.recordId}).then(res => {
          if (res != null && res.length > 0) {
            res.forEach(value => {
              
              if(value.configKey == 'selfCashierState') {
                vdata.selfCashierState = value
              }else if (value.configKey == 'selfCashierSiteInfoType') {
                vdata.selfCashierSiteInfoType = value
              }else if (value.configKey == 'selfCashierWxH5Config') {
                vdata.selfCashierWxH5Config = value
                if (vdata.selfCashierWxH5Config.configVal) {
                  vdata.selfCashierWxH5Config.configVal = JSON.parse(vdata.selfCashierWxH5Config.configVal)
                }
              }else if (value.configKey == 'selfCashierAliWapConfig') {
                vdata.selfCashierAliWapConfig = value
                if (vdata.selfCashierAliWapConfig.configVal) {
                  vdata.selfCashierAliWapConfig.configVal = JSON.parse(vdata.selfCashierAliWapConfig.configVal)
                }
              }
            })
          }
        })


        vdata.appId = ''
        vdata.storeId = ''
        // 请求接口，获取所有的appid，只有此处进行pageSize=-1传参
        req.list(API_URL_MCH_APP, { pageSize: -1, mchNo: vdata.recordId, state: 1 }).then(res => {
          vdata.mchAppList = res.records
          if (vdata.mchAppList.length > 0) {
            // 赋予默认值
            //vdata.appId = vdata.mchAppList[0].appId
            //TODO
            if(vdata.appId == '') vdata.appId = vdata.mchAppList[0].appId
            // 根据appId的值，动态显示支付方式
            // appPaywayListHandle(vdata.appId)
            one()
          }
          
        })
        function one (){
          // 请求接口，获取商户所有门店，只有次数进行pageSize=-1传参
          req.list(API_URL_MCH_STORE_LIST, { pageSize: -1, mchNo: vdata.recordId }).then(res => {
            vdata.mchStoreList = res.records
            if (vdata.mchStoreList.length > 0) {
              // 赋予默认值
              if(vdata.storeId == '') vdata.storeId = vdata.mchStoreList[0].storeId
              // two()
            }
          })
        }
        // function two (){
        //   $mchConfigCreateCashier({
        //     appId: vdata.appId, // appId
        //     storeId: vdata.storeId, // storeId
        //     mchNo: vdata.recordId
        //   }).then(res => {
        //     vdata.cashierUrl = res
        //   }).catch(() => {
        //     payTestBarCode.value.processCatch()
        //   })
        // }

      }else if(key == 'webCashier'){ // web收银台配置

        // 初始化配置
        vdata.webCashierState = { configKey: 'webCashierState', configName: 'WEB收银台权限开关', configVal: '0', type: 'radio' }, 
        vdata.webCashierSiteInfoType = { configKey: 'webCashierSiteInfoType', configName: 'WEB收银台logo/底部显示配置', configVal: 'DEFAULT', type: 'text' }, 
        vdata.webCashierWxH5Config = { configKey: 'webCashierWxH5Config', configName: 'WEB收银台微信H5配置', configVal: ['WX_H5'], type: 'text' }, 
        vdata.webCashierAliWapConfig = { configKey: 'webCashierAliWapConfig', configName: 'WEB收银台支付宝WAP配置', configVal: ['ALI_WAP'], type: 'text'},


        req.list(API_URL_MCH_CONFIG, {groupKey: vdata.groupKey, mchNo: vdata.recordId}).then(res => {
          if (res != null && res.length > 0) {
            res.forEach(value => {
              
              if(value.configKey == 'webCashierState') {
                vdata.webCashierState = value
              }else if (value.configKey == 'webCashierSiteInfoType') {
                vdata.webCashierSiteInfoType = value
              }else if (value.configKey == 'webCashierWxH5Config') {
                vdata.webCashierWxH5Config = value
                if (vdata.webCashierWxH5Config.configVal) {
                  vdata.webCashierWxH5Config.configVal = JSON.parse(vdata.webCashierWxH5Config.configVal)
                }
              }else if (value.configKey == 'webCashierAliWapConfig') {
                vdata.webCashierAliWapConfig = value
                if (vdata.webCashierAliWapConfig.configVal) {
                  vdata.webCashierAliWapConfig.configVal = JSON.parse(vdata.webCashierAliWapConfig.configVal)
                }
              }
            })
          }
        })

      }else if(key == 'memberConfig'){
        req.list(API_URL_MCH_CONFIG, {groupKey: vdata.groupKey, mchNo: vdata.recordId}).then(res => {

          if (res != null && res.length > 0) {
            vdata.memberConfigData.forEach(item =>{
              res.forEach(res => {
                if (item.configKey == res.configKey) {
                  item.configVal = res.configVal

                  if (item.configKey == 'mbrMaxBalance' && res.configVal) {
                    item.configVal = Number.parseFloat((res.configVal / 100).toFixed(2))
                  }
                }
              })
            })
          }

        })
      }

    }
  }


  // 确认更新
  function confirm (e) {
    configFormModel.value.validate().then(valid => {
      $infoBox.confirmPrimary('确认修改支付配置吗？', '', () => {
        vdata.btnLoading = true // 打开按钮上的 loading

        req.updateById(API_URL_MCH_CONFIG, vdata.groupKey, {mchNo: vdata.recordId, configData: JSON.stringify(vdata.mchConfigData)}).then(res => {
          $infoBox.message.success('修改成功')
          vdata.btnLoading = false
        }).catch(res => {
          vdata.btnLoading = false
        })
      })
    })
  }


  // 确认更新
  function confirmByNotify (e) {

    $infoBox.confirmPrimary('确认修改回调参数吗？', '更新完成后请尽快检查回调接收地址，避免验签失败造成业务损失！', () => {
      vdata.btnLoading = true // 打开按钮上的 loading


      let arr : any = []
      vdata.notifyTableData.filter(r => !r.disabled).forEach(r => {
          if(r.checked){
            arr.push(r.key)
          }
      })

      let configItem = [
                  { groupKey: vdata.groupKey, configName: '支付订单回调和查单参数', configKey: 'payOrderNotifyExtParams', configVal : JSON.stringify(arr) },
                  { groupKey: vdata.groupKey, configName: 'POS支付回调地址', configKey: 'mchPayNotifyUrl', configVal : vdata.mchPayNotifyUrl },
                  { groupKey: vdata.groupKey, configName: 'POS退款回调地址', configKey: 'mchRefundNotifyUrl', configVal : vdata.mchRefundNotifyUrl },
                  { groupKey: vdata.groupKey, configName: '回调开关', configKey: 'mchNotifyFlag', configVal : vdata.mchNotifyFlag },
                  { groupKey: vdata.groupKey, configName: '商户接收通知方式', configKey: 'mchNotifyPostType', configVal : vdata.mchNotifyPostType }
                      ]

      req.updateById(API_URL_MCH_CONFIG, vdata.groupKey, {mchNo: vdata.recordId, configData: JSON.stringify(configItem)}).then(res => {
        $infoBox.message.success('修改成功')
        vdata.btnLoading = false
      }).catch(res => {
        vdata.btnLoading = false
      })
    })
  }

  function confirmByDivisionManage(){

    $infoBox.confirmPrimary('确认修改分账设置？', '', () => {
      vdata.btnLoading = true // 打开按钮上的 loading

      let reqObject : any = JSON.parse(JSON.stringify(vdata.divisionConfig))
      reqObject.autoDivisionRules.amountLimit = Number.parseInt((reqObject.autoDivisionRules.amountLimit * 100) + '')

      req.updateById(API_URL_MCH_CONFIG, vdata.groupKey, {mchNo: vdata.recordId, configData: [{configKey: 'divisionConfig', configName: '分账管理', configVal: JSON.stringify(reqObject)}]}).then(res => {
        $infoBox.message.success('修改成功')
        vdata.btnLoading = false
      }).catch(res => {
        vdata.btnLoading = false
      })
    })

  }
  function confirmByAdvertManage(){

    $infoBox.confirmPrimary('确认修改广告设置？', '', () => {
      vdata.btnLoading = true // 打开按钮上的 loading

      let reqObject : any = JSON.parse(JSON.stringify(vdata.advertConfig))

      req.updateById(API_URL_MCH_LIST, vdata.recordId, reqObject).then(res => {
        $infoBox.message.success('修改成功')
        vdata.btnLoading = false
      }).catch(res => {
        vdata.btnLoading = false
      })
    })

  }

  function confirmBySelfCashierManage(){

    $infoBox.confirmPrimary('确认修改便捷收银台设置？', '', () => {
      vdata.btnLoading = true // 打开按钮上的 loading

      let arr : any = []
      arr.push(vdata.selfCashierState)
      arr.push(vdata.selfCashierSiteInfoType)
      arr.push(vdata.selfCashierWxH5Config)
      arr.push(vdata.selfCashierAliWapConfig)

      let reqObject : any = JSON.stringify(arr)

      req.updateById(API_URL_MCH_CONFIG, vdata.groupKey, {mchNo: vdata.recordId, configData: reqObject}).then(res => {
        vdata.btnLoading = false
        $infoBox.message.success('修改成功')
      }).catch(res => {
        vdata.btnLoading = false
      })
    })

  }

  function confirmByWebCashierManage(){

    $infoBox.confirmPrimary('确认修改WEB收银台设置？', '', () => {
      vdata.btnLoading = true // 打开按钮上的 loading

      let arr : any = []
      arr.push(vdata.webCashierState)
      arr.push(vdata.webCashierSiteInfoType)
      arr.push(vdata.webCashierWxH5Config)
      arr.push(vdata.webCashierAliWapConfig)

      let reqObject : any = JSON.stringify(arr)

      req.updateById(API_URL_MCH_CONFIG, vdata.groupKey, {mchNo: vdata.recordId, configData: reqObject}).then(res => {
        vdata.btnLoading = false
        $infoBox.message.success('修改成功')
      }).catch(res => {
        vdata.btnLoading = false
      })
    })

  }

  // 更新会员配置
function confirmUpdateMemberConfig(){
  $infoBox.confirmPrimary('确认修改会员配置吗？', '', () => {
    vdata.btnLoading = true // 打开按钮上的 loading

    let reqObject : any = JSON.parse(JSON.stringify(vdata.memberConfigData))

    // 最大储值金额
    reqObject.forEach(item => {
      if (item.configKey == 'mbrMaxBalance') {
        const mbrMaxBalance = item.configVal ? item.configVal : 0
        item.configVal = Number.parseInt((mbrMaxBalance * 100).toFixed(0)).toString()
      }
    })

    req.updateById(API_URL_MCH_CONFIG, vdata.groupKey, {mchNo: vdata.recordId, configData: reqObject}).then(res => {
      $infoBox.message.success('修改成功')
      vdata.btnLoading = false

      selectTabs('memberConfig')
    }).catch(res => {
      vdata.btnLoading = false
    })
  })
}


    // 确认更新
  function confirmByApiEnt (e) {

    $infoBox.confirmPrimary('确认修改商户的接口权限？', '', () => {
      vdata.btnLoading = true // 打开按钮上的 loading

      let arr : any = []
      vdata.mchApiEntTableData.forEach(r => {
          if(r.checked){
            arr.push(r.key)
          }
      })

      let configItem = [{ groupKey: vdata.groupKey, configName: '商户接口权限集合', configKey: 'mchApiEntList', configVal : JSON.stringify(arr) }]

      req.updateById(API_URL_MCH_CONFIG, vdata.groupKey, {mchNo: vdata.recordId, configData: JSON.stringify(configItem)}).then(res => {
        $infoBox.message.success('修改成功')
        vdata.btnLoading = false
      }).catch(res => {
        vdata.btnLoading = false
      })
    })
  }


   // 全选，反选
  function notifyAllCehcked(isChekced){
      vdata.notifyTableData.filter(r => !r.disabled).forEach(r => {
        r.checked = isChekced
      })
  }

  // 全选，反选
  function mchApiEntAllCehcked(isChekced){
    vdata.mchApiEntTableData.filter(r => !r.disabled).forEach(r => {
      r.checked = isChekced
    })
  }

 // 变更 appId的事件
 function changeAppId (value) {
    immediatelyPay()
  }
  // 变更 门店ID的事件
  function changeStoreId (value) {
    immediatelyPay()
  }
  // 立即支付按钮
  function immediatelyPay () {

    // 判断是否选择门店
    if (vdata.appId === '') {
      vdata.isShowCashier = false
      return false
    }
    // 判断是否选择门店
    if (vdata.storeId === '') {
      vdata.isShowCashier = false
      return false
    }

    vdata.isShowCashier = true
    // $mchConfigCreateCashier({
    //   appId: vdata.appId, // appId
    //   storeId: vdata.storeId, // storeId
    //   mchNo: vdata.recordId
    // }).then(res => {
    //   vdata.cashierUrl = res
    // }).catch(() => {
    //   payTestBarCode.value.processCatch()
    // })
  }

defineExpose({
  show
})
</script>
<style lang="less" scoped>
  .bottom-btn{
    /deep/ div{
      display: flex;
      justify-content: center;
    }
  }
  .typePopover {
  position: absolute;
  top: -30px;
  left: 93px;
}
</style>
