<!--
 * @Author: SunTao 328867980@qq.com
 * @Date: 2024-10-29 15:30:35
 * @LastEditors: SunTao 328867980@qq.com
 * @LastEditTime: 2024-11-18 17:16:45
 * @FilePath: \znxjxt-ui\src\views\index-components\traffic-index.vue
 * @Description: 首页-路况评定
-->

<template>
  <div class="content">
    <div class="title">
      <span>路况评定</span>
      <div class="title-select">
        <el-radio-group v-model="itemSelect" size="mini" @change="changeRadio">
          <el-radio-button
            v-for="(item, index) in selectTypeArr"
            :label="item.value"
            :key="`left-screen-${index}`"
            >{{ item.label }}</el-radio-button
          >
        </el-radio-group>
      </div>
    </div>
    <div class="traffic-table">
      <el-table :data="trafficTableData" style="width: 100%" height="180">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="name" show-overflow-tooltip label="名称">
        </el-table-column>
        <el-table-column prop="roadPerKm" label="路面病害数(个/km)">
        </el-table-column>
        <el-table-column prop="eventPerKm" label="交安病害数(个/km)">
        </el-table-column>
        <el-table-column prop="bridgePerKm" label="桥隧病害数(个/km)">
        </el-table-column>
        <el-table-column prop="greenPerKm" label="绿化病害数(个/km)">
        </el-table-column>
      </el-table>
    </div>
    <div class="traffic-chart" ref="trafficChart"></div>
  </div>
</template>
  
<script>
import * as echarts from "echarts";
// 接口
import { getRoadCondition, selectTypeList } from "@/api/xj/index/index";
export default {
  name: "TrafficIndex",
  data() {
    return {
      // 右上角单选择框
      selectTypeArr: [],
      // 右上角选择框绑定
      itemSelect: "",
      // 路况表格数据
      trafficTableData: [],
    };
  },
  created() {
    this.getTableData();
    this.getSelect();
  },
  methods: {
    /* 获取数据栏右上角选项数据 */
    getSelect() {
      selectTypeList().then(({ code, data }) => {
        if (code === 200) {
          this.selectTypeArr = data;
        }
      });
    },

    /* 右上角单选改变事件 */
    changeRadio(){
      this.getTableData()
    },

    /* 获取表格数据 */
    getTableData() {
      getRoadCondition({ type: this.itemSelect }).then(({ code, data }) => {
        if (code === 200) {
          console.log(data, "ggg");
          this.trafficTableData = data;
          this.$nextTick(() => {
            this.drawTrafficChart();
          });
        }
      });
    },
    /* 绘制柱状图 */
    drawTrafficChart() {
      if (this.$refs.trafficChart) {
        const chart = echarts.init(this.$refs.trafficChart);
        chart.setOption({
          color: ["#388BD8", "#04DAD7"],
          dataZoom: [
            {
              type: "inside",
              start: 0,
              end: 100,
            },
          ],
          title: [
            // 中心比例
            {
              text: "病害趋势",
              x: "1%",
              y: "1%",
              textStyle: {
                fontSize: 14,
              },
            },
          ],
          legend: {
            show: true,
            right: "3%",
          },
          tooltip: {
            trigger: "axis",
            padding: [5, 10, 5, 10],
          },
          grid: {
            top: "20%",
            left: "7%",
            right: "4%",
            bottom: "12%",
          },
          xAxis: [
            {
              data: [
                "1月",
                "2月",
                "3月",
                "4月",
                "5月",
                "6月",
                "7月",
                "8月",
                "9月",
                "10月",
              ],
              type: "category",
              axisTick: {
                show: false, //隐藏X轴刻度
              },
              axisLabel: { color: "#A2B0B8" },
              axisLine: {
                lineStyle: {
                  color: "#3C6579",
                  width: 1,
                },
              },
              splitLine: {
                show: false,
              },
              boundaryGap: false,
            },
          ],
          yAxis: [
            {
              nameTextStyle: {
                color: "#fff",
                fontSize: 12,
              },
              name: "",
              type: "value",
              axisLabel: {
                show: true,
                color: "#9eaaba",
              },
              axisLine: { show: false },
              splitLine: { show: true },
            },
          ],
          series: [
            {
              name: "病害修补数量",
              type: "line",
              showSymbol: false,
              smooth: true,
              areaStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: "#388BD8",
                  },
                  {
                    offset: 1,
                    color: "#ffffff",
                  },
                ]),
              },
              data: [400, 600, 1400, 1700, 400, 600, 400, 600, 1400, 1700],
            },
            {
              name: "病害数量",
              type: "line",
              showSymbol: false,
              smooth: true,
              areaStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: "#04DAD7",
                  },
                  {
                    offset: 1,
                    color: "#ffffff",
                  },
                ]),
              },
              data: [40, 500, 1500, 700, 40, 500, 40, 500, 1500, 700],
            },
          ],
        });
        window.addEventListener("resize", () => {
          chart.resize();
        });
      }
    },
  },
};
</script>
  
<style lang="scss" scoped>
.content {
  width: 100%;
  height: 100%;
  padding: 1rem;

  .title {
    width: 100%;
    height: 10%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-left: 3rem;
    background: url("../../assets/index/div-title.png") no-repeat;
    background-size: 5%;
    background-position: 0 30%;

    > span {
      font-family: "DouYu";
    }
  }

  .traffic-table {
    width: 100%;
    height: 45%;

    ::v-deep .el-table tr {
      background-color: #f6f7fa;
    }
    ::v-deep .el-table .el-table__header-wrapper th,
    .el-table .el-table__fixed-header-wrapper th {
      background-color: #f6f7fa;
    }
  }

  .traffic-chart {
    width: 100%;
    height: 45%;
  }
}
</style>
