<template>
  <el-form
    ref="authorizeFormRef"
    :model="authorizeFormData"
    :rules="authorizeRules"
    label-width="110px"
    class="authorizeForm">
    <el-form-item label="当前版本号" prop="system_version">
      <el-input disabled v-model="authorizeFormData.system_version" />
    </el-form-item>
    <el-form-item label="User" prop="system_user">
      <el-input placeholder="统一管理用户认证平台" v-model="authorizeFormData.system_user" maxlength="30" />
    </el-form-item>
    <el-form-item label="Key" prop="license">
      <el-input
        placeholder="请输入"
        rows="5"
        v-model="authorizeFormData.license"
        type="textarea"
        @blur="getLicenseInfo" />
    </el-form-item>
    <el-form-item label="有效期至" prop="license_dead_date">
      <el-input disabled v-model="authorizeFormData.license_dead_date" />
    </el-form-item>
    <el-form-item label="剩余有效期提醒" prop="license_inform_day">
      <el-input placeholder="请输入" v-model.trim.number="authorizeFormData.license_inform_day">
        <template #append>天</template>
      </el-input>
      <el-tooltip placement="top-start" effect="light">
        <template #content>
          范围（0~90），单位：天， 当剩余有效期在配置区间内时，系统会在用户登录系统时发出提醒，0表示不提醒
        </template>
        <div class="tip-image"></div>
      </el-tooltip>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { reactive, toRefs, ref } from "vue";
import { ElMessage } from "element-plus";
import axios from "@/request/http.js";

const props = defineProps({
  authorizeFormData: {
    type: Object,
    default: () => ({}),
  },
});
const state = reactive({
  authorizeRules: {
    license_inform_day: [{ type: "number", min: 0, max: 90, message: "请输入0~90的整数", trigger: "blur" }],
  },
});
const authorizeFormRef = ref(null);
//根据license获取授权信息
const getLicenseInfo = () => {
  if (!state.authorizeFormData.license) return;
  axios
    .post("/apaas/system/v5/sysOptions/getLicenseInfo", {
      license: state.authorizeFormData.license,
    })
    .then((res) => {
      if (res.data.code == 200) {
        const data = res.data.data || {};
        state.authorizeFormData.system_version = data.version;
        state.authorizeFormData.license_dead_date = data.deadDate;
      } else {
        ElMessage.error(res.data.data);
      }
    });
};
//保存
const save = () => {
  authorizeFormRef.value.validate((valid) => {
    if (valid) {
      params = {
        ...props.authorizeFormData,
      };
      axios.post("/apaas/system/v5/sysOptions/licenseOpts", params).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success(res.data.msg);
        } else {
          ElMessage.error(res.data.data);
        }
      });
    }
  });
};
const { authorizeRules } = toRefs(state);
defineExpose({
  save,
});
</script>
