<template>
  <a-layout :class="$style.page">
    <a-layout-header :class="$style.header">
      <div :class="$style.headerTitle" :title="projectName"
        >{{ projectName }}
        <!-- <a-button
          v-if="moduleflag"
          type="link"
          size="small"
          :title="'关闭'"
          :class="$style.optbutton"
          @click="fndeal"
        > -->
        <!-- <img src="./gb.png" alt="" style="width: 15px;margin-top: -5px;" /> -->
        <!-- </a-button> -->
      </div>
      <a-menu
        v-if="loadShow"
        mode="horizontal"
        :default-selected-keys="defaultselectedkey"
        :style="{
          lineHeight: '48px',
          background: 'transparent',
          color: '#FFFFFF',
        }"
        @click="menuClick"
      >
        <a-menu-item key="1"> 项目作业</a-menu-item>
        <a-menu-item key="2"> 项目成员</a-menu-item>
        <a-menu-item v-if="showSubProject" key="3"> 项目分组</a-menu-item>
      </a-menu>
      <!-- <div>
        <a-icon type="close" :class="$style.closeBtn" @click="close"></a-icon>
      </div> -->
    </a-layout-header>
    <a-layout v-if="!iframeTag" style="padding:20px;background: #fff;" class="sd-has-table">
      <router-view></router-view>
    </a-layout>
    <a-layout v-if="iframeTag" style="padding:20px 20px 20px 0">
      <a-layout-sider
        theme="light"
        width="200"
        :class="[
          fold ? $style.leftSiderHidden : null,
          $style.leftSider,
          { [$style.leftOver]: !fold },
        ]"
        style="padding:0;background: #fff;"
      >
        <div :class="$style.siderContent">
          <div :class="$style.siderTitleDivFirst">
            <div :class="$style.siderTitle">
              <a-icon type="file-done" style="color:#7c7c7c" />
              准备阶段
            </div>
          </div>
          <a-spin :spinning="spinning">
            <a-timeline :class="[spinning ? $style.timeline : '']">
              <div
                v-for="node in preparaNodes"
                :key="node.id"
                :class="[$style.siderUlDiv, { checked: checkedId === node.nodePeriod }]"
                @click="setChecked(node.nodePeriod, node.nodeUrl, node, 1)"
              >
                <a-timeline-item :class="$style.siderLi">{{ node.nodeName }}</a-timeline-item>
              </div>
            </a-timeline>
          </a-spin>
        </div>
        <div :class="$style.siderContent">
          <div :class="$style.siderTitleDiv">
            <div :class="$style.siderTitle">
              <a-icon type="profile" style="color:#7c7c7c" />
              实施阶段
              <a-icon
                v-show="projectStage === 1 && showChangeStatus"
                type="swap"
                :class="$style.changeStatus"
                @click="changeStatus('effect')"
              />
            </div>
          </div>
          <a-spin :spinning="spinning">
            <a-timeline :class="[spinning ? $style.timeline : '']">
              <div
                v-for="node in effectNodes"
                :key="node.id"
                :class="[$style.siderUlDiv, { checked: checkedId === node.nodePeriod }]"
                @click="setChecked(node.nodePeriod, node.nodeUrl, node, 2)"
              >
                <a-timeline-item :class="$style.siderLi">{{ node.nodeName }}</a-timeline-item>
              </div>
            </a-timeline>
          </a-spin>
        </div>
        <div :class="$style.siderContent">
          <div :class="$style.siderTitleDiv">
            <div :class="$style.siderTitle">
              <a-icon type="file-text" style="color:#7c7c7c" />
              报告阶段
              <a-icon
                v-show="projectStage === 2 && showChangeStatus"
                type="swap"
                :class="$style.changeStatus"
                @click="changeStatus('report')"
              />
            </div>
          </div>
          <a-spin :spinning="spinning">
            <a-timeline>
              <div
                v-for="node in reportNodes"
                :key="node.id"
                :class="[$style.siderUlDiv, { checked: checkedId === node.nodePeriod }]"
                @click="setChecked(node.nodePeriod, node.nodeUrl, node, 3)"
              >
                <a-timeline-item :class="$style.siderLi">{{ node.nodeName }}</a-timeline-item>
              </div>
            </a-timeline>
          </a-spin>
        </div>
      </a-layout-sider>
      <a-layout-sider width="20" style="background: #f0f2f5">
        <a-button type="primary" :class="$style.fold" @click="foldClick">
          <a-icon :type="fold ? 'right' : 'left'" />
        </a-button>
      </a-layout-sider>
      <a-layout :class="['sd-has-table', $style.xmCpmProject]">
        <!-- <iframe ref="rightfrm" :class="$style.rightfrm" frameborder="0"></iframe> -->
        <router-view
          v-if="ifRouterView"
          name="rightfrm"
          :module-id="moduleId"
          :form-list-id="formListId"
          :custom-values="customValues"
          :filter="Expressions"
        ></router-view>
      </a-layout>
    </a-layout>
  </a-layout>
