<!-- 学时统计 -->
<template>
  <div class="study-hour-card">
    <div class="card-header">
      <span class="card-title">学时统计</span>
      <p class="export-btn" @click="exportButton()">
        <i class="el-icon-download"></i> 导出数据
      </p>
    </div>

    <!-- 分组柱状图 -->
    <p class="chart-wrap" v-if="!chartData[0]" style="color:#999;font-size:14px;line-height:260px;text-align: center;">暂无数据</p>
    <div class="chart-wrap" v-if="chartData[0]" id="chart-wrap-bar-chart" ref="barChart"></div>

    <!-- 筛选 -->
    <div class="filter-row">
      <el-select v-model="queryParams.personType" placeholder="用户类型" size="small" clearable style="width:220px">
        <el-option label="研究生" value="2" />
        <el-option label="本科生" value="3" />
        <el-option label="教职工" value="1" />
      </el-select>
      <el-cascader
        style="width: 180px"
        v-model="queryParams.deptId"
        :options="optionListA"
        :props="{
                  emitPath:false,
                  checkStrictly: true,
                  value: 'deptId',
                  label: 'deptName',
                  children: 'child',
                }"
        :show-all-levels="false"
        filterable
        placeholder="学院"
      ></el-cascader>
      <el-select v-model="queryParams.gradeNumber" placeholder="学年" style="width: 140px">
        <el-option
          v-for="dict in optionListB"
          :key="dict.gradeNumber"
          :label="dict.gradeNumber"
          :value="dict.gradeNumber"
        />
      </el-select>
      <el-input v-model="queryParams.searchValue" placeholder="学号/姓名" size="small" prefix-icon="el-icon-search" style="width:210px" clearable />
      <p class="page-inquire-common-style-button" @click="handleQuery">查询</p>
      <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
    </div>

    <!-- 表格 -->
    <div class="page-content-box">
      <el-table class="table-box" :data="tableData" border>
        <el-table-column prop="userName" label="姓名" width="200" />
        <el-table-column prop="account" label="学工号" width="140" />
        <el-table-column prop="year" label="学年" width="140" />
        <el-table-column prop="deptName" label="所属学院" show-overflow-tooltip />
        <el-table-column prop="year1CreditHours" label="第一年" width="140" align="center" />
        <el-table-column prop="year2CreditHours" label="第二年" width="140" align="center" />
        <el-table-column prop="year3CreditHours" label="第三年" width="140" align="center" />
        <el-table-column prop="year4CreditHours" label="第四年" width="140" align="center" />
        <el-table-column prop="totalCreditHours" label="总时长" width="140" align="center" />
      </el-table>
      <pagination :page-sizes="[20, 30, 40, 50]"
                  v-show="total>0"
                  :total="total"
                  :page.sync="queryParams.page"
                  :limit.sync="queryParams.pageSize"
                  @pagination="examElStatisticsCreditHourList"
      />
    </div>
  </div>
</template>

