<template>
  <div class="container">
    <!-- 教学成果区域 -->
    <div class="container_box">
      <div class="box_title">
        <img src="@/assets/images/box-icon.png" />
        教学成果
      </div>

      <!-- 筛选框区域 -->
      <div class="box_select">
        <span>部门</span>
        <el-select
          v-model="valueClass"
          placeholder="请选择部门"
          style="width: 206px"
        >
          <el-option
            v-for="item in optionsClass"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>

      <div class="box_select">
        <span>考试名称</span>
        <el-select
          v-model="valueTime"
          placeholder="请选择学期"
          style="width: 206px"
        >
          <el-option
            v-for="item in optionsTime"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>

      <!-- 图表展示区域 -->
      <div class="box_chart" ref="pieChart"></div>
    </div>

    <!-- 排行榜区域 -->
    <div class="container_box">
      <div class="box_title">
        <img src="@/assets/images/box-icon.png" />
        排行榜
      </div>

      <div class="box_change">
        <div
          class="change_box"
          :class="currentTimeRang === 0 ? 'active' : ''"
          @click="changeTimeRang(0)"
        >
          本学期
        </div>
        <div
          class="change_box"
          :class="currentTimeRang === 1 ? 'active' : ''"
          @click="changeTimeRang(1)"
        >
          本周
        </div>
        <div
          class="change_box"
          :class="currentTimeRang === 2 ? 'active' : ''"
          @click="changeTimeRang(2)"
        >
          本月
        </div>
      </div>

      <div class="box_form form">
        <el-table :data="tableData">
          <el-table-column label="排名" align="center">
            <template #default="{ $index }">
              <div class="form_index color" v-if="$index < 3">
                NO.{{ $index + 1 }}
              </div>
              <div class="form_index color2" v-else>NO.{{ $index + 1 }}</div>
              <div class="form_icon"></div>
            </template>
          </el-table-column>
          <el-table-column label="姓名" align="center">
            <template #default="{ row }">
              <div class="form_name">
                <img
                  src="https://img1.baidu.com/it/u=1398895526,1958525606&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800"
                />
                {{ row.name }}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="value" label="战力值" align="center">
            <template #default="{ row }">
              <div class="form_value">{{ row.value }}</div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import * as Echarts from "echarts";

let myPieChart: any;
const pieChart = ref();

const valueTime = ref("");
const valueClass = ref("");

// 切换排行榜时间 0本学期 1本周 2本月
const currentTimeRang = ref(0);

const optionsTime = [
  {
    value: "0",
    label: "2022-2023",
  },
  {
    value: "1",
    label: "2021-2022",
  },
  {
    value: "2",
    label: "2020-2021",
  },
];

const optionsClass = [
  {
    value: "0",
    label: "八年级一班",
  },
  {
    value: "1",
    label: "八年级二班",
  },
  {
    value: "2",
    label: "八年级三班",
  },
];

const chartData = ref([
  { value: 30, name: "60分以下" },
  { value: 20, name: "60-70 分" },
  { value: 13, name: "71-80 分" },
  { value: 12, name: "81-90 分" },
  { value: 12, name: "91-100分" },
]);

const tableData = [
  {
    name: "张三",
    value: 35462,
  },
  {
    name: "李四",
    value: 35462,
  },
  {
    name: "王五",
    value: 35462,
  },
  {
    name: "老刘",
    value: 35462,
  },
];

onMounted(() => {
  myPieChart = Echarts.init(pieChart.value);
  initBarChart();
});

const initBarChart = () => {
  const options = {
    title: {
      text: "总次数",
      left: "22%",
      top: "38%",
      textStyle: {
        fontSize: 14,
        color: "#D5E3EA",
      },
      subtext: "{a|200}",
      subtextStyle: {
        rich: {
          a: {
            padding: 2,
            fontSize: 18,
            color: "#D5E3EA",
            fontWeight: "bold",
          },
        },
      },
      itemGap: 15,
    },
    tooltip: {
      trigger: "item",
    },
    legend: {
      orient: "vertical",
      right: "4%",
      top: "center",
      itemWidth: 8,
      itemHeight: 8,
      itemGap: 25,
      textStyle: {
        padding: 8,
        color: "#fff",
        fontSize: 14,
        rich: {
          a: {
            fontSize: 16,
            fontWeight: "bold",
            align: "right",
            padding: [0, -150, 0, 0],
          },
        },
      },
      formatter: (params: any) => {
        const valueObj: any = chartData.value.find(
          (item) => item.name === params
        );
        return params + "{a|" + valueObj.value + "}";
      },
    },
    color: ["#50A4E1", "#FF85BE", "#FFC77D", "#93DE62", "#4AE8E8"],
    series: [
      {
        name: "教学成绩",
        type: "pie",
        center: ["28%", "50%"],
        radius: ["55%", "75%"],
        avoidLabelOverlap: false,
        label: {
          show: false,
        },
        itemStyle: {
          borderColor: "rgba(2, 27, 41, 0.1)",
          borderWidth: 5,
        },
        data: chartData.value,
      },
    ],
  };

  myPieChart.setOption(options);
};

