<template>
  <a-layout :class="$style.page">
    <a-layout-header :class="$style.header">
      <div :class="$style.headerTitle" :title="taskName">{{ taskName }}</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 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="menuId !== '3' ? 'padding:20px;background: #fff;' : 'background: #fff;'"
      class="sd-has-table"
    >
      <!--      <iframe ref="mainfrm" :class="$style.rightfrm" frameborder="0"></iframe>-->
      <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 v-if="firstNodes.length > 0" :class="$style.siderContent">
          <div :class="$style.siderTitleDivFirst">
            <div :class="$style.siderTitle">
              <a-icon type="file-search" style="color:#7c7c7c" />
              风险识别
            </div>
          </div>
          <a-spin :spinning="spinning">
            <a-timeline :class="[spinning ? $style.timeline : '']">
              <div
                v-for="node in firstNodes"
                :key="node.id"
                :class="[
                  $style.siderUlDiv,
                  { checked: checkedId === node.nodePeriod + '-' + node.id },
                ]"
                @click="setChecked(node.nodePeriod + '-' + node.id, node.nodeUrl)"
              >
                <a-timeline-item :class="$style.siderLi">{{ node.nodeName }}</a-timeline-item>
              </div>
            </a-timeline>
          </a-spin>
        </div>
        <div v-if="secondNodes.length > 1" :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 secondNodes"
                :key="node.id"
                :class="[
                  $style.siderUlDiv,
                  { checked: checkedId === node.nodePeriod + '-' + node.id },
                ]"
                @click="setChecked(node.nodePeriod + '-' + node.id, node.nodeUrl)"
              >
                <a-timeline-item :class="$style.siderLi">{{ node.nodeName }}</a-timeline-item>
              </div>
            </a-timeline>
          </a-spin>
        </div>
        <div v-if="threeNodes.length > 0" :class="$style.siderContent">
          <div :class="$style.siderTitleDiv">
            <div :class="$style.siderTitle">
              <a-icon type="file-protect" 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 threeNodes"
                :key="node.id"
                :class="[
                  $style.siderUlDiv,
                  { checked: checkedId === node.nodePeriod + '-' + node.id },
                ]"
                @click="setChecked(node.nodePeriod + '-' + node.id, node.nodeUrl)"
              >
                <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 fourNodes"
                :key="node.id"
                :class="[
                  $style.siderUlDiv,
                  { checked: checkedId === node.nodePeriod + '-' + node.id },
                ]"
                @click="setChecked(node.nodePeriod + '-' + node.id, node.nodeUrl)"
              >
                <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 style="padding:20px;background: #fff;" class="sd-has-table">
        <!-- <iframe ref="rightfrm" :class="$style.rightfrm" frameborder="0"></iframe> -->
        <router-view name="rightfrm"></router-view>
      </a-layout>
    </a-layout>
  </a-layout>
</template>

<script>
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import components from './_import-components/risk-tasks-work-import'
import riskTasksService from './risk-tasks-service'

