<template>
  <div class="auth-set">
    <div class="part-box part-box-pad">
      <el-form
        ref="modalFormComp"
        :model="modalForm"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item prop="loginName" label="当前信息：">
          {{ AUTH_TYPE[authInfo.authMode] }}
        </el-form-item>
        <el-form-item label="过期时间：">
          <span v-if="authInfo.expireTime === -1">不过期</span>
          <span v-else-if="authInfo.expireTime">
            {{ authInfo.expireTime | timestampFilter }}</span
          >
          <span v-else>--</span>
        </el-form-item>
        <el-form-item label="授权模式：">
          <el-select v-model="mode" @change="modeChange">
            <el-option value="OFF_LINE" label="离线激活"></el-option>
            <el-option value="ON_LINE" label="在线激活"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="IS_OFFLINE_MODE" prop="mobileNumber" label="导出：">
          <el-button type="primary" :loading="downloading" @click="toExport">
            硬件信息
          </el-button>
        </el-form-item>
        <el-form-item v-if="IS_OFFLINE_MODE" 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-item
          v-if="!IS_OFFLINE_MODE"
          prop="accessKey"
          label="accessKey："
        >
          <el-input
            v-model.trim="modalForm.accessKey"
            placeholder="请输入accessKey"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item
          v-if="!IS_OFFLINE_MODE"
          prop="accessSecret"
          label="accessSecret："
        >
          <el-input
            v-model.trim="modalForm.accessSecret"
            placeholder="请输入accessSecret"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item v-if="!IS_OFFLINE_MODE">
          <el-button type="primary" :loading="loading" @click="confirm"
            >保存</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

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

export default {
  name: "auth-set",
  components: { UploadButton },
  data() {
    return {
      mode: "OFF_LINE",
      authInfo: { expireTime: -1, authMode: "NON_ACTIVATED" },
      AUTH_TYPE: {
        ON_LINE: "在线激活",
        OFF_LINE: "离线激活",
        NON_ACTIVATED: "未激活"
      },
      modalForm: {
        accessKey: "",
        accessSecret: ""
      },
      rules: {
        accessKey: [
          {
            required: true,
            message: "请输入秘钥",
            trigger: "change"
          },
          {
            message: "秘钥不能超过200个字符",
            max: 200,
            trigger: "change"
          }
        ],
        accessSecret: [
          {
            required: true,
            message: "请输入密匙",
            trigger: "change"
          },
          {
            message: "密匙不能超过200个字符",
            max: 200,
            trigger: "change"
          }
        ]
      },
      loading: false,
      downloading: false,
      // import
      uploadUrl: "/api/admin/auth/offline/activation"
    };
  },
  computed: {
    IS_OFFLINE_MODE() {
      return this.mode === "OFF_LINE";
    }
  },
  created() {
    this.search();
  },
  methods: {
    async search() {
      const res = await authSelect();
      this.authInfo = res || {};
      this.mode =
        this.authInfo.authMode === "NON_ACTIVATED"
          ? ""
          : this.authInfo.authMode;
      this.modalForm = this.$objAssign(this.modalForm, this.authInfo);
    },
    modeChange() {
      this.$nextTick(() => {
        this.$refs.modalFormComp.clearValidate();
      });
    },
    async confirm() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.loading) return;
      this.loading = true;
      let datas = { ...this.modalForm };
      const data = await onlineActivation(datas).catch(() => {});
      this.loading = false;
      this.search();

      if (!data) return;
      this.$message.success("操作成功！");
    },
    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>
