<template>
  <div style="background: #fff;padding: 20px 30px">
    <div class="account-settings-info-view">
      <a-form ref="configFormModel" class="edit-box" :rules="vdata.rules">
        <a-row  justify="space-between" type="flex">
            <a-divider orientation="left" style="color: #1A66FF;">基础设置</a-divider>

          <a-col :span="24">
            <a-form-item label="自动审核打款：" name="state" >
              <div style="display: grid">
                <a-radio-group v-model:value="vdata.saveObject['state']" style="margin-top:8px">
                  <a-radio :value="1">
                    开启
                  </a-radio>
                  <a-radio :value="0">
                   <span style="display: grid">
                      <span>关闭</span >
                     </span>
                  </a-radio>
                </a-radio-group>
                <span   class="jeepay-tip-text">请谨慎开启此功能：一旦开启，后续发起任务结算时将不再需要短信验证码进行确认，仅需输入支付密码即可完成。请在确保账户安全的前提下开启此功能</span >
              </div>
            </a-form-item>
          </a-col>

          <a-col :span="24">
            <a-form-item label="APi接口：" name="api" >
              <a-radio-group v-model:value="vdata.saveObject['api']" style="margin-top:8px">
                <a-radio :value="1">
                  开启
                </a-radio>
                <a-radio :value="0">
                  关闭
                </a-radio>
              </a-radio-group>
            </a-form-item>
          </a-col>
        </a-row>

            <div  v-if="vdata.saveObject.api == 1" style="width: 100%">

            <a-divider orientation="left" style="color: #1A66FF;">签名配置</a-divider>

              <a-col :span="24">
                <a-form-item name="appSignType"  >
                  <template #label>
                    <span>支持的签名方式</span> &nbsp;
                    <a-popover placement="top">
                      <template #title><span>签名方式</span></template>
                      <template #content>
                        <p>若需要使用系统测试或者{{ $SYS_NAME_MAP.MCH_APP }}APP则必须支持MD5， 若仅通过API调用则根据需求进行选择。 </p>
                      </template>
                      <question-circle-outlined />
                    </a-popover>
                  </template>

                  <a-checkbox-group v-model:value="vdata.saveObject.appSignTypeObject" style="margin-top:8px">
                    <a-checkbox value="MD5">MD5</a-checkbox>
                    <a-checkbox value="RSA2">
                       <span style="display: grid">
                          <span>RSA2</span >
                          <span style="margin-left:-25px;width: 250px" class="jeepay-tip-text">点此查看：<a href='https://shouyinbei.yuque.com/rv7pt7/ly9al3/oudphcwqibfo9gni' target="_blank">RSA2密钥配置流程</a></span >

                         </span>
                    </a-checkbox>
                  </a-checkbox-group>
                </a-form-item>
              </a-col>

              <a-col :span="24">
                  <a-form-item v-show="vdata.saveObject.appSignTypeObject && vdata.saveObject.appSignTypeObject.indexOf('MD5') >= 0" label="设置MD5秘钥" name="appSecret">
                    <a-textarea v-model:value="vdata.saveObject['appSecret']" :placeholder="vdata.saveObject['appSecret_ph']" type="textarea" />
                    <a-button type="primary" style="margin-top: 5px;" ghost @click="randomKey(false, 128, 0)"><file-sync-outlined />随机生成私钥</a-button>
                  </a-form-item>
              </a-col>


              <a-col :span="24">
                  <a-form-item v-show="vdata.saveObject.appSignTypeObject && vdata.saveObject.appSignTypeObject.indexOf('RSA2') >= 0" label="设置RSA2平台公钥" name="appRsa2PublicKey">
                    <a-textarea v-model:value="vdata.saveObject['appRsa2PublicKey']" type="textarea" />
                  </a-form-item>
              </a-col>

              <a-col :span="24">
                <a-form-item v-show="vdata.saveObject.appSignTypeObject && vdata.saveObject.appSignTypeObject.indexOf('RSA2') >= 0" label="支付网关系统公钥（回调验签使用）">
                  <a-textarea :rows="6" :value="vdata.sysRSA2PublicKey" :disabled="true" type="textarea" />
                </a-form-item>
              </a-col>
          </div>
        <a-form-item class="bottom-btn">
          <a-button v-if="$access('ENT_SYS_CONFIG_EDIT')"  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, $getSysRSA2PublicKey} from '@/api/manage'
import { reactive, onMounted, getCurrentInstance } from 'vue'

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

const vdata = reactive ({
  btnLoading: false,
  groupKey: 'applicationConfig',
  configData: {} as any, // 配置保存对象
  saveObject:{},
  rules: {
    state: [{required: true, message: '请选择经营场景', trigger: 'blur'}],
    api: [{required: true, message: '请填写特殊资质', trigger: 'blur'}],
    appSignTypeObject: [{required: true, message: '请填写特殊资质', trigger: 'blur'}],
  },
}) as any

onMounted(()=>{
  getSysRSA2PublicKey()
})
function  getSysRSA2PublicKey(){
// 查询系统公钥
  $getSysRSA2PublicKey().then((res) => {
    vdata.sysRSA2PublicKey = res
  })
}
// 确认更新
function confirm (e) {
  $infoBox.confirmPrimary('确认修改配置吗？', '', () => {
    vdata.btnLoading = true // 打开按钮上的 loading

    const reqData:any = {}
    for (var i in vdata.configData) {
      reqData[vdata.configData[i].configKey] = vdata.configData[i].configVal
    }

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

}
function randomKey(randomFlag, min, max) { // 生成随机128位私钥
  let str = ''
  let range = min
  const arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
  // 随机产生
  if (randomFlag) {
    range = Math.round(Math.random() * (max - min)) + min
  }
  for (var i = 0; i < range; i++) {
    var pos = Math.round(Math.random() * (arr.length - 1))
    str += arr[ pos ]
  }
  vdata.saveObject['appSecret'] = str
}

</script>
<style lang="less" scoped>
.bottom-btn{
  /deep/ div{
    display: flex;
    justify-content: center;
  }
}
.jeepay-tip-text:before{
  border-style:none;
}
.edit-box{
  padding: 20px 30px;
}
</style>
