<template>
  <a-drawer
    :visible="vdata.visible"
    :title="'功能配置'"
    width="40%"
    :maskClosable="false"
    @close="onClose"
  >
    <a-form ref="infoFormModel" :model="vdata.saveObject" layout="vertical" :rules="vdata.rules">
        <a-col :span="24">
          <a-form-item label="功能" name="state">
            <a-checkbox-group v-model:value="vdata.saveObject['funcCodes']">
              <a-checkbox v-for="(value) in vdata.funcCodesList" :key="value.key"   :value="value.key">{{ value.desc }}</a-checkbox>
            </a-checkbox-group>
          </a-form-item>
        </a-col>
    </a-form>

    <div class="drawer-btn-center">
      <a-button :style="{ marginRight: '8px' }" @click="onClose"><close-outlined />取消</a-button>
      <a-button type="primary" :loading="vdata.btnLoading" @click="onSubmit"><check-outlined />保存</a-button>
    </div>
  </a-drawer>
</template>

<script setup lang="ts">
import {
  API_URL_MCH_APP,
  req,
  $getSysRSA2PublicKey,
  $getMchAppAllCapability,$getMchAppConfigAppCapability,

} from '@/api/manage'
import {ref, reactive, getCurrentInstance} from 'vue'
const { $SYS_NAME_MAP,$infoBox } = getCurrentInstance()!.appContext.config.globalProperties

const props = defineProps({
  callbackFunc: { type: Function, default: () => () => ({}) }
})
  
const vdata : any =  reactive({
  funcCodesList:[] as any,
  isAdd: true, // 新增 or 修改
  visible: false, // 抽屉开关
  appId: '', // 应用AppId
  saveObject: {}, // 数据对象
  btnLoading:false,
  sysRSA2PublicKey: '',
  recordInfo:{},
})

// 表单组件
const infoFormModel = ref()

// 抽屉显示
const show = (record) => {
  vdata.isAdd = !record
  vdata.recordInfo = record
  vdata.appId = record.appId
  // 数据清空
  vdata.saveObject = {
    'funcCodes': [] as any,
  }
  if(record.capabilities){
     for (var i=0;i<record.capabilities.length;i++){
       console.log(record.capabilities[i].key,'record.capabilitiesrecord.capabilitiesrecord.capabilities')
       vdata.saveObject.funcCodes.push(record.capabilities[i].key);
     }
  }
  if (infoFormModel.value !== undefined) {
    infoFormModel.value.resetFields()
  }

  $getMchAppAllCapability({}).then((res) => {
    vdata.funcCodesList = res
  })


  vdata.visible = true

}
// 表单提交
const onSubmit = () => {

  // 处理签名方式
  vdata.saveObject.appSignType = JSON.stringify(vdata.saveObject.appSignTypeObject)
  
  infoFormModel.value.validate().then(() => {

    $getMchAppConfigAppCapability({funcCodes:vdata.saveObject.funcCodes,mchAppId:vdata.appId}).then((res) => {
      $infoBox.message.success('保存成功')
    })

  })
}


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
}

const onClose = () => {
  vdata.visible = false
}

defineExpose({ show })
</script>

