<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 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 :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 + '-' + node.id },
                ]"
                @click="setChecked(node.nodePeriod + '-' + node.id, node.nodeUrl, 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 + '-' + node.id },
                ]"
                @click="setChecked(node.nodePeriod + '-' + node.id, node.nodeUrl, 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 + '-' + node.id },
                ]"
                @click="setChecked(node.nodePeriod + '-' + node.id, node.nodeUrl, 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 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 { Message } from 'ant-design-vue'
import { getUserInfo } from '@/common/store-mixin'
import axios from '@/common/services/axios-instance'
import axiosll from 'axios'
import { Modal } from '@/common/one-ui'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import icEvaluationService from '../ic-evaluation-service'
import components from './_import-components/iam-ic-work-import'

export default {
  name: 'IamIcWork',
  metaInfo: {
    title: '内控测试',
  },
  components: {
    ...components,
  },
  mixins: [],
  data() {
    return {
      menuId: '',
      defaultselectedkey: ['1'],
      fold: false,
      spinning: true, // 左侧节点加载状态
      iframeTag: true,
      planId: '',
      projectStatus: '',
      checkedId: '', // 默认选中节点id
      preparaNodes: [
        { id: 1, nodePeriod: 'control', nodeUrl: 'iam-ic-control-list', nodeName: '控制测试底稿' },
        { id: 2, nodePeriod: 'cross', nodeUrl: 'iam-ic-cross-list', nodeName: '穿行测试底稿' },
      ], // 测试底稿
      effectNodes: [
        {
          id: 1,
          nodePeriod: 'listcon',
          nodeUrl: 'iam-ic-listcon-list',
          nodeName: '缺陷汇总清单',
        },
        { id: 2, nodePeriod: 'confirm', nodeUrl: 'iam-ic-confirm', nodeName: '缺陷确认' },
      ], // 缺陷汇总清单
      reportNodes: [
        {
          id: 1,
          nodePeriod: 'report',
          nodeUrl: 'iam-ic-evaluation-report-list',
          nodeName: '内控评价报告',
        },
      ], // 报告
      projectStage: 1, // 项目阶段1准备2实施3报告
      projectName: '', // 项目名称,
      currentUserType: [], // 当前登录人所在项目角色
      loadShow: true,
      showChangeStatus: false, // 切换阶段按钮显示
      type: '',
      moduleflag: false,
      todoId: '',
    }
  },
  created() {
    const planId = this.$route.query.planId
    if (this.$route.query.module && this.$route.query.module === '1') {
      this.moduleflag = true
      this.todoId = this.$route.query.todoId
    }
    if (
      this.$route.path.indexOf('ic-planuser-list') > -1 ||
      this.$route.path.indexOf('iam-ic-evaluation-overview') > -1
    ) {
      this.iframeTag = false
      if (this.$route.path.indexOf('iam-ic-evaluation-overview') > -1) {
        this.defaultselectedkey = ['2']
        this.menuId = '2'
      } else if (this.$route.path.indexOf('ic-planuser-list') > -1) {
        this.defaultselectedkey = ['3']
        this.menuId = '3'
      }
    }
  },
  mounted() {
    var arr = []
    arr = arr.concat(this.preparaNodes)
    arr = arr.concat(this.effectNodes)
    arr = arr.concat(this.reportNodes)
    arr.forEach((i) => {
      if (this.$route.path.indexOf(i.nodeUrl) > -1) {
        this.checkedId = i.nodePeriod + '-' + i.id
      }
    })

    const planId = this.$route.query.planId
    if (planId) {
      this.planId = planId
    }
    axios({
      url: `api/framework/v1/page/wp/ic/evaluation/plan/icEvaluationPlan?id=` + planId,
    }).then((res) => {
      this.spinning = !this.spinning
      const title = res.data.pageFormData.pageFieldInfos.findIndex(
        (i) => i.name === 'evaluationName'
      )
      const type = res.data.pageFormData.pageFieldInfos.findIndex(
        (i) => i.name === 'evaluationType'
      )
      this.projectName = res.data.pageFormData.pageFieldInfos[title].value
      this.type = res.data.pageFormData.pageFieldInfos[type].value
      this.preparaNodes = []
      if (this.type.indexOf('controlTest') > -1) {
        this.preparaNodes.push({
          id: 1,
          nodePeriod: 'control',
          nodeUrl: 'iam-ic-control-list',
          nodeName: '控制测试底稿',
        })
      }
      if (this.type.indexOf('crossTest') > -1) {
        this.preparaNodes.push({
          id: 2,
          nodePeriod: 'cross',
          nodeUrl: 'iam-ic-cross-list',
          nodeName: '穿行测试底稿',
        })
      }
    })
  },
  destroyed() {
    window.removeEventListener('hasLeaderAuthority', this.hasLeaderAuthority)
  },
  methods: {
    // 关闭作业，删除待办
    fndeal() {
      const key = Symbol.for()
      icEvaluationService.fndeleteTodo(this.todoId).then((res) => {
        if (res.data) {
          Message.success('提交成功').then((res) => {
            crossWindowWatcher.notifyChange(this.$route.fullPath, true)
            window.close()
          })
        } else {
          Message.error({ content: '提交失败,请联系管理员', key })
        }
      })
    },
    // 隐藏左侧节点
    foldClick() {
      this.fold = !this.fold
    },
    // 是否拥有项目组长和项目副组长的权限
    hasLeaderAuthority() {
      if (this.currentUserType.indexOf('01') > -1 || this.currentUserType.indexOf('05') > -1) {
        return true
      }
      return false
    },
    // 切换阶段功能
    changeStatus(status) {
      const _this = this
      const planId = this.planId
      let msgText = ''
      let projectStage = ''
      if (status === 'effect') {
        msgText = '是否切换至实施阶段'
        projectStage = 'profile'
      } else {
        msgText = '是否切换至报告阶段'
        projectStage = 'file-text'
      }
      Modal.confirm({
        content: msgText,
        onOk() {
          axios({
            url: `api/xcoa-mobile/v1/iamauditproject/changePorjectPhase?planId=${planId}&projectStage=${projectStage}`,
            method: 'post',
          }).then((res) => {
            Message.success('阶段切换成功', 1)
            if (projectStage === 'profile') {
              _this.projectStage = 2
            } else if (projectStage === 'file-text') {
              _this.projectStage = 3
            }
          })
        },
        cancelText: '取消',
        okText: '确认',
        onCancel() {},
      })
    },
    setChecked(val, url, stage) {
      // 如果点击的阶段大于当前阶段，则不能点击
      // if (stage > this.projectStage) {
      //   return
      // }
      url = url + '?planId=' + this.planId
      this.checkedId = val
      // this.$refs.rightfrm.src = url
      this.$router.push('/ic-work/' + url)
    },
    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.preparaNodes[0].id
          // 修改跳转路由
          this.$router.push('/ic-work/' + this.preparaNodes[0].nodeUrl + '?planId=' + this.planId)
        }, 0)
      }
      if (val.key === '2') {
        // 概况
        this.iframeTag = false
        setTimeout(() => {
          this.$router.push(
            '/ic-work/iam-ic-evaluation-overview?planId=' + this.planId // 新页面要打开的路由地址
          )
        }, 0)
      }
      if (val.key === '3') {
        // 评价人员
        this.iframeTag = false
        setTimeout(() => {
          this.$router.push(
            '/ic-work/ic-planuser-list?planId=' + this.planId // 新页面要打开的路由地址
          )
        }, 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>
