<script setup lang="ts">
import DataCard from "@/views/dashboard/components/DataCard.vue";
import LiquidChart from "@/views/charts-components/LiquidChart.vue";
import CircleChart from "@/views/charts-components/CircleChart.vue";
import PercentBarChart from "@/views/charts-components/PercentBarChart.vue";
import { GradeList } from "@/api/school/types";
import { watch } from "vue";
import { useUserStore } from "@/store/modules/user";

import { getGradeSelect } from "@/api/school";
import { getDashboardData, getDashboardTop } from "@/api/dashboard";
import { DashboardCard, DashboardData } from "@/api/dashboard/types";

const userStore = useUserStore();
defineOptions({
  name: "DashboardIndex",
  inheritAttrs: false,
});
/**
 * 数据卡片
 */
let cards: DashboardCard = reactive({
  grade: 0, // 全部班级
  teacher: 0, // 全部教师
  student: 0, // 全部学生
  equipment: 0, // 设备套数
  game: 0, // 累计训练次数
});
async function getDataCard(schoolId: number) {
  getDashboardTop(schoolId)
    .then(({ data }) => {
      cards = <DashboardCard>{ ...data };
    })
    .catch((error) => {
      console.log(error);
    });
}
/**
 * 班级数据
 */
const gradeData = ref<GradeList[]>();
// 班级编号
let gradeId = ref(0);
async function getGradeData(schoolId: number) {
  getGradeSelect(schoolId)
    .then(({ data }) => {
      data.unshift({ id: 0, name: "全部班级" });
      gradeData.value = data;
    })
    .catch((error) => {
      console.log(error);
    });
}
/**
 * 图表数据
 */
const chartStatus = ref(false);
let chartData: DashboardData = reactive({
  frontAverage: 0, // 初期专注力估值
  afterAverage: 0, // 近期专注力估值
  front: 0, // 初期50分以上的占比
  after: 0, // 近期50分以上的占比
  frontProportion: { num: [], percentage: [] }, // 初期分期占比分析
  afterProportion: { num: [], percentage: [] }, // 近期分期占比分析
});
async function getChartData(gradeId: number) {
  getDashboardData(gradeId)
    .then(({ data }) => {
      chartData = <DashboardData>{ ...data };
      if (chartData.frontAverage > 0) {
        chartStatus.value = true;
        dataStatus.value = 1;
      } else {
        dataStatus.value = 2;
      }
    })
    .catch((error) => {
      console.log(error.toString());
      dataStatus.value = 2;
    });
}
// 数据状态：1正常，2训练次数小于3，3过期，4缺省
const dataStatus = ref(5);
// 改变班级重新加载图表数据
function changeGrade() {
  dataStatus.value = 5;
  getChartData(gradeId.value);
}
onMounted(() => {
  // 获取班级
  getGradeData(userStore.schoolId);
  // 数据卡片
  getDataCard(userStore.schoolId);
  // 图表数据
  getChartData(gradeId.value);
});
watch(
  () => userStore.schoolId,
  (newValue, oldValue) => {
    console.log("userStore.schoolId", newValue, oldValue);
    // 学校切换后重新加载数据
    gradeId.value = 0;
    dataStatus.value = 5;
    getGradeData(newValue);
    getDataCard(newValue);
    getChartData(0);
  }
);
</script>

