<template>
  <transition
    appear
    name="animate__animated animate__move"
    enter-active-class="animate__slideInRight"
    leave-active-class="animate__slideOutRight"
  >
    <div class="widget-RiskAndSecurityNew">
      <div class="head">
        <div class="title">
          <div class="icon"></div>
          <span class="site-info">近期隐患与安全</span>
          <div class="dateRange">{{ beginTime }}～{{ endTime }}</div>
        </div>
      </div>
      <div class="content-info">
        <!---->
        <div class="securityContent">
          <div class="title">
            <div class="icon"></div>
            <span class="item-name">安全</span>
          </div>
          <!---->
          <div class="sc-text">
            <div class="sctItem">
              <div class="sctItem-content">
                <span class="sctItem-title"></span>
                {{ weeklySafetyReport }}
              </div>
            </div>
          </div>
        </div>
        <div class="rectificationStatistic">
          <div class="title">
            <div style="display: flex; align-items: center">
              <div class="icon"></div>
              <span class="item-name">近7天隐患创建情况</span>
            </div>
            <div class="dateRange">{{ beginTime }}～{{ endTime }}</div>
          </div>
          <div class="rsChart">
            <RiskTypeChart :chartData="chartData" />
          </div>
        </div>
        <!---->
        <div class="riskContent" ref="scrollContent">
          <div class="risk-item" v-for="(item, index) in riskInfoList" :key="index">
            <div class="risk-title">
              <div class="type-name">
                <span
                  :style="{
                    border: `1px solid ${getColor(item.risk_level.substring(0, 2)).color}`,
                    color: `${getColor(item.risk_level.substring(0, 2)).color}`
                  }"
                  >{{ item.risk_level.substring(0, 2) }}</span
                >
                <span>{{ item.type_name }}</span>
              </div>
              <div class="time-status">
                <span>{{ item.create_time }}</span>
                <span
                  :style="{
                    background: `linear-gradient(90deg, ${getStatusColor(item.risk_status).color0}, ${
                      getStatusColor(item.risk_status).color1
                    })`
                  }"
                  >{{ item.risk_status }}</span
                >
              </div>
            </div>
            <div class="risk-content">
              <span>{{ item.coordinate }}</span>
              <span>{{ item.description }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </transition>
</template>

<script lang="ts">
import moment from 'moment'
import { Vue, Component, Prop, Watch } from 'vue-property-decorator'
import RiskTypeChart from '../../../components/ComprehensiveChart/riskTypeChart.vue'
import { getRequestResult, getDangerCheckSevenDayData, getWeekCountData } from '../../../apis'
//近期隐患与安全(2023/5/5)
@Component({
  name: 'RiskAndSecurityNew',
  components: { RiskTypeChart }
})
export default class RiskAndSecurityNew extends Vue {
  beginTime = moment(new Date().setDate(new Date().getDate() - 6)).format('YYYY.MM.DD')
  endTime = moment(new Date()).format('YYYY.MM.DD')
  // beginTime = ''
  // endTime = ''
  weeklySafetyReport = ''
  chartData = {}
  rcStaList = [
    { level: '一级', lastMonth: 22, past6Months: 50 },
    { level: '二级', lastMonth: 22, past6Months: 50 },
    { level: '三级', lastMonth: 22, past6Months: 50 },
    { level: '四级', lastMonth: 22, past6Months: 50 },
    { level: '高风险作业', lastMonth: 22, past6Months: 50 }
  ]
  rcList = []
  hasScroll = false
  timerId = null
  riskInfoList = []
  get projectCode() {
    return this.$store.state.bigScreen.currentProjectCode
  }
  @Watch('projectCode', { immediate: true })
  onChangMehod() {
    this.getPageData()
  }
  async getPageData() {
    let data = { blockCode: 'ycepclist' }
    let projectCode = this.projectCode
    //获取全部数据
    const result = await getRequestResult(data)
    let res = (result as any).filter((item) => item.indexCode.indexOf(projectCode + '-') != -1)
    res = res.map((item) => {
      Object.keys(item).forEach((val) => (item[val] = item[val] || ''))
      return { ...item }
    })
    // this.weeklySafetyReport = res.find((e) => e.indexCode == projectCode + '-' + 148).indexValue
    let dataIndex = Array.from({ length: 7 }, (value, key) => key + 1), //1-7
      xData = [],
      report = [],
      rectification = []
    // dataIndex.forEach((item) => {
    //   xData.push(res.find((e) => e.indexCode == projectCode + '-' + '149' + '-' + item).indexName.split('上')[0])
    //   report.push(res.find((e) => e.indexCode == projectCode + '-' + '149' + '-' + item).indexValue)
    //   rectification.push(res.find((e) => e.indexCode == projectCode + '-' + '150' + '-' + item).indexValue)
    // })
    // this.chartData = {
    //   xData,
    //   report,
    //   rectification
    // }
    this.rcList = res.find((e) => e.indexCode == projectCode + '-' + 151).indexValue.split('/')

    const interfaceResult = await getDangerCheckSevenDayData({ code: this.projectCode })
    const { hiddenEcharts, hiddenDangerChecks } = interfaceResult.result || {}
    let { xaxis, seriesDatas } = hiddenEcharts || {}
    xaxis.sort((a, b) => {
      return a.localeCompare(b)
    })
    seriesDatas = seriesDatas.map((item) => {
      return { ...item, seriesData: item.seriesData.reverse() }
    })
    this.chartData = {
      xData: xaxis,
      levelOne: (seriesDatas.find((i) => i.name.includes('轻微')) || {}).seriesData,
      levelTwo: (seriesDatas.find((i) => i.name.includes('重要')) || {}).seriesData,
      levelThree: (seriesDatas.find((i) => i.name.includes('重大')) || {}).seriesData
    }
    this.riskInfoList = hiddenDangerChecks
    if (this.riskInfoList.length != 0) {
      this.tableScroll()
    }
    //
  }
  tableScroll() {
    const that = this
    const area = this.$refs.scrollContent as any
    // 拿到元素后，对元素进行定时增加距离顶部距离，实现滚动效果(此配置为每100毫秒移动1像素)
    area.scrollTop = 0
    function scrollUp() {
      if (area.scrollTop >= area.scrollHeight - area.offsetHeight) {
        area.scrollTop = 0
      } else {
        const ratio = parseFloat((1 / window.devicePixelRatio).toFixed(2)) + 0.01 // + 0.01 为了保证 ratio >= 1
        area.scrollTop = area.scrollTop + ratio
      }
    }
    if (!this.hasScroll) {
      this.hasScroll = true
      let time = 50
      this.setAnimationFrame(scrollUp, time)
      area.onmouseover = function () {
        cancelAnimationFrame(that.timerId)
      }
      area.onmouseout = function () {
        that.setAnimationFrame(scrollUp, time)
      }
    }
  }
  //使用requestAnimationFrame重写setInterval，进行性能优化
  setAnimationFrame(render, time) {
    //当前执行时间
    var nowTime = 0
    //记录每次动画执行结束的时间
    var lastTime = Date.now()
    //我们自己定义的动画时间差值
    var diffTime = time
    //当前requestAnimationFrame的id
    //此处使用对象，对象存储在地址空间，函数内部更新了对象的值，函数外部也可以接收到
    var that = this
    function animloop() {
      //记录当前时间
      nowTime = Date.now()
      if (nowTime - lastTime > diffTime) {
        lastTime = nowTime
        render()
      }
      //timerid为数字
      that.timerId = requestAnimationFrame(animloop)
    }
    animloop()
  }
  getColor(type) {
    const typeList = [
      { type: '重大', color: '#FF2828' },
      { type: '重要', color: '#E6C14D' },
      { type: '轻微', color: '#13E3F0' }
    ]
    return typeList.find((i) => i.type == type) || { color: '#13E3F0' }
  }
  getStatusColor(status) {
    const typeList = [
      { status: '待整改', color0: '#dc8310', color1: '#d99032' },
      { status: '已关闭', color0: '#03A25D ', color1: '#21CB7D ' },
      { status: '待闭合', color0: '#761BDD', color1: '#9C42E8' }
    ]
    return typeList.find((i) => i.status == status) || { color0: '#dc8310', color1: '#d99032' }
  }
  beforeDestroy() {
    if (this.timerId) {
      cancelAnimationFrame(this.timerId)
    }
  }
}
</script>

<style lang='scss' scoped>
.animate__slideInRight,
.animate__slideOutRight {
  animation-duration: 3s; //动画持续时间
  animation-delay: 0s; //动画延迟时间
}
.widget-RiskAndSecurityNew {
  z-index: 2;
  //position
  bottom: $size10 /* 10/192 */;
  margin-right: $size20 /* 20/192 */;
  right: 0;
  position: absolute;
  //size
  height: calc(100% - 0.557292rem /* 107/192 */);
  width: 2.083333rem /* 400/192 */;
  .head {
    height: 0.166667rem /* 32/192 */;
    width: 100%;
    background: linear-gradient(-90deg, rgba(43, 167, 255, 0.2) 0%, rgba(43, 167, 255, 0.08) 100%);
    font-family: Source Han Sans CN-HEAVY;
    .title {
      width: 100%;
      height: 100%;
      display: flex;
      font-weight: 400;
      align-items: center;
      .icon {
        height: 0.166667rem /* 32/192 */;
        width: 0.34375rem /* 66/192 */;
        background: url('~@/views/groupPage/images/模块图标/统计分析/近期隐患与安全.png') no-repeat center center;
        background-size: 100% 100%;
      }
      span {
        flex: 1;
        font-weight: bold;
        font-size: 0.083333rem /* 16/192 */;
        color: #ffffff;
        padding: 0.041667rem /* 8/192 */;
        background: linear-gradient(0deg, #9bd2fa 0%, #ffffff 100%);
        background-clip: text;
        -webkit-text-fill-color: transparent;
      }
    }
  }
  .dateRange {
    height: 0.0625rem /* 12/192 */;
    font-size: 0.072917rem /* 14/192 */;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #ffffff;
  }
  .content-info {
    width: 100%;
    height: calc(100% - 0.166667rem);
    display: inline-block;
    background: linear-gradient(0deg, rgba(14, 167, 255, 0.16) 0%, rgba(14, 167, 255, 0.02) 100%);
    font-family: Source Han Sans CN;
    .title {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .icon {
        height: 0.072917rem /* 14/192 */;
        width: 0.078125rem /* 15/192 */;
        margin-right: 0.046875rem /* 9/192 */;
        background: url('~@/views/groupPage/images/三角.png') no-repeat center center;
        background-size: 100% 100%;
      }
      .item-name,
      .item-type {
        font-family: Source Han Sans CN;
        color: #0ea7ff;
        font-size: 0.072917rem /* 14/192 */;
        font-weight: 500;
      }
      .item-type {
        color: #89c3ec !important;
      }
      .symbol {
        margin-left: 0.052083rem /* 10/192 */;
        width: 0.208333rem /* 40/192 */;
        height: 0.104167rem /* 20/192 */;
        font-size: 0.072917rem /* 14/192 */;
        font-family: Source Han Sans CN;
        font-weight: 400;
        color: #ffffff;
        background: linear-gradient(90deg, #2188d3, #9c2bec);
        border-radius: 2px;
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
    .securityContent {
      margin-top: 0.09375rem /* 18/192 */;
      width: 100%;
      height: 0.885417rem /* 170/192 */;
      background: #051e32;
      padding: 0.052083rem /* 10/192 */;
      overflow: hidden;
      .sc-text {
        margin-top: 0.072917rem /* 14/192 */;
        height: calc(100% - 0.145833rem /* 28/192 */);
        width: 100%;
        overflow: auto;
        .sctItem {
          width: 100%;
          color: #feffff;
          font-family: Source Han Sans CN;
          font-size: 0.072917rem /* 14/192 */;
          .sctItem-content {
            line-height: 0.114583rem /* 22/192 */;
            word-break: break-all;
            .sctItem-title {
              color: #17aaff;
            }
          }
        }
      }
    }
    .rectificationStatistic {
      margin-top: 0.104167rem /* 20/192 */;
      height: 1.25rem /* 240/192 */;
      width: 100%;
      padding: 0.052083rem /* 10/192 */;
      .rsChart {
        height: calc(100% - 0.072917rem /* 14/192 */);
        width: 100%;
      }
    }
    .riskContent {
      margin-top: 0.052083rem /* 10/192 */;
      width: 100%;
      height: calc(100% - 2.385417rem /* 458/192 */);
      padding: 0.052083rem /* 10/192 */;
      overflow: hidden;
      .risk-item {
        width: 100%;
        height: 0.520833rem /* 100/192 */;
        display: flex;
        align-items: center;
        flex-flow: column;
        position: relative;
        margin-bottom: 0.104167rem /* 20/192 */;
        .risk-title {
          width: 100%;
          display: flex;
          justify-content: space-between;
          align-items: center;
          .type-name {
            display: flex;
            align-items: center;
            & > span:first-child {
              display: inline-block;
              width: 0.208333rem /* 40/192 */;
              height: 0.104167rem /* 20/192 */;
              border-radius: 2px;
              font-size: 0.072917rem /* 14/192 */;
              font-family: Source Han Sans CN;
              font-weight: 400;
              text-align: center;
              line-height: 0.09375rem /* 18/192 */;
              margin-right: 0.03125rem /* 6/192 */;
            }
            span {
              font-size: 0.083333rem /* 16/192 */;
              font-family: Source Han Sans CN;
              font-weight: 500;
              color: #ffffff;
            }
          }
          .time-status {
            display: flex;
            align-items: center;
            & > span:last-child {
              display: inline-block;
              width: 0.260417rem /* 50/192 */;
              height: 0.104167rem /* 20/192 */;
              // background: linear-gradient(90deg, #dc8310, #d99032);
              border-radius: 0.010417rem /* 2/192 */;
              font-size: 0.072917rem /* 14/192 */;
              font-family: Source Han Sans CN;
              font-weight: 400;
              color: #ffffff;
              text-align: center;
              line-height: 0.09375rem /* 18/192 */;
              margin-left: 0.041667rem /* 8/192 */;
            }
            span {
              font-size: 0.072917rem /* 14/192 */;
              font-family: Source Han Sans CN;
              font-weight: 400;
              color: #7fbfe4;
            }
          }
        }
        .risk-content {
          height: calc(100% - 0.104167rem /* 20/192 */);
          width: 100%;
          overflow: auto;
          display: flex;
          flex-flow: column;
          & > span:first-child {
            margin-top: 0.083333rem /* 16/192 */;
            font-size: 0.072917rem /* 14/192 */;
            font-family: Source Han Sans CN;
            font-weight: 500;
            color: #0ea7ff;
          }
          & > span:last-child {
            margin-top: 0.041667rem /* 8/192 */;
            font-size: 0.072917rem /* 14/192 */;
            font-family: Source Han Sans CN;
            font-weight: 500;
            color: #ffffff;
          }
        }
      }
      .risk-item::after {
        content: '';
        height: 1px;
        width: 100%;
        background: rgba(255, 255, 255, 0.24);
        position: absolute;
        bottom: 0;
      }
    }
  }
}
</style>