<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 {watch} from "vue";
import {useUserStore} from "@/store/modules/user";

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

const userStore = useUserStore();
defineOptions({
  name: "DashboardIndex",
  inheritAttrs: false,
});
/**
 * 数据卡片
 */
const cardStatus = ref(false);
const cards = ref<DashboardCard>();
async function getDataCard(schoolId: number) {
  getDashboardTop(schoolId)
    .then(({ data }) => {
			cards.value = data
			cardStatus.value = true;
    })
    .catch((error) => {
      console.log(error);
    });
}
/**
 * 班级数据
 */
const gradeData = ref<GradeList[]>();
// 班级编号
let gradeId = ref(0);
async function getGradeData(schoolId: number) {
	getGradeSelect(schoolId)
		.then(({ data }) => {
			gradeData.value = data;
			gradeData.value?.unshift({ id: 0, name: "全部班级" })
		})
		.catch((error) => {
			gradeData.value = [];
			gradeData.value?.unshift({ id: 0, name: "全部班级" })
			console.log(error);
		});
}
/**
 * 图表数据
 */
const chartStatus = ref(false);
const chartMessage = ref("加载中...");
const chartData = ref<DashboardData>();
// let chartData: DashboardData = reactive({
//   frontAverage: 0, // 初期专注力估值
//   afterAverage: 0, // 近期专注力估值
//   front: 0, // 初期50分以上的占比
//   after: 0, // 近期50分以上的占比
//   frontProportion: { num: [], percentage: [] }, // 初期分期占比分析
//   afterProportion: { num: [], percentage: [] }, // 近期分期占比分析
// });
async function getChartData(schoolId:number, gradeId: number) {
	chartStatus.value = false;
  getDashboardData(schoolId, gradeId)
    .then(({ data }) => {
      chartData.value = data;
			chartStatus.value = true;
    })
    .catch((error) => {
			chartStatus.value = false;
			chartMessage.value = error.message;
      console.log(error.message);
    });
}

// 改变班级重新加载图表数据
function changeGrade() {
  getChartData(userStore.schoolId, gradeId.value);
}
onMounted(() => {
  // 获取班级
  getGradeData(userStore.schoolId);
  // 数据卡片
  getDataCard(userStore.schoolId);
  // 图表数据
  getChartData(userStore.schoolId, gradeId.value);
});
watch(
  () => userStore.schoolId,
  (newValue, oldValue) => {
    console.log("userStore.schoolId", newValue, oldValue);
    // 学校切换后重新加载数据
    gradeId.value = 0;
    getGradeData(newValue);
    getDataCard(newValue);
    getChartData(newValue,0);
  }
);
</script>

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

    <!-- 班级选择 及 案例展示 -->
    <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="chartStatus" :gutter="20">
      <el-col :md="24" :lg="8" :xl="8">
        <div class="charts-item">
          <p class="title">学员专注力平均值整体对比分析</p>
          <el-row justify="space-between">
            <el-col :xs="24" :sm="12">
              <div class="item">
                <LiquidChart
                  id="liquidChart1"
                  :key="chartData.frontAverage"
                  :data="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 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 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 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="16" :xl="16">
        <div class="charts-item">
          <p class="title">学员专注力评分分级占比分析</p>
          <el-row justify="space-between">
            <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
              <div 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="12" :xl="12">
              <div 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 class="empty">
						<img src="../../assets/empty.png" alt="数据为空">
            <p>{{ chartMessage }}</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 class="empty">
						<img src="../../assets/empty.png" alt="数据为空">
            <p>{{ chartMessage }}</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 {
    padding: 200px 0;
  }
}
</style>
