<!-- 实验室与人员匹配 -->
<template>
  <div class="lab-person-card">
    <div class="card-header">
      <span class="card-title">实验室与人员匹配</span>
      <div class="header-right">
        <p class="export-btn export-btn--blue" @click="exportButton1()">
          <i class="el-icon-download"></i> 导出各学院数据
        </p>
        <p class="export-btn export-btn--green" @click="exportButton2()">
          <i class="el-icon-download"></i> 导出实验室人员明细
        </p>
      </div>
    </div>

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

    <!-- 筛选 -->
    <div class="filter-row">
      <el-cascader
        style="width: 180px"
        clearable
        v-model="queryParams.deptId"
        :options="optionListA"
        :props="{
                    emitPath:false,
                    checkStrictly: true,
                    value: 'deptId',
                    label: 'deptName',
                    children: 'child',
                  }"
        :show-all-levels="false"
        filterable
        placeholder="筛选学院"
        @change="examElStatisticsLabList"
      ></el-cascader>
      <el-select v-model="queryParams.levelId" clearable @change="examElStatisticsLabList" placeholder="实验室等级" style="width:200px;">
        <el-option v-for="item in optionListB" :key="item.levelId" :label="item.titleName"
                   :value="item.levelId"/>
      </el-select>
    </div>

    <!-- 表格 -->
    <div class="page-content-box">
      <el-table class="table-box" :data="tableData" border size="small">
        <el-table-column prop="deptName" label="学院" min-width="140" />
        <el-table-column prop="level1Count" label="Ⅰ级人数" width="200" align="center" />
        <el-table-column prop="level2Count" label="Ⅱ级人数" width="200" align="center" />
        <el-table-column prop="level3Count" label="Ⅲ级人数" width="200" align="center" />
        <el-table-column prop="level4Count" label="Ⅳ级人数" width="200" align="center" />
        <el-table-column prop="totalCount" label="总人数" width="200" align="center"/>
      </el-table>
    </div>
  </div>
</template>

<script>
import echarts from 'echarts'
import {
  examElStatisticsLabChart,examElStatisticsLabList,
  laboratoryLevelConfigGetLevelTitleList,systemDeptCurrentDept,
} from "@/api/safetyEducationExaminationNew/index";

export default {
  name: 'LabPersonMatch',
  data() {
    return {
      optionListA:[],
      optionListB:[],
      queryParams:{
        deptId:'',
        levelId:'',
      },
      chartData: [],
      tableData: [],
    }
  },
  mounted() {
    this.laboratoryLevelConfigGetLevelTitleList();
    this.systemDeptCurrentDept();
    this.examElStatisticsLabChart();
    this.examElStatisticsLabList();
  },
  beforeDestroy() {
    if (this._barChart) this._barChart.dispose()
  },
  methods: {
    //导出
    exportButton1(){
      let self = this;
      self.$confirm(`确认导出各学院数据?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        let obj = {
          academicYear:this.valueYear,
          personType:this.checkIndex,
          deptId:this.queryParams.deptId,
          examTypeId:this.queryParams.examTypeId,
        };
        self.download('/exam/elStatistics/lab/export', {...obj}, '实验室与人员匹配学院数据.xlsx')
      }).catch(() => {})
    },
    exportButton2(){
      let self = this;
      self.$confirm(`确认导出实验室人员明细?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        let obj = {
          academicYear:this.valueYear,
          personType:this.checkIndex,
          deptId:this.queryParams.deptId,
          examTypeId:this.queryParams.examTypeId,
        };
        self.download('/exam/elStatistics/lab/detail-export', {...obj}, '实验室与人员匹配实验室人员明细.xlsx')
      }).catch(() => {})
    },
    initBarChart() {
      const chart = echarts.init(this.$refs.barChart)
      this._barChart = chart
      const data = this.chartData
      let num = 0;
      for(let i=0;i<data.length;i++){
        if(data[i].count>num){
          num = data[i].count
        }
      }
      chart.setOption({
        grid: { top: 30, bottom: 20, left: 50, right: 30, containLabel: false },
        xAxis: {
          type: 'category',
          data: data.map(d => d.levelName),
          axisLine: { lineStyle: { color: '#e0e0e0' } },
          axisTick: { show: false },
          axisLabel: { fontSize: 12, color: '#666' },
        },
        yAxis: {
          type: 'value',
          max: num,
          interval: 100,
          splitLine: { lineStyle: { color: '#f0f0f0' } },
          axisLabel: { fontSize: 11, color: '#aaa' },
          axisLine: { show: false },
          axisTick: { show: false },
        },
        series: [
          {
            type: 'bar',
            barWidth: 60,
            data: data.map(d => ({
              value: d.count,
              itemStyle: { color: d.levelColor },
            })),
            label: {
              show: true,
              position: 'top',
              fontSize: 12,
              fontWeight: 700,
              color: '#555',
              formatter: '{c}',
            },
          },
        ],
        tooltip: {
          trigger: 'axis',
          axisPointer: { type: 'shadow' },
          formatter: (params) => `${params[0].name}: ${params[0].value}`,
        },
      })
    },
    //图表
    examElStatisticsLabChart() {
      examElStatisticsLabChart({}).then(response => {
        this.$set(this, 'chartData', response.data)
        if(response.data[0]){
          this.$nextTick(() => {
            this.initBarChart()
          })
        }
      })
    },
    //列表
    examElStatisticsLabList() {
      examElStatisticsLabList(this.queryParams).then(response => {
        this.$set(this, 'tableData', response.data)
      })
    },
    //查询风险等级
    laboratoryLevelConfigGetLevelTitleList() {
      laboratoryLevelConfigGetLevelTitleList({}).then(response => {
        this.$set(this, 'optionListB', response.data)
      })
    },
    //学院列表
    systemDeptCurrentDept(){
      systemDeptCurrentDept({}).then(response => {
        const list = response.data || [];
        this.formatTreeData(list);
        this.$set(this,'optionListA',list);
      });
    },
  }
}
</script>

<style scoped lang="scss">
.lab-person-card {
  background: #fff;
  /*border-radius: 6px;*/
  padding: 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; }
    .header-right { display: flex; gap: 20px; }
  }

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

    &--blue  { background-color: #0183fa; border: 1px solid #0183fa; }
    &--green { background-color: #00B176; border: 1px solid #00B176; }
  }

  .chart-wrap {
    border: 1px solid #eee;
    border-radius: 6px;
    padding: 20px 20px 30px;
    margin-bottom: 16px;

    .bar-chart {
      height: 260px;
    }
  }

  .filter-row {
    display: flex;
    gap: 20px;
    margin-bottom: 12px;
  }

  .page-content-box{
    height:500px;
    padding:0;
  }
  .total-link { color: #409eff; }
}
</style>
