<script setup name="Index">
import { onMounted, ref, reactive } from 'vue'
import { CountUp } from 'view-ui-plus';
import { useRouter } from 'vue-router'
import { changeRoute } from '@/utils/switchRoute'
import img from '@/assets/images/index/index.js'
import HomeCharts from '@/components/HomeCharts/index.vue'
import DashboardCharts from '@/components/HomeCharts/dashboard.vue'
import pageIcon from '@/assets/images/ruleConfig/discoverPage.png'
import projectLogo from '@/assets/images/projectLogo.png'
import { getHomePage } from '@/api/home'

const router = useRouter()
const { proxy } = getCurrentInstance()

const projectName = ref(sessionStorage.getItem('projectName'))
const projectId = ref(sessionStorage.getItem('projectId')) // 项目id
// 首页统计数据
const topList = ref([
  {
    name: '数据源',
    icon: img.icon2,
    num: 0,
    bgColor: 'linear-gradient(136.52deg, rgba(161, 230, 230, 1) 0%, rgba(211, 240, 241, 1) 47.92%, rgba(194, 238, 237, 1) 95.83%)'
  },
  {
    name: '表',
    icon: img.icon3,
    num: 0,
    bgColor: 'linear-gradient(136.52deg, rgba(189, 213, 248, 1) 0%, rgba(217, 232, 255, 1) 47.92%, rgba(189, 213, 248, 1) 100%)'
  },
  {
    name: '字段',
    icon: img.icon4,
    num: 0,
    bgColor: 'linear-gradient(136.52deg, rgba(185, 229, 253, 1) 0%, rgba(209, 240, 255, 1) 47.92%, rgba(196, 234, 251, 1) 100%)'
  },
  {
    name: '加密表数量',
    icon: img.icon5,
    num: 0,
    bgColor: 'linear-gradient(136.52deg, rgba(170, 187, 242, 1) 0%, rgba(223, 228, 247, 1) 47.92%, rgba(196, 202, 245, 1) 100%)'
  },
  {
    name: '加密字段数量',
    icon: img.icon6,
    num: 0,
    bgColor: 'linear-gradient(136.52deg, rgba(205, 220, 250, 1) 0%, rgba(222, 234, 255, 1) 47.92%, rgba(200, 214, 250, 1) 100%)'
  }
])

// 系统资源数据
const dataObj = ref({
  cpuCount: 0,
  percentCpuLoad: 0,
  totalvirtualMemory: 0,
  percentMemoryLoad: 0,
})

// 磁盘空间图表数据
const homeChartData = reactive({
  xList: [],
  yList1: [],
  yList2: [],
})
provide('homeChartData', homeChartData)

// 获取首页数据
const getHomePageFunc = () => {
  getHomePage({ projectid: projectId.value }).then(res => {
    const { data } = res
    dataObj.value = data
    topList.value[0].num = data.dataBaseNum
    topList.value[1].num = data.tableNum
    topList.value[2].num = data.fieldNum
    topList.value[3].num = data.encryptTableCount
    topList.value[4].num = data.encryptColumnCount

    if (!data.space) return
    homeChartData.xList = data.space.map(item => item.path)
    homeChartData.yList1 = data.space.map(item => item.usedpace)
    homeChartData.yList2 = data.space.map(item => item.freespace)
  })
}

// 返回主页面
const pageProjectManage = () => {
  changeRoute()
  router.push({
    path: '/project/Project'
  })
}

onMounted(() => {
  getHomePageFunc()
})
</script>