</template>

<script>
import { getUserInfo } from '@/common/store-mixin'
import CpmService from '../cpm-service'
import components from './_import-components/cpm-project-import'

export default {
  name: 'CpmProject',
  metaInfo: {
    title: '合规项目',
  },
  components: {
    ...components,
  },
  mixins: [],
  provide() {
    return {
      reload: this.reload,
    }
  },
  data() {
    return {
      ifRouterView: true,
      menuId: '',
      defaultselectedkey: ['1'],
      fold: false,
      spinning: true, // 左侧节点加载状态
      iframeTag: true,
      projectId: '', // 项目Id
      groupId: '', // 分组Id
      projectStatus: '',
      checkedId: '', // 默认选中节点id
      preparaNodes: [
        {
          id: 1,
          nodePeriod: 'cpm-check-plan',
          nodeUrl: 'moduleId=267018022657347584&formListId=267018023269715968',
          nodeName: '合规检查方案',
          moduleId: '267018022657347584',
          formListId: '267018023269715968',
        },
        {
          id: 2,
          nodePeriod: 'cpm-check-notice',
          nodeUrl: 'moduleId=267018517979484160&formListId=267018518503772160',
          nodeName: '合规检查通知',
          moduleId: '267018517979484160',
          formListId: '267018518503772160',
        },
      ], // 准备极端
      effectNodes: [
        {
          id: 1,
          nodePeriod: 'cpm-check-self',
          nodeUrl: 'moduleId=261598158822875136&formListId=261598159410077696',
          nodeName: '合规自查',
          moduleId: '261598158822875136',
          formListId: '261598159410077696',
        },
        {
          id: 2,
          nodePeriod: 'cpm-work-papre',
          nodeUrl: 'moduleId=260200672874090496&formListId=260200678972608512',
          nodeName: '工作底稿',
          moduleId: '260200672874090496',
          formListId: '260200678972608512',
        },
        {
          id: 3,
          nodePeriod: 'cpm-confirm-list',
          nodeUrl: 'moduleId=272824893662429184&formListId=273908685835730944',
          nodeName: '问题清单确认',
          moduleId: '272824893662429184',
          formListId: '273908685835730944',
        },
      ], // 实施阶段
      reportNodes: [
        {
          id: 1,
          nodePeriod: 'cpm-work-report',
          nodeUrl: 'moduleId=269255411016146944&formListId=269255416636514304',
          nodeName: '报告管理',
          moduleId: '269255411016146944',
          formListId: '269255416636514304',
        },
      ], // 报告
      projectStage: 1, // 项目阶段1准备2实施3报告
      projectName: '', // 项目名称,
      currentUserType: [], // 当前登录人所在项目角色
      loadShow: true,
      showChangeStatus: false, // 切换阶段按钮显示
      type: '',
      moduleflag: false,
      todoId: '',
      moduleId: '',
      formListId: '',
      customValues: null,
      Expressions: [],
      showSubProject: false, // 是否显示项目分组
    }
  },
  created() {
    const projectId = this.$route.query.projectId
    this.projectId = projectId
    const groupId = this.$route.query.groupId
    this.groupId = groupId
    this.moduleId = this.$route.query.moduleId
    this.formListId = this.$route.query.formListId
    this.initData(this.projectId)
    if (
      this.$route.path.indexOf('cpm-check-user') > -1 ||
      this.$route.path.indexOf('cpm-check-group') > -1
    ) {
      if (this.$route.path.indexOf('cpm-check-user') > -1) {
        this.defaultselectedkey = ['2']
        this.menuId = '2'
      } else if (this.$route.path.indexOf('cpm-check-group') > -1) {
        this.defaultselectedkey = ['3']
        this.menuId = '3'
      }
    }

    if (this.groupId !== '') {
      this.customValues = { sdCustomField1: this.projectId, sdCustomField2: this.groupId }
    } else {
      this.customValues = { sdCustomField1: this.projectId, sdCustomField2: null }
    }
  },
  mounted() {
    this.spinning = !this.spinning
    var arr = []
    arr = arr.concat(this.preparaNodes)
    arr = arr.concat(this.effectNodes)
    arr = arr.concat(this.reportNodes)
    arr.forEach((i) => {
      if (this.$route.fullPath.indexOf(i.nodeUrl) > -1) {
        this.checkedId = i.nodePeriod
      }
    })
  },
  destroyed() {
    window.removeEventListener('hasLeaderAuthority', this.hasLeaderAuthority)
  },
  methods: {
    // 初始化项目信息
    initData(projectId) {
      if (projectId) {
        // 获取项目信息
        CpmService.getProjectData(projectId).then((res) => {
          this.projectName = res.data[0].title
          // 判断是否显示项目分组
          const userId = res.data[0].submitterAccounts.split(',')
          const userInfo = getUserInfo()
          if (this.groupId === '1' && userId.includes(userInfo.account)) {
            this.showSubProject = true
          } else {
            this.showSubProject = false
          }
        })
      }
    },
    reload() {
      this.ifRouterView = false
      this.$nextTick(() => {
        this.ifRouterView = true
      })
    },

    // // 隐藏左侧节点
    foldClick() {
      this.fold = !this.fold
    },
    // // 是否拥有项目组长和项目副组长的权限
    // hasLeaderAuthority() {
    //   if (this.currentUserType.indexOf('01') > -1 || this.currentUserType.indexOf('05') > -1) {
    //     return true
    //   }
    //   return false
    // },
    setChecked(val, url, node, stage) {
      // 如果点击的阶段大于当前阶段，则不能点击
      this.projectId = this.$route.query.projectId
      this.groupId = this.$route.query.groupId
      this.moduleId = node.moduleId
      this.formListId = node.formListId
      url = url + '&projectId=' + this.projectId + '&groupId=' + this.groupId
      this.checkedId = val
      // if (
      //   node.nodeName === '工作底稿' ||
      //   node.nodeName === '合规自查' ||
      //   node.nodeName === '问题清单确认' ||
      //   node.nodeName === '报告管理'
      // ) {
      if (this.groupId !== '') {
        this.customValues = { sdCustomField1: this.projectId, sdCustomField2: this.groupId }
        this.Expressions = []
      } else {
        this.customValues = { sdCustomField1: this.projectId, sdCustomField2: null }
        this.Expressions = []
      }
      // } else {
      //   this.customValues = { sdCustomField1: this.projectId }
      //   this.Expressions = []
      // }
      this.$router.push('/cpm-project/' + val + '?' + url)
      this.reload()
    },
    // close() {
    //   crossWindowWatcher.notifyChange(true)
    //   window.close()
    // },
    menuClick(val) {
      this.menuId = val.key
      if (val.key === '1') {
        // 评价作业
        this.iframeTag = true
        setTimeout(() => {
          // 调整左侧节点选中状态
          this.checkedId = this.preparaNodes[0].nodePeriod
          // 修改跳转路由
          this.$router.push(
            '/cpm-project/' +
              this.preparaNodes[0].nodePeriod +
              '?' +
              this.preparaNodes[0].nodeUrl +
              '&projectId=' +
              this.projectId +
              '&groupId=' +
              this.groupId
          )
        }, 0)
      }
      if (val.key === '2') {
        // 成员
        this.iframeTag = false
        setTimeout(() => {
          // onl%3Acd78040cdda84c6b90b2c688d9bfbaa1
          // bdd32992af534987b2e6e4818972bb77
          this.$router.push(
            '/cpm-project/cpm-check-user?pageId=onl:cd78040cdda84c6b90b2c688d9bfbaa1&projectId=' +
              this.projectId +
              '&groupId=' +
              this.groupId // 新页面要打开的路由地址
          )
        }, 0)
      }
      if (val.key === '3') {
        // 分组
        this.iframeTag = false
        setTimeout(() => {
          this.$router.push(
            '/cpm-project/cpm-check-group?pageId=onl:cd78040cdda84c6b90b2c688d9bfbaa1&projectId=' +
              this.projectId +
              '&groupId=' +
              this.groupId // 新页面要打开的路由地址
          )
        }, 0)
      }
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;

.fold {
  position: absolute;
  top: calc(50% - 30px);
  right: 5px;
  z-index: 2;
  width: 15px;
  height: 75px;
  padding: 0;
  border-radius: 0 10px 10px 0;
}

.audit-table {
  :global(.sd-readonly) {
    margin-bottom: 24px;
  }
}

.close-btn {
  font-size: 15px;
  color: #ffffff;
  position: absolute;
  right: 15px;
  top: 25px;
  cursor: pointer;
}

$link-icon-size: 22px;
$line-height-theme: 66px;
$theme-check: #fff;
$icon-size: 15px;

.page {
  // 表格样式
  :global(div.sd-has-table) {
    :global(.ant-table-wrapper) {
      margin-top: -8px;
    }

    :global(.ant-card-body) {
      padding: 20px;

      :global(.ant-table-wrapper) {
        margin-top: -8px;
      }
    }
  }

  :global(.sd-has-table) {
    :global(.ant-table-wrapper) {
      margin-top: -8px;
    }
  }

  height: 100%;

  .left-sider {
    overflow: hidden;
    white-space: nowrap;

    :global(.ant-layout-sider-children) {
      overflow: auto;
    }
  }

  .left-sider-hidden {
    flex: 0 0 0 !important;
    max-width: 0 !important;
    min-width: 0 !important;
    width: 0 !important;
    padding: 0px;
    background: rgb(255, 255, 255);

    :global(.ant-layout-sider-children) {
      overflow: hidden;
    }
  }
  .left-over {
    margin-left: 20px;
  }

  .trigger {
    display: inline-block;
    padding: 0 15px;
    vertical-align: top;
    cursor: pointer;
    transition: color 0.3s;

    &:hover {
      color: $text-color-inverse;
    }
  }

  aside {
    overflow: auto;
  }

  :global(.sd-frame-main) {
    height: 100%;
    padding: $padding-lg;
    overflow: auto;
  }

  :global(sd-readonly) {
    margin-bottom: 24px;
  }
}

.title {
  display: inline-block;
  margin-left: -35px;
  font-weight: 600;
  line-height: inherit;
  text-align: center;
  vertical-align: top;

  > i {
    // 固定大小的logo
    /* stylelint-disable-next-line */
    font-size: 20px;
    transform: scale(10);
  }
}

// 主内容区的Card白色背景要占满
:global .sd-frame-main > .ant-card {
  min-height: 100%;
}

.header {
  background: linear-gradient(to right, #3f9bff, #0e7df6);
  height: 48px;
  line-height: 48px;
  padding: 0 20px 0 20px;

  :global .ant-menu {
    float: right;
  }

  :global(.ant-menu-item-selected) {
    color: #ffffff;
    background: #1366c1;
    border-bottom: 0 !important;
    top: 0;
  }

  :global(.ant-menu-item) {
    border-bottom: 2px solid #ffffff;
    border-bottom: 0 !important;
    top: 0;
  }

  :global(.ant-menu-item-active) {
    color: #ffffff !important;
    background: #1366c1 !important;
    border-bottom: 0 !important;
    top: 0;
  }

  .header-title {
    color: #ffffff;
    font-size: 25px;
    font-weight: 400;
    display: inline-block;
    width: 40%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

.header > div:last-child {
  width: calc(100% - 200px);
}

.links {
  display: flex;
  align-items: center;
  margin: 0 $link-icon-size / 2;

  li:global(.ant-menu-item) {
    padding: 0;
  }

  i:global(.anticon) {
    padding: 0 $link-icon-size / 2;
    margin: 0;
    font-size: $link-icon-size;
    line-height: $layout-header-height;
    vertical-align: middle;

    &:hover {
      // 悬浮的颜色，与主题无关
      /* stylelint-disable-next-line */
      background-color: rgba(0, 0, 0, 0.2);
    }
  }
}

.tools {
  display: flex;
  float: right;
}

.headerbadge {
  bottom: $line-height-theme / 6;
  left: -$link-icon-size / 2 - 5;
}

.sider-content {
  padding: 0 0;

  .sider-title {
    position: relative;
    margin-left: 23px;

    i {
      color: #1890ff;
      position: absolute;
      font-size: 18px;
      left: -27px;
      top: 3px;
    }

    font-weight: bold;
  }

  .sider-li {
    :global(.ant-timeline-item-content) {
      margin: 0 0 0 15px;
    }

    :global(.ant-timeline-item-head-blue) {
      color: #9f9f9f;
      border-color: #9f9f9f;
      background-color: #9f9f9f;
      top: 4px;
      left: 3px;
      width: 3px;
      height: 3px;
    }

    :global(.ant-timeline-item-tail) {
      height: calc(100% - -18px);
      border-left: 2px solid #f5f5f5;
      display: none;
    }
  }

  .sider-title-div-first {
    padding: 10px 0 10px 20px;
    margin-bottom: 10px;
    border-bottom: 2px solid #e8e8e8;
  }

  .sider-title-div {
    padding: 10px 0 10px 20px;
    margin-bottom: 10px;
    border-top: 2px solid #e8e8e8;
    border-bottom: 2px solid #e8e8e8;
  }

  ul {
    margin-left: 0;
    padding-bottom: 5px;
  }

  .sider-ul-div {
    padding: 13px 0px 0px 12px;
    width: 100%;
    margin: 3px auto;
    border-radius: 0px;
    cursor: pointer;

    :global(.ant-timeline-item) {
      padding: 0;
      margin-left: 3px;
    }
  }

  .node-readonly {
    cursor: normal;
    color: #b6b6b6;
  }

  :global(.checked) {
    background: linear-gradient(to right, #13c2c2, #2db7f5);
    color: #fff;

    :global(.ant-timeline-item-head-blue) {
      background-color: #ffffff;
      border-color: #ffffff !important;
      top: 4px;
      left: 3px;
      width: 3px;
      height: 3px;
    }
  }

  :global(.ant-timeline-item-last .ant-timeline-item-tail) {
    display: none;
  }

  .timeline {
    min-height: 150px;
  }
}

:global(.ant-tabs) {
  overflow-y: auto;
}

.rightfrm {
  height: 100%;
  width: 100%;
}

.change-status {
  margin-left: 150px;
  font-size: 20px !important;
  color: #28baeb !important;
  cursor: pointer;
}
.xmCpmProject {
}
</style>
