<template>
  <div>
    <a-tabs v-model="activatedTab" type="card" style="height: 100%;">
      <a-tab-pane v-for="tab in tabsForSjdw" :key="tab.value" :tab="tab.label">
        <component :is="activatedTab" :pType="pType" style="height: 100%;" :userRoles="userRoles"/>
      </a-tab-pane>
    </a-tabs>
    <a-modal
      title="提示"
      v-model:visible="visible"
      @cancel="handleCancel"
      :closable="false"
    >
      <p>工作提示：问题延期申请，须在原整改时限到期前10天以上提出，否则本系统将无法进行延期操作！！！</p>
      <p>2026年1月起，取消问题超期记录申诉功能，请大家积极按期完成整改！</p>
      <template #footer>
        <a-button @click="visible = false">确定</a-button>
      </template>
    </a-modal>
  </div>
</template>

<script>
import {
  distinct,
  getAuthenticatedTabs,
  requestUserRole,
} from './Common/rectify_api';
import { tabsForSjdw } from './Common/tabsForRectify';
import sjdwDoubleApprove from './sjdw_DoubleApprove.vue';
import sjdwDoubleCheck from './sjdw_DoubleCheck.vue';
import sjdwPblmView from './sjdw_pblmView.vue';
import sjdwPendingApprove from './sjdw_pendingApprove.vue';
import sjdwPendingRectify from './sjdw_pendingRectify.vue';
import sjdwUnderRectify from './sjdw_UnderRectify.vue';
import sjdwOverdueAppeal from './sjdwOverdueAppeal.vue';
import sjdwOverdueAppeal1 from './sjdwOverdueAppeal1.vue';
import sjdwOverDate from './xjdwOverDate.vue';
export default {
  name: 'SjdwIndex',
  components: {
    sjdwPendingRectify,
    sjdwUnderRectify,
    sjdwPendingApprove,
    sjdwPblmView,
    sjdwDoubleCheck,
    sjdwDoubleApprove,
    sjdwOverdueAppeal,
    sjdwOverdueAppeal1,
    sjdwOverDate,
  },
  props: [],
  data() {
    return {
      tabsForSjdw: [],
      activatedTab: 'none',
      pType: 1,
      userId: localStorage.getItem('persId'),
      visible: false,
      readOnlyRole: localStorage.getItem('readOnlyRole'),
      userRoles: [],
    };
  },
  created() {
    this.visible = true;
    this.getAuthenticatedTabs(this.userId, tabsForSjdw);
  },
  methods: {
    handleTabClick(tab, event) {
      this.activatedTab = tab.name;
    },
    getAuthenticatedTabs(userId, tabs) {
      const currentUser = { userId: userId };
      let userRoles = [];
      this.userRoles = [];
      requestUserRole(currentUser).then((res) => {
        if (res.data != null) {
          userRoles = res.data; // 当前用户的所有角色列表
          if (userRoles && userRoles.length > 0) {
            userRoles.forEach((item, index) => {
              this.userRoles.push(item.roleId);
            });
          }
          let authenticatedTabs = [];
          let tempTabs = [];
          var flag = false;
          userRoles.forEach((userRole) => {
            // 用户所有角色可见的标签的集合
            tempTabs = tabs.filter(
              (tab) =>
                tab.roles.indexOf(userRole.roleId) > -1 ||
                tab.roles.length === 0,
            );
            tempTabs.forEach((tab) => {
              authenticatedTabs.push(tab);
            });
            if (userRole.roleId == '66a6e697fe3e4d02b79f9f90771a4b68') {
              flag = true;
            }
          });
          if (flag) {
            authenticatedTabs = tabs;
          }
          // 去除重复的标签
          authenticatedTabs = distinct(authenticatedTabs);
          if (this.readOnlyRole == 'hide') {
            this.tabsForSjdw = [
              {
                value: 'sjdwPblmView',
                label: '问题查看',
                roles: [], // 督查处所有角色可见
              },
            ];
          } else {
            this.tabsForSjdw = authenticatedTabs;
          }
          if (authenticatedTabs.length > 0) {
            this.activatedTab = this.tabsForSjdw[0].value; // 默认显示第一个标签
          }
        }
      });
    },
    handleCancel() {
      this.visible = false;
    },
  },
};
</script>

<style lang='less' scoped>

</style>