<template>
  <div class="app-container home scroller">
    <IconTitle :icon="projectLogo" :title="projectName">
      <el-button type="primary" @click="pageProjectManage" icon="Back" plain style="height: 34px;">返回项目管理</el-button>
    </IconTitle>
    <div class="topBox">
      <div class="topItem" :style="`background: ${item.bgColor};`" v-for="(item, index) in topList" :key="index">
        <img class="icon" :src="item.icon" alt="">
        <div class="name">{{ item.name }}</div>
        <div class="num">
          <CountUp :end="item.num" :duration="3" />
        </div>
      </div>
    </div>
    <div class="boxCard">
      <div class="line"></div>
      <div class="container">
        <DividerTitle title-name="系统资源" style="margin-bottom: 50px;" />
        <div class="bottom" v-if="dataObj">
          <div class="bottomItem">
            <div class="infoBox" style="padding-left: 22px;">
              <img class="icon" :src="img.img1" style="width: 72px;height: 72px;" alt="">
              <div class="type">CPU</div>
              <div class="name">
                <div>
                  <CountUp :end="dataObj.cpuCount" :duration="2" />
                </div>
                <div class="unit">核</div>
              </div>
            </div>
            <div class="typeName">
              <div class="point point1"></div>
              <div>CPU占用</div>
            </div>
            <div class="chart">
              <DashboardCharts :chartNum="dataObj.percentCpuLoad"
                :color="['rgba(121, 167, 252, 1)', 'rgba(45, 117, 250, 1)','rgba(236, 243, 254, 1)']" />
              <div class="num">
                <CountUp :end="dataObj.percentCpuLoad" :duration="3" />
                <span class="unit">%</span>
              </div>
            </div>
          </div>
          <div class="bottomItem">
            <div class="infoBox" style="padding-left: 14px;">
              <img class="icon" :src="img.img2" style="width: 78px;height: 78px;" alt="">
              <div class="type">内存</div>
              <div class="name">
                <div>
                  <CountUp :end="dataObj.totalvirtualMemory" :duration="2" />
                </div>
                <div class="unit">G</div>
              </div>
            </div>
            <div class="typeName">
              <div class="point point2"></div>
              <div>内存占用</div>
            </div>
            <div class="chart">
              <DashboardCharts :chartNum="dataObj.percentMemoryLoad"
                :color="['rgba(245, 126, 7, 1)', 'rgba(245, 126, 7, 1)','rgba(255, 246, 237, 1)']" />
              <div class="num">
                <CountUp :end="dataObj.percentMemoryLoad" :duration="3" />
                <span class="unit">%</span>
              </div>
            </div>
          </div>
          <div class="bottomItem">
            <div class="typeName" style="margin-bottom: 18px;">
              <div class="point point1"></div>
              <div>磁盘空间</div>
            </div>
            <HomeCharts />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.home {
  padding: 20px 0 0 0;

  .return-logo {
    width: 16px;
    height: 12.25px;
  }

  .topBox {
    margin-top: 21px;
    display: flex;
    justify-content: space-around;
    align-items: center;
    height: 94px;

    .topItem {
      position: relative;
      margin-right: 16px;
      width: 224px;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;
      border-radius: 8px;
      box-shadow: 0px 2px 5px 2px rgba(78, 128, 126, 0.13);
      backdrop-filter: blur(4px);

      .icon {
        position: absolute;
        top: -17px;
        left: 16px;
        width: 46px;
        height: 42px;
      }

      .name {
        padding-left: 23px;
        font-size: 16px;
        font-weight: 500;
        color: rgba(51, 51, 51, 1);
      }

      .num {
        padding-right: 30px;
        font-size: 20px;
        color: rgba(51, 51, 51, 1);
      }
    }
  }

  .boxCard {
    margin-top: 72px;
    display: flex;
    width: 100%;
    height: 556px;
    background: rgba(255, 255, 255, 1);
    display: flex;
    flex-direction: column;

    .line {
      width: 100%;
      height: 2px;
      border-radius: 5px 5px 0px 0px;
      background: linear-gradient(90deg,
          rgba(33, 103, 217, 1) 0%,
          rgba(17, 200, 250, 1) 34.03%,
          rgba(33, 103, 217, 1) 64.58%,
          rgba(48, 128, 255, 1) 100%);
    }

    .container {
      padding: 45px 36px;
      height: calc(100% - 2px);
      overflow: hidden;
      display: flex;
      flex-direction: column;

      .bottom {
        display: flex;
        align-items: center;
        flex: 1;
        overflow: hidden;

        .bottomItem {
          margin-right: 20px;
          width: 286px;
          height: 100%;
          display: flex;
          flex-direction: column;
          align-items: center;

          .infoBox {
            margin-bottom: 46px;
            width: 100%;
            height: 102px;
            border-radius: 5px;
            background: linear-gradient(90deg, rgba(228, 240, 252, 1) 0%, rgba(234, 244, 247, 1) 44.44%, rgba(238, 250, 242, 1) 100%);
            display: flex;
            align-items: center;

            .name {
              margin-left: 8px;
              display: flex;
              align-items: center;
              font-size: 18px;
              font-weight: 700;
              color: rgba(33, 103, 217, 1);

              .unit {
                font-size: 14px;
                font-weight: 700;
                color: rgba(33, 103, 217, 1);
              }
            }

            .type {
              margin-left: 13px;
              font-size: 14px;
              line-height: 22px;
              color: rgba(53, 64, 79, 1);
            }
          }

          .typeName {
            margin-bottom: 28px;
            font-size: 14px;
            font-weight: 500;
            line-height: 22px;
            color: rgba(0, 0, 0, 1);
            width: 100%;
            display: flex;
            align-items: center;

            .point {
              margin-right: 9px;
              width: 6px;
              height: 6px;
              border-radius: 50%;
            }

            .point1 {
              background: rgba(33, 103, 217, 1);
            }

            .point2 {
              background: rgba(245, 126, 7, 1);
            }
          }

          .chart {
            position: relative;
            width: 100%;
            flex: 1;
            pointer-events: none;

            .num {
              position: absolute;
              bottom: 35px;
              left: 0;
              width: 100%;
              display: flex;
              justify-content: center;
              align-items: center;
              font-size: 28px;
              font-weight: 700;
              color: rgba(51, 51, 51, 1);

              .unit {
                font-size: 16px;
                line-height: 16px;
                color: rgba(51, 51, 51, 1);
              }
            }
          }
        }

        .bottomItem:last-child {
          padding: 0 50px;
          flex: 1;
          height: 100%;
          margin-right: 0;
        }
      }
    }
  }
}
</style>