<script>
import echarts from 'echarts'
import {
  examElStatisticsCreditHourChart,examElStatisticsCreditHourList,
  systemUserGetAcademicYear,systemDeptCurrentDept,
} from "@/api/safetyEducationExaminationNew/index";
export default {
  name: 'StudyHourStats',
  data() {
    return {
      filterData: {
        personType: '',
        college: '',
        grade: '',
        keyword: '',
        page: 1,
        colleges: ['化学与化工学院','生命科学学院','物理学院','电气工程学院','材料科学学院','机械工程学院','自动化学院','资源环境学院'],
        grades: ['2017级','2018级','2019级','2020级','2021级','2022级'],
      },
      chartData: [
        // { label: '化学与化工', graduate: 320, undergrad: 180, staff: 95 },
        // { label: '生命科学',   graduate: 280, undergrad: 160, staff: 72 },
        // { label: '物理',       graduate: 240, undergrad: 145, staff: 68 },
        // { label: '电气工程',   graduate: 300, undergrad: 195, staff: 85 },
        // { label: '材料科学',   graduate: 220, undergrad: 135, staff: 60 },
        // { label: '机械工程',   graduate: 195, undergrad: 170, staff: 55 },
        // { label: '自动化',     graduate: 180, undergrad: 120, staff: 48 },
        // { label: '资源环境',   graduate: 165, undergrad: 110, staff: 52 },
      ],
      tableData: [],
      optionListA:[],
      optionListB:[],
      queryParams:{
        page:1,
        pageSize:20,
        personType:'',
        deptId:'',
        gradeNumber:'',
        searchValue:'',
      },
      total:0,
    }
  },
  mounted() {
    this.systemUserGetAcademicYear();
    this.systemDeptCurrentDept();
    this.examElStatisticsCreditHourChart();
    this.examElStatisticsCreditHourList();
  },
  beforeDestroy() {
    if (this._barChart) this._barChart.dispose()
  },
  methods: {
    //导出
    exportButton(){
      let self = this;
      self.$confirm(`确认导出数据?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        let obj = {
          academicYear:this.valueYear,
          deptId:this.queryParams.deptId,
          gradeNumber:this.queryParams.gradeNumber,
          searchValue:this.queryParams.searchValue,
        };
        self.download('/exam/elStatistics/creditHour/export', {...obj}, '学时统计.xlsx')
      }).catch(() => {})
    },
    initBarChart() {
      const chart = echarts.init(this.$refs.barChart)
      this._barChart = chart
      const data = this.chartData
      chart.setOption({
        legend: {
          data: ['研究生', '本科生', '教职工'],
          top: 10,
          right: 'center',
          itemWidth: 14,
          itemHeight: 14,
          textStyle: { fontSize: 12, color: '#555' },
        },
        grid: { top: 50, bottom: 36, left: 56, right: 20, containLabel: false },
        xAxis: {
          type: 'category',
          data: data.map(d => d.deptName),
          axisLine: { lineStyle: { color: '#ddd' } },
          axisTick: { show: false },
          axisLabel: { fontSize: 12, color: '#666', rotate: 30 },
        },
        yAxis: {
          type: 'value',
          name: '学时',
          nameTextStyle: { fontSize: 11, color: '#aaa', padding: [0, 30, 0, 0] },
          max: 350,
          interval: 50,
          splitLine: { lineStyle: { color: '#f0f0f0' } },
          axisLabel: { fontSize: 11, color: '#aaa' },
          axisLine: { show: false },
          axisTick: { show: false },
        },
        series: [
          {
            name: '研究生',
            type: 'bar',
            barGap: '10%',
            barCategoryGap: '40%',
            data: data.map(d => d.graduateCreditHours),
            itemStyle: { color: '#5b9cf6' },
            label: {
              show: true,
              position: 'top',
              fontSize: 11,
              color: '#555',
              formatter: '{c}',
            },
          },
          {
            name: '本科生',
            type: 'bar',
            data: data.map(d => d.undergraduateCreditHours),
            itemStyle: { color: '#4fbe7e' },
            label: {
              show: true,
              position: 'top',
              fontSize: 11,
              color: '#555',
              formatter: '{c}',
            },
          },
          {
            name: '教职工',
            type: 'bar',
            data: data.map(d => d.staffCreditHours),
            itemStyle: { color: '#f59e42' },
            label: {
              show: true,
              position: 'top',
              fontSize: 11,
              color: '#555',
              formatter: '{c}',
            },
          },
        ],
        tooltip: {
          trigger: 'axis',
          axisPointer: { type: 'shadow' },
        },
      })
    },
    //学时统计图表
    examElStatisticsCreditHourChart(){
      examElStatisticsCreditHourChart({}).then(response => {
        this.$set(this,'chartData',response.data.deptStats);
        if(response.data.deptStats[0]){
          this.$nextTick(() => {
            this.initBarChart()
          })
        }
      });
    },
    //学时统计列表
    examElStatisticsCreditHourList(){
      let obj = JSON.parse(JSON.stringify(this.queryParams))
      examElStatisticsCreditHourList(obj).then(response => {
        this.$set(this,'tableData',response.data.records);
        this.$set(this,'total',response.data.total);
      });
    },
    //学年下拉列表
    systemUserGetAcademicYear(){
      systemUserGetAcademicYear({}).then(response => {
        this.$set(this,'optionListB',response.data);
      });
    },
    //学院列表
    systemDeptCurrentDept(){
      systemDeptCurrentDept({}).then(response => {
        const list = response.data || [];
        this.formatTreeData(list);
        this.$set(this,'optionListA',list);
      });
    },
    //查询按钮
    handleQuery(){
      this.$set(this.queryParams,'page',1);
      this.examElStatisticsCreditHourList();
    },
    //重置按钮
    resetQuery(){
      this.$set(this,'dateRange',[])
      this.$set(this,'queryParams',{
        page:1,
        pageSize:20,
        personType:'',
        deptId:'',
        gradeNumber:'',
        searchValue:'',
      });
      this.examElStatisticsCreditHourList();
    },
  }
}
</script>

<style scoped lang="scss">
.study-hour-card {
  background: #fff;
  /*border-radius: 6px;*/
  padding: 16px 20px;
  margin-bottom: 12px;
  /*box-shadow: 0 1px 4px rgba(0,0,0,0.06);*/
  background: rgba(255, 255, 255, 0.95);
  border: 1px solid #e8edf5;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(37, 50, 74, 0.06);

  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    .card-title { font-size: 16px; font-weight: 700; color: #222; }
  }

  .export-btn {
    display: inline-block;
    padding: 0 16px;
    height: 40px;
    line-height: 40px;
    border-radius: 4px;
    font-size: 14px;
    cursor: pointer;
    background-color: #0183fa;
    color: #fff;
    border: 1px solid #0183fa;
  }

  .chart-wrap {
    border: 1px solid #eee;
    border-radius: 6px;
    margin-bottom: 16px;
    height: 300px;
  }

  .filter-row {
    display: flex;
    gap: 20px;
    margin-bottom: 12px;
    flex-wrap: wrap;
  }
  .page-content-box{
    height:500px;
    padding:0;
  }

  .link-blue { color: #409eff; }

  .pagination-wrap {
    display: flex;
    justify-content: center;
    margin-top: 14px;
  }
}
</style>