<template>
  <div class="dashboard-container">
    <!-- 数据卡片 -->
    <DataCard
      :key="cards.toString()"
      :classes="cards.grade"
      :teachers="cards.teacher"
      :students="cards.student"
      :equipments="cards.equipment"
      :trainings="cards.game"
    />

    <!-- 班级选择 及 案例展示 -->
    <div class="class-select clear">
      <el-select
        v-model="gradeId"
        placeholder="全部班级"
        size="large"
        @change="changeGrade()"
      >
        <el-option
          v-for="item in gradeData"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <router-link to="/dashboard/example">优秀教学效果示例</router-link>
    </div>

    <!-- Echarts 图表 -->
    <el-row v-if="dataStatus == 1" :gutter="20">
      <el-col :md="24" :lg="12" :xl="8">
        <div class="charts-item">
          <p class="title">学员专注力平均值整体对比分析</p>
          <el-row justify="space-between">
            <el-col :xs="24" :sm="12">
              <div v-if="chartStatus" class="item">
                <LiquidChart
                  id="liquidChart1"
                  :key="chartData.frontAverage"
                  :data="chartData.frontAverage ? chartData.frontAverage : 0"
                  height="200px"
                  width="200px"
                  color="#3a7fc2"
                  bg-color="#accded"
                  class="chart"
                />
                <p>全体学员初期</p>
                <p>专注力评估均值</p>
              </div>
            </el-col>
            <el-col :xs="24" :sm="12">
              <div v-if="chartStatus" class="item">
                <LiquidChart
                  id="liquidChart2"
                  :key="chartData.afterAverage"
                  :data="chartData.afterAverage ? chartData.afterAverage : 0"
                  height="200px"
                  width="200px"
                  color="#5563ac"
                  bg-color="#cacce6"
                  class="chart"
                />
                <p>全体学员训练近期</p>
                <p>专注力评估均值</p>
              </div>
            </el-col>
          </el-row>
          <el-row justify="space-between">
            <el-col :xs="24" :sm="12">
              <div v-if="chartStatus" class="item">
                <CircleChart
                  id="circleChart1"
                  :key="chartData.front"
                  :data="chartData.front ? chartData.front : 0"
                  height="200px"
                  width="200px"
                  color="#3a7fc2"
                  bg-color="#e4e7f4"
                  font-color="#3a7fc2"
                  font-size="30px"
                  :round-cap="Boolean(true)"
                />
                <p>初期训练</p>
                <p>专注力50以上人数比例</p>
              </div>
            </el-col>
            <el-col :xs="24" :sm="12">
              <div v-if="chartStatus" class="item">
                <CircleChart
                  id="circleChart2"
                  :key="chartData.after.toString()"
                  :data="chartData.after ? chartData.after : 0"
                  height="200px"
                  width="200px"
                  color="#5563ac"
                  bg-color="#e4e7f4"
                  font-color="#5563ac"
                  font-size="30px"
                  :round-cap="Boolean(true)"
                />
                <p>近期训练</p>
                <p>专注力50以上人数比例</p>
              </div>
            </el-col>
          </el-row>
        </div>
      </el-col>
      <!-- 学员专注力评分分级占比分析 -->
      <el-col :md="24" :lg="12" :xl="16">
        <div class="charts-item">
          <p class="title">学员专注力评分分级占比分析</p>
          <el-row justify="space-between">
            <el-col :xs="24" :sm="24" :md="12" :lg="24" :xl="12">
              <div v-if="chartStatus" class="bar">
                <PercentBarChart
                  id="barChart1"
                  :key="chartData.frontProportion.toString()"
                  width="400px"
                  height="500px"
                  title="全体学员初期训练专注力评分占比"
                  :percent="chartData.frontProportion.percentage"
                  :data="chartData.frontProportion.num"
                  class="chart"
                />
              </div>
            </el-col>
            <el-col :xs="24" :sm="24" :md="12" :lg="24" :xl="12">
              <div v-if="chartStatus" class="bar">
                <PercentBarChart
                  id="barChart2"
                  :key="chartData.afterProportion.toString()"
                  width="400px"
                  height="500px"
                  title="全体学员训练近期专注力评分平均占比"
                  :percent="chartData.afterProportion.percentage"
                  :data="chartData.afterProportion.num"
                  class="chart"
                />
              </div>
            </el-col>
          </el-row>
        </div>
      </el-col>
    </el-row>
    <el-row v-else :gutter="20">
      <el-col :md="24" :lg="8">
        <div class="charts-item">
          <p class="title">学员专注力平均值整体对比分析</p>
          <div v-if="dataStatus == 2" class="empty">
            <p>筛选条件的学生人群训练3次以上才能进行训练数据分析，</p>
            <p>当前训练数据不足以进行数据分析！</p>
          </div>
          <div v-if="dataStatus == 3" class="empty">
            <p class="red">合作已过期，无法进行数据看板的查看！</p>
          </div>
          <div v-if="dataStatus == 4" class="empty">
            <p>您的学校还没有任何训练数据！</p>
          </div>
          <div v-if="dataStatus == 5" class="empty">
            <p>加载中...</p>
          </div>
        </div>
      </el-col>
      <el-col :md="24" :lg="16">
        <div class="charts-item">
          <p class="title">学员专注力评分分级占比分析</p>
          <div v-if="dataStatus == 2" class="empty">
            <p>筛选条件的学生人群训练3次以上才能进行训练数据分析，</p>
            <p>当前训练数据不足以进行数据分析！</p>
          </div>
          <div v-if="dataStatus == 3" class="empty">
            <p class="red">合作已过期，无法进行数据看板的查看！</p>
          </div>
          <div v-if="dataStatus == 4" class="empty">
            <p>您的学校还没有任何训练数据！</p>
          </div>
          <div v-if="dataStatus == 5" class="empty">
            <p>加载中...</p>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<style lang="scss" scoped>
.dashboard-container {
  position: relative;
  padding: 30px;
}
.class-select {
  margin: 30px auto;
  .el-select {
    width: 160px;
    margin: 0 20px 0 0;
  }
  a {
    display: inline-block;
    height: 38px;
    line-height: 38px;
    padding: 0 30px;
    background: #4284f2;
    border-radius: 12px;
    color: #ffffff;
  }
}
/* 自定义 el-select 样式 */
:deep(.el-input__wrapper) {
  background: #ffffff;
  border-radius: 12px;
}

/* el-select 各种边框线隐藏**/
:deep(.el-select) {
  --el-select-input-focus-border-color: none !important;
}
:deep(.el-input__wrapper) {
  box-shadow: none !important;
}
:deep(.el-select .el-input__wrapper.is-focus) {
  box-shadow: none !important;
}
:deep(.el-select:hover:not(.el-select--disabled) .el-input__wrapper) {
  box-shadow: none !important;
}

.charts-item {
  background: #ffffff;
  border: 1px solid #e8eaec;
  border-radius: 24px;
  text-align: center;
  position: relative;
  .title {
    margin: 0;
    height: 78px;
    line-height: 78px;
    text-align: left;
    text-indent: 2em;
    font-size: 18px;
  }
  .item {
    padding-bottom: 30px;
  }
  .chart {
    margin: 0 auto;
  }
  p {
    margin: 0;
    line-height: 24px;
    font-size: 16px;
  }
  .bar {
    margin-top: 60px;
  }
  .empty {
    box-sizing: border-box;
    height: 400px;
    padding-top: 200px;
    text-align: center;
    background-image: url("../../assets/empty.png");
    background-repeat: no-repeat;
    background-position: center 30%;
    p {
      font-size: 14px;
      color: #a1a1a1;
      &.red {
        color: #e04962;
      }
    }
  }
}
</style>