export default {
  name: 'RiskTasksWork',
  metaInfo: {
    title: '风险任务',
  },
  components: {
    ...components,
  },
  mixins: [],
  data() {
    return {
      menuId: '',
      defaultselectedkey: ['1'],
      fold: false,
      spinning: true, // 左侧节点加载状态
      iframeTag: true,
      taskId: '',
      projectStatus: '',
      checkedId: '', // 默认选中节点id
      firstNodes: [], // 任务识别
      secondNodes: [], // 风险评估
      threeNodes: [], // 风险应对
      fourNodes: [
        {
          id: 1,
          nodePeriod: 'report',
          nodeUrl: 'risk-task-report-list',
          nodeName: '风险任务报告',
        },
      ], // 报告
      projectStage: 1, // 项目阶段1准备2实施3报告
      taskName: '', // 项目名称,
      currentUserType: [], // 当前登录人所在项目角色
      loadShow: true,
      showChangeStatus: false, // 切换阶段按钮显示
      type: '',
    }
  },
  created() {
    const taskId = this.$route.query.taskId
    if (
      this.$route.path.indexOf('risk-eventuser-list') > -1 ||
      this.$route.path.indexOf('ic-evaluation-overview') > -1
    ) {
      this.iframeTag = false
      if (this.$route.path.indexOf('ic-evaluation-overview') > -1) {
        this.defaultselectedkey = ['2']
        this.menuId = '2'
      } else if (this.$route.path.indexOf('risk-eventuser-list') > -1) {
        this.defaultselectedkey = ['3']
        this.menuId = '3'
      }
    }
  },
  mounted() {
    const taskId = this.$route.query.taskId
    if (taskId) {
      this.taskId = taskId
    }
    // 获取风险任务信息
    riskTasksService.getRiskTaskInfo(taskId).then((res) => {
      this.spinning = !this.spinning
      // 任务名称
      const title = res.data.pageFormData.pageFieldInfos.find((i) => i.name === 'dutieName').value
      // 任务类型
      const type = res.data.pageFormData.pageFieldInfos.find((i) => i.name === 'dutieType').value
      this.taskName = title
      this.type = type
      // 风险识别
      if (this.type.indexOf('RISK_DISTINGUISH') > -1) {
        this.firstNodes.push({
          id: 1,
          nodePeriod: 'identification',
          nodeUrl: 'risk-identification-list',
          nodeName: '风险识别',
        })
      }
      // 风险评估
      if (this.type.indexOf('RISK_ASSESSMENT') > -1) {
        this.secondNodes.push({
          id: 1,
          nodePeriod: 'assessment',
          nodeUrl: 'risk-assessment-list',
          nodeName: '风险评估',
        })
      }
      this.secondNodes.push({
        id: 2,
        nodePeriod: 'map',
        nodeUrl: 'risk-assessment-map',
        nodeName: '风险图谱',
      })
      // 风险识别
      if (this.type.indexOf('RISK_RESPONSE') > -1) {
        this.threeNodes.push({
          id: 1,
          nodePeriod: 'response',
          nodeUrl: 'risk-response-list',
          nodeName: '风险应对',
        })
      }
      var arr = []
      arr = arr.concat(this.firstNodes)
      if (this.secondNodes.length > 1) {
        arr = arr.concat(this.secondNodes)
      }
      arr = arr.concat(this.threeNodes)
      this.allTaskNodes = arr
      // 跳转
      this.setChecked(arr[0].nodePeriod + '-' + arr[0].id, arr[0].nodeUrl)
    })
  },
  destroyed() {
    window.removeEventListener('hasLeaderAuthority', this.hasLeaderAuthority)
  },
  methods: {
    // 隐藏左侧节点
    foldClick() {
      this.fold = !this.fold
    },
    // 是否拥有项目组长和项目副组长的权限
    hasLeaderAuthority() {
      if (this.currentUserType.indexOf('01') > -1 || this.currentUserType.indexOf('05') > -1) {
        return true
      }
      return false
    },
    setChecked(val, url) {
      url = url + '?taskId=' + this.taskId
      this.checkedId = val
      this.$router.push('/risk-tasks/' + url)
    },
    close() {
      crossWindowWatcher.notifyChange(true)
      window.close()
    },
    menuClick(val) {
      this.menuId = val.key
      if (val.key === '1') {
        // 评价作业
        this.iframeTag = true
        setTimeout(() => {
          // 调整左侧节点选中状态
          this.checkedId = this.allTaskNodes[0].nodePeriod + '-' + this.allTaskNodes[0].id
          // 修改跳转路由
          this.$router.push(
            '/risk-tasks/' + this.allTaskNodes[0].nodeUrl + '?taskId=' + this.taskId
          )
        }, 0)
      }
      if (val.key === '2') {
        // 概况
        this.iframeTag = false
        setTimeout(() => {
          this.$router.push(
            '/risk-tasks/risk-task-overview?taskId=' + this.taskId // 新页面要打开的路由地址
          )
        }, 0)
      }
      if (val.key === '3') {
        // 评价人员
        this.iframeTag = false
        setTimeout(() => {
          this.$router.push(
            '/risk-tasks/risk-eventuser-list?taskId=' + this.taskId // 新页面要打开的路由地址
          )
        }, 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: 0;

      :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;
}
</style>
