<template>
  <div class="auth-set">
    <div class="part-box part-box-pad">
      <el-form ref="modalFormComp" :model="modalForm" label-width="120px">
        <el-form-item prop="loginName" label="当前信息：">
          {{ modalForm.expireTime === null ? "未授权" : "已授权" }}
        </el-form-item>
        <el-form-item label="过期时间：">
          <span v-if="modalForm.expireTime === -1">不过期</span>
          <span v-else-if="modalForm.expireTime">
            {{ modalForm.expireTime | timestampFilter }}</span
          >
          <span v-else>--</span>
        </el-form-item>
        <el-form-item prop="mobileNumber" label="导出：">
          <el-button type="primary" :loading="downloading" @click="toExport">
            硬件信息
          </el-button>
        </el-form-item>
        <el-form-item prop="code" label="导入授权文件：">
          <upload-button
            btn-content="选择文件"
            btn-type="primary"
            :upload-url="uploadUrl"
            :format="['lic']"
            accept=".lic"
            style="margin: 0"
            @valid-error="validError"
            @upload-success="uploadSuccess"
          >
          </upload-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { downloadByApi } from "@/plugins/download";
import { exportDeviceInfo, authSelect } from "../api";
import UploadButton from "@/components/UploadButton";

export default {
  name: "auth-set",
  components: { UploadButton },
  data() {
    return {
      modalForm: {
        expireTime: "",
        authFile: null,
      },
      downloading: false,
      // import
      uploadUrl: "/api/auth/offline/activation",
    };
  },
  created() {
    this.search();
  },
  methods: {
    async search() {
      const res = await authSelect();
      this.modalForm.expireTime = res;
    },
    async toExport() {
      if (this.downloading) return;
      this.downloading = true;

      const res = await downloadByApi(() => {
        return exportDeviceInfo();
      }).catch((e) => {
        this.$message.error(e || "下载失败，请重新尝试！");
      });
      this.downloading = false;

      if (!res) return;
      this.$message.success("下载成功！");
    },
    validError(errorData) {
      this.$message.error(errorData.message);
    },
    uploadSuccess() {
      this.search();
    },
  },
};
</script>
