<template>
  <div class="analysis-report-view">
    <div class="part-box part-box-pad">
      <el-button
        v-for="item in roles"
        :key="item.roleId"
        type="primary"
        class="auth-item"
        :disabled="loading"
        @click="toAuth(item)"
        >{{ item.roleName }}</el-button
      >
    </div>
  </div>
</template>

<script>
import { autoSubmitForm } from "@/plugins/utils";
import { ssoAnalysisLogin } from "../api";

export default {
  name: "analysis-report-view",
  data() {
    return {
      user: {},
      roles: [],
      loading: false,
    };
  },
  created() {
    this.user = this.$ls.get("user", {
      roleSource: [],
      loginName: "",
      role: "",
      orgName: "",
    });
    this.roles = this.user.roleSource.filter(
      (item) => item.roleSource === "ANALYSIS"
    );
    if (this.$route.query.trigger) {
      this.toAuth({ roleCode: this.$route.query.trigger });
    }
  },
  methods: {
    async toAuth(item) {
      if (this.loading) return;
      this.loading = true;

      const params = {
        loginName: this.user.loginName,
        role: item.roleCode,
        orgName: this.user.orgInfo.name,
        returnUrl: window.location.href,
      };
      const data = await ssoAnalysisLogin(params).catch(() => {});
      this.loading = false;
      if (!data) return;

      console.log(data);
      // if (data) return;

      autoSubmitForm(data.redirectUrl, data);
    },
  },
};
</script>
