<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_form form">
        <el-table :data="tableData">
          <el-table-column label="排名" align="center" width="180">
            <template #default="{ row, $index }">
              <div class="form_index color" v-if="$index < 5">
                NO.{{ row.rank }}
              </div>
              <div class="form_index color2" v-else>NO.{{ row.rank }}</div>
              <div class="form_icon"></div>
            </template>
          </el-table-column>

          <el-table-column label="姓名">
            <template #default="{ row }">
              <div class="form_name">
                <img :src="row.headImage" />
                {{ row.name }}
              </div>
            </template>
          </el-table-column>

          <el-table-column prop="value" label="战力值" align="center">
            <template #default="{ row }">
              <div class="form_value">{{ row.totalScore }}</div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useRoute } from "vue-router";
import * as Echarts from "echarts";
// 引入用户画像相关的接口
import { reqGetRankingList } from "@/api/user/index";
// 引入解密函数
import { decryptDes } from "@/utils/des";

const $route = useRoute();

// 用户信息
const userInfo = ref();

// 扇形图实例
let myPieChart: any;
// 扇形图DOM元素
const pieChart = ref();

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

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 = ref<any>([]);

onMounted(() => {
  // 获取用户信息
  const obj = JSON.parse(decodeURIComponent($route.query.obj as string));

  userInfo.value = obj;

  // 初始化扇形图
  initBarChart();

  // 获取教师战力值排行榜数据
  getRankingList();
});

// 获取教师战力值排行榜数据
const getRankingList = async () => {
  const res = await reqGetRankingList({
    cardNo: userInfo.value.cardNo,
  });
  // console.log(res);
  if ((res as any).code == 200) {
    const result = JSON.parse(decryptDes(res.data));
    console.log(result);
    tableData.value = result.list;
    if (result.single) {
      tableData.value.push(result.single);
    }
  }
};

// 初始化扇形图
const initBarChart = () => {
  myPieChart = Echarts.init(pieChart.value);

  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);
};
</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: 20px;
      height: 336px;
      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;
        align-items: center;
        font-size: 14px;
        color: #fff;

        img {
          margin-right: 8px;
          width: 30px;
          height: 30px;
          border-radius: 50%;
          object-fit: cover;
        }
      }

      .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>
