<template>
  <el-dialog title="证书激活" v-model="visible" :close-on-click-modal="false" width="40%">
    <el-form ref="formRef" :model="addData">
      <el-form-item label="激活码：" prop="licenseMask" :rules="[required]" maxlength="10240">
        <el-input type="textarea" v-model="addData.licenseMask" :rows="15"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="ok" :loading="loading">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { installCaptcha } from 'services/license'
import { required } from '@/validate'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
// 单个传 sinple   多个 multiple
export default {
  setup() {
    const loading = ref(false)
    const formRef = ref()
    const addData = reactive({
      licenseMask: ''
    })
    const visible = ref(false)
    function open() {
      addData.licenseMask = ''
      visible.value = true
    }
    const router = useRouter()
    async function ok() {
      formRef.value.validate(async valid => {
        if (valid) {
          loading.value = true
          const data = await installCaptcha(addData)
          loading.value = false
          if (data.success) {
            ElMessage.success(data.message)
            router.replace('/login')
          }
        }
      })
    }
    return {
      visible,
      required,
      loading,
      formRef,
      addData,
      ok,
      open
    }
  }
}
</script>