// 排行榜切换时间范围按钮回调
const changeTimeRang = (value: number) => {
  if (currentTimeRang.value !== value) {
    if (value === 0) {
      currentTimeRang.value = 0;
    } else if (value === 1) {
      currentTimeRang.value = 1;
    } else if (value === 2) {
      currentTimeRang.value = 2;
    }
  }
};
</script>

<style lang="scss" scoped>
.container {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  width: 500px;
  height: 834px;
  color: #fff;

  .container_box {
    padding: 17px 0 0 22px;
    width: 500px;
    height: 412px;
    background-image: url(@/assets/images/box-bg5.png);
    background-size: 100% 100%;

    .box_title {
      display: flex;
      align-items: center;
      font-size: 16px;
      font-weight: bold;

      img {
        margin-right: 12px;
        width: 24px;
        height: 24px;
      }
    }

    .box_select {
      display: flex;
      justify-content: flex-end;
      align-items: center;
      margin-top: 15px;
      margin-right: 32px;
      font-size: 14px;

      span {
        margin-right: 13px;
      }
    }

    .box_chart {
      height: 270px;
    }

    .box_change {
      display: flex;
      justify-content: space-between;
      margin: 20px 0 33px 220px;
      padding: 2px;
      width: 200px;
      height: 32px;
      font-size: 14px;
      border-radius: 2px;
      border: 1px solid #9c9c9c;

      .change_box {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 61px;
        border-radius: 2px;
        background-color: rgba(114, 151, 179, 0.3);
        cursor: pointer;
      }

      .active {
        border: 1px solid #70b7fa;
        background-color: rgba(114, 151, 179, 1);
      }
    }

    .box_form {
      padding-right: 44px;
      margin-top: 38px;
      height: 245px;
      overflow: hidden;

      .form_index {
        font-size: 14px;
        font-weight: bold;
      }

      .form_icon {
        position: absolute;
        top: 0;
        left: 0;
        width: 0;
        height: 0;
        border-color: transparent #3be8ff;
        border-width: 0 0 8px 8px;
        border-style: solid;
      }

      .color {
        color: #ff7734;
      }

      .color2 {
        color: #57fff4;
      }

      .form_name {
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 14px;
        color: #fff;
        img {
          margin-right: 8px;
          width: 30px;
          height: 30px;
          border-radius: 50%;
        }
      }

      .form_value {
        color: #57fff4;
        font-size: 18px;
        font-weight: bold;
      }
    }
  }
}

// 修改select背景颜色
::v-deep(.el-input__wrapper) {
  background: transparent;
  background-color: rgba(48, 75, 95, 0.5);
}

// 修改select筛选框文字颜色
::v-deep(.el-input__inner) {
  color: #fff;
}

/*最外层透明*/
.form ::v-deep(.el-table),
.form ::v-deep(.el-table__expanded-cell) {
  background-color: transparent;
  color: white;
  border: none;
}
/* 表格内背景颜色 */
.form ::v-deep(.el-table th),
.form ::v-deep(.el-table tr),
.form ::v-deep(.el-table td),
::v-deep(.el-table th.el-table__cell.is-leaf) {
  background-color: transparent !important;
  color: #5cb3ff;
  font-size: 16px;
  border-color: #021b29;
  border-width: 3px;
  height: 40px;
}

// 表格底部白线清除
::v-deep(.el-table__inner-wrapper::before) {
  height: 0;
}

// 修改表头背景颜色
::v-deep(.el-table__header) {
  background-color: rgba(58, 126, 199, 0.5);
}
// 清除表格默认padding
::v-deep(.el-table .cell) {
  padding: 0;
}
</style>
