<template>
  <div style="background: #ececec; padding: 15px">
    <a-card :bordered="false">
      <div class="query">
        <div>
          <div style="display: flex">
            <span style="font-size: 15px; margin-right: 10px; display: flex; justify-content: center; align-items: center; font-weight: bold"
              >年级:
            </span>
            <a-checkbox-group :options="entrydateOptions" v-model:value="collegeentrydate" @change="onCollegeEntrydateChange" />
          </div>
          <div style="display: flex; margin-top: 10px">
            <span style="font-size: 15px; margin-right: 10px; display: flex; justify-content: center; align-items: center; font-weight: bold"
              >学院:
            </span>
            <a-checkbox-group :options="collegeOptions" v-model:value="collegetab2" @change="onCollegeChange" />
          </div>

          <div style="margin-top: 10px; margin-right: 20px; display: flex; justify-content: flex-end; align-items: center">
            <a-button style="margin-left: 10px" type="primary" @click="clearCollege">清除</a-button>
            <a-button style="margin-left: 10px" type="primary" @click="collegeQuery">查询</a-button>
          </div>
        </div>
      </div>
    </a-card>
    <a-card title="学院四级通过率对比" :loading="tab2loading" :bordered="false">
      <a-row :gutter="12">
        <a-col :xl="14">
          <div class="container">
            <div id="map2" style="width: 100%; min-height: 400px"></div>
          </div>
        </a-col>
        <a-col :xl="10">
          <div class="container">
            <div id="map3" style="width: 100%; min-height: 400px"></div>
          </div>
        </a-col>
      </a-row>
    </a-card>
  </div>
</template>

<script>
  import { defHttp } from '/@/utils/http/axios';
  import * as echarts from 'echarts';
  import { message } from 'ant-design-vue';

  export default {
    data() {
      return {
        activeKey: '1',
        allCollege: [],
        showBox: false,
        showGroup: false,
        oneentrydate: null,
        collegeentrydate: [],
        majorentrydate: [],
        lastMajorEntrydate: [],
        checkedOptions: [],
        collegetab2: [],
        collegeMajorOptions: [],
        collegeMajor: [],
        Url: {
          getBatch: '/cet/getBatch',
          getEntrydate: '/cet/getEntrydate',
          getCollege: '/cet/getCollege',
          getCollegeRate: '/cet/getRateByCollege',
          getAllRate: '/cet/getAllRate',
          getCollegeMajor: '/cet/getCollegeMajor',
          getRateByMajor: '/cet/getRateByMajor',
        },
        tab1loading: false,
        tab2loading: false,
        tab3loading: false,

        collegeOptions: [],
        batchOptions: [],
        levelOptions: [
          { value: 'cet4', label: '英语四级' },
          { value: 'cet6', label: '英语六级' },
        ],
        visible: false,
        level: null,
        college: [],
        batch: null,
        entrydateOptions: [],
        entrydate: [],
        majorCheckOn: [],
        //对不同的学院选择的专业进行记忆化
        lastCollegeMajor: [],
        majorLength: 0,
        majorOptions: [],
        //对学院对比学院进行记忆化上次选择结果
        lastCollege: [],
        //对学院对比学院进行记忆化上次选择结果
        lastCollegeEntrydate: [],
      };
    },
    mounted() {
      this.getCollegeOptions();
      // this.getBatch();
      this.getEntrydate();
      this.allQuery();
      this.getCollegeMajorData();
    },
    methods: {
      onMajorChange(value) {
        console.log(value, 'value');
        let length = value.length;
        //遍历value,与this.collegeMajorOptions比较，如果value中的数据在this.collegeMajorOptions中则获得他的children长度
        for (let i = 0; i < value.length; i++) {
          for (let j = 0; j < this.collegeMajorOptions.length; j++) {
            if (value[i] == this.collegeMajorOptions[j].value) {
              length += this.collegeMajorOptions[j].children.length;
              length -= 1;
            }
            if (length > 5) {
              message.error('最多选择五个专业');
              //删除value最后一个数据
              value.pop();
              return;
            }
          }
        }
        console.log(length, 'length');
      },
      allQuery() {
        this.tab1loading = true;
        let college = ['全校'];
        let query = 'tab1';
        if (this.oneentrydate == null) {
          this.oneentrydate = '2017';
        }
        this.query(query, college, [this.oneentrydate]);
      },
      async majorQuery() {
        if (this.majorentrydate.length == 0) {
          message.error('请选择年级');
          return;
        }
        if (this.collegeMajor.length == 0 || this.collegeMajor == null) {
          message.error('请选择学院');
          return;
        }

        if (this.lastCollegeMajor.length == 0 || this.lastCollegeMajor == null) {
          message.error('请选择专业');
          return;
        }
        this.tab3loading = true;
        let res = null;
        try {
          let params = {
            college: this.lastCollegeMajor,
            entrydate: this.majorentrydate,
          };
          res = await defHttp.post({ url: this.Url.getRateByMajor, params });
        } finally {
          console.log(res, 'res');
          this.tab3loading = false;
          this.$nextTick(() => {
            this.drawChart(res.data, 'tab3');
          });
        }
      },
      collegeQuery() {
        if (this.collegeentrydate.length == 0) {
          message.error('请选择年级');
          return;
        }
        if (this.collegetab2.length == 1) {
          message.error('请至少选择两个学院');
          return;
        }
        if (this.collegetab2.length == 0) {
          message.error('请选择学院');
          return;
        }

        this.tab2loading = true;
        let query = 'tab2';
        this.query(query, this.collegetab2, this.collegeentrydate);
      },
      test(value) {
        console.log(this.collegeMajor);
        console.log(value);
      },
      //选择全校
      checkAll(e) {
        this.college = ['全校'];
        this.showGroup = !this.showGroup;
      },

      //选择学院
      checkOne(e) {
        console.log(e);
        this.college = e;
        this.showBox = true;
        if (e.length == 0) {
          this.showBox = false;
        }
      },
      //选择年级
      checkoneentrydate(e) {
        console.log(e.target.value, 'e');
        this.entrydate = [e.target.value];
      },

      //获取学院专业级联数据
      async getCollegeMajorData() {
        const res = await defHttp.get({ url: this.Url.getCollegeMajor });
        //通过map方法将数据转换为级联选择器需要的数据格式
        this.collegeMajorOptions = res.collegeMajor.map((item) => {
          return {
            value: item.college,
            label: item.college,
            children: item.major.map((major) => {
              return {
                value: major,
                label: major,
              };
            }),
          };
        });
        // this.collegeMajor = ['东语学院'];
        console.log(this.collegeMajor, 'collegeMajor');
        console.log(this.collegeMajorOptions, 'collegeMajorOptions1');
      },

      getCollegeOptions() {
        defHttp.get({ url: this.Url.getCollege }).then((res) => {
          this.collegeOptions = res.colleges;
          // 手动添加一个全校字段

          // this.collegeOptions.unshift({ value: '全校', label: '全校' });
          // this.collegeOptions.forEach(option => {
          //  if (option.value !== '全校') {
          //     option.disabled = true;
          //}

          //  });
          //  this.college = ['全校'];
          // this.collegetab2 = ['地理科学学院', '文学院'];
          console.log(this.collegeOptions, 'collegeOptions');
        });
      },
      onMajorEntrydateChange(value) {
        console.log(value, 'value');
        if (value.length > 5) {
          //找到value中不在this.lastMajorEntrydate中的数据
          for (let i = 0; i < value.length; i++) {
            if (!this.lastMajorEntrydate.includes(value[i])) {
              message.error('最多选择五个');
              //删除value[i]
              value.splice(i, 1);
              return;
            }
          }
        }
        this.lastMajorEntrydate = value;
      },
      //学院对比 学院选项回调函数
      onCollegeChange(value) {
        console.log(value, 'value');
        if (value.length > 5) {
          message.error('最多选择五个');
          //和lastCollege比较，如果value中的数据不在lastCollege中则删除
          for (let i = 0; i < value.length; i++) {
            if (!this.lastCollege.includes(value[i])) {
              value.splice(i, 1);
              return;
            }
          }
          return;
        }
        this.lastCollege = value;
      },
      //学院对比 年级选项回调函数
      onCollegeEntrydateChange(value) {
        console.log(value, 'value');
        if (value.length > 5) {
          message.error('最多选择五个');
          //和lastCollegeEntrydate比较，如果value中的数据不在lastCollegeEntrydate中则删除
          for (let i = 0; i < value.length; i++) {
            if (!this.lastCollegeEntrydate.includes(value[i])) {
              value.splice(i, 1);
              return;
            }
          }
          return;
        }
        this.lastCollegeEntrydate = value;
      },
      //专业选项回调函数
      onCollegeMajorChange(value) {
        //将value中的数据与this.lastCollegeMajor比较，如果value中的有this.lastCollegeMajor中没有的数据则添加,反之寻找在this.majorOptions中并且在this.lastCollegeMajor中但是不在value中的数据删除
        for (let i = 0; i < value.length; i++) {
          if (!this.lastCollegeMajor.map((item) => item[1]).includes(value[i])) {
            if (this.majorLength == 5) {
              message.error('最多选择五个');
              //从value中删除value[i]
              value.splice(i, 1);
              return;
            }
            this.lastCollegeMajor.push([this.collegeMajor, value[i]]);
            this.majorLength++;
          }
        }
        for (let i = 0; i < this.lastCollegeMajor.length; i++) {
          if (!value.includes(this.lastCollegeMajor[i][1])) {
            //查看该数据是都在this.majorOptions中
            if (this.majorOptions.find((item) => item.value == this.lastCollegeMajor[i][1])) {
              this.lastCollegeMajor.splice(i, 1);
              console.log('splice');
              this.majorLength--;
            }
          }
        }
        console.log(value, 'value');
      },
      removeTag(index) {
        if (this.majorCheckOn.includes(this.lastCollegeMajor[index][1])) {
          let index1 = this.majorCheckOn.indexOf(this.lastCollegeMajor[index][1]);
          this.majorCheckOn.splice(index1, 1);
        }
        this.lastCollegeMajor.splice(index, 1);
        this.majorLength--;
      },
      //学院选项回调函数
      onMajorCollegeChange(value) {
        let val = value.target.value;
        console.log(val, 'value');
        console.log(this.collegeMajorOptions.find((item) => item.value == val).children, 'majorOptions');
        this.majorOptions = this.collegeMajorOptions.find((item) => item.value == val).children;
        //将this.majorCheckOn中的数据与this.lastCollegeMajor比较，如果this.lastCollegeMajor中有this.majorCheckOn中没有的数据则删除,反之添加
        // for (let i = 0; i < this.majorCheckOn.length; i++) {
        //   if (!this.lastCollegeMajor.includes(this.majorCheckOn[i])) {
        //     this.lastCollegeMajor.push(this.majorCheckOn[i]);
        //   }
        // }
        // for (let i = 0; i < this.lastCollegeMajor.length; i++) {
        //   if (!this.majorCheckOn.includes(this.lastCollegeMajor[i])) {
        //     this.lastCollegeMajor.splice(i, 1);
        //     console.log("splice")
        //   }
        // }
        // this.lastCollegeMajor = this.majorCheckOn;
        console.log(this.lastCollegeMajor, 'lastCollegeMajor');
        this.majorCheckOn = this.lastCollegeMajor.map((item) => item[1]);
        console.log(this.majorCheckOn, 'majorCheckOn');
      },

      clearMajor() {
        this.majorCheckOn = [];
        this.lastCollegeMajor = [];
        this.majorLength = 0;
      },
      clearCollege() {
        this.collegetab2 = [];
        this.collegeentrydate = [];
        this.lastCollege = [];
        this.lastCollegeEntrydate = [];
      },
      //tab2\3图标
      drawChart(data, tab) {
        let seriesData = [];

        let xData = this.majorentrydate.sort((a, b) => a - b);
        if (tab == 'tab2') {
          xData = this.collegeentrydate.sort((a, b) => a - b);
        }
        let k = 0;
        let legendData = [];
        let colors = [
          '#5370c5',
          '#91CC75',
          '#fac858',
          '#ee6666',
          '#73c0de',
          '#FF6A6A',
          '#FFA500',
          '#EE2C2C',
          '#90EE90',
          '#008B8B',
          '#FFC0CB',
          '#FFDAB9',
          '#FFDEAD',
          '#FFE4B5',
          '#FFE4C4',
          '#FFE4E1',
          '#FFEBCD',
          '#FFEFD5',
          '#FFFAF0',
          '#FFFAFA',
          '#FFFFE0',
          '#FFFFF0',
          '#FFFFFF',
          '#F0F8FF',
          '#FAEBD7',
          '#FAF0E6',
          '#FAFAD2',
          '#F5FFFA',
          '#F8F8FF',
          '#F0FFF0',
          '#F0FFFF',
          '#F0E68C',
          '#F0F8FF',
          '#F0FFF0',
          '#F0FFFF',
          '#F4A460',
          '#F5DEB3',
          '#F5F5DC',
          '#F5F5F5',
          '#F5FFFA',
          '#F8F8FF',
          '#F9EBEA',
          '#FAD7A0',
          '#FAF0E6',
          '#FAFAD2',
          '#FAF0E6  ',
        ];
        let isJK = false;
        for (let i in data) {
          console.log(i, 'i');
          legendData.push(i);
          let yData = [];
          // legendData=[];
          console.log(data[i], 'data[i]');

          for (let j in data[i]) {
            console.log(data[i][j].college, 'data[i][j].college111');
            // if(data[i][j].college == ''){
            yData.push(data[i][j].passRate);
            // legendData.push(data[i][j].college);
          }
          console.log(yData, 'yData');
          seriesData.push({
            name: i,
            type: 'bar',
            //设置柱状图大小
            barWidth: 16,
            data: yData,
            //柱子间距
            barGap: '30%',
            //颜色
            itemStyle: {
              normal: {
                label: {
                  show: true, //开启显示
                  position: 'top', //在上方显示
                  formatter: '{c}%',
                  textStyle: {
                    //数值样式
                    color: 'black',
                    fontSize: 10,
                  },
                },
                color: colors[k++],
              },
            },
          });
        }
        console.log(legendData, 'legendData');
        console.log(seriesData, 'dasaaseriresdata');
        // debugger
        let myChart = null;
        console.log(tab, 'tab');
        myChart = document.getElementById('map2');
        if (myChart) {
          myChart = echarts.getInstanceByDom(myChart);
          if (myChart) {
            myChart.dispose();
          }
        }
        myChart = echarts.init(document.getElementById('map2'));
        // 指定图表的配置项和数据
        let option = {
          //title: {
          //  text: '学院四级通过率对比',
          //  textStyle: {
          //    fontSize: 14,
          //  },
          //},
          legend: {},
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow',
            },
            formatter: function (params) {
              //生成tooltip,加上小圆球
              let result = params[0].name + '级通过率<br>';
              for (let i = 0; i < params.length; i++) {
                result += params[i].marker + legendData[i] + ' : ' + params[i].value + '%' + '<br>';
              }
              return result;
            },
          },
          //toolbox: {
          //  show: true,
          //  feature: {
          //    magicType: { show: true, type: ['line', 'bar'] },
          //    restore: { show: true },
          //    saveAsImage: { show: true },
          //  },
          //},
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true,
          },
          xAxis: [
            {
              type: 'category',
              data: xData,
              axisLabel: {
                interval: 0, //代表显示所有x轴标签显示
                rotate: 0, //代表倾斜0度显示
              },
            },
          ],
          yAxis: [
            {
              type: 'value',
              name: '通过率',
              axisLabel: {
                formatter: '{value} %',
              },
            },
          ],
          series: seriesData,
        };
        // 使用刚指定的配置项和数据显示图表。
        console.log(option, 'option');
        console.log(myChart, 'myChart');
        setTimeout(() => {
          myChart.setOption(option);
        }, 1);
      },
      drawChart2(data, tab) {
        let seriesData = [];

        let xData = this.majorentrydate.sort((a, b) => a - b);
        if (tab == 'tab2') {
          xData = this.collegeentrydate.sort((a, b) => a - b);
        }
        let k = 0;
        let legendData = [];
        let colors = [
          '#5370c5',
          '#91CC75',
          '#fac858',
          '#ee6666',
          '#73c0de',
          '#FF6A6A',
          '#FFA500',
          '#EE2C2C',
          '#90EE90',
          '#008B8B',
          '#FFC0CB',
          '#FFDAB9',
          '#FFDEAD',
          '#FFE4B5',
          '#FFE4C4',
          '#FFE4E1',
          '#FFEBCD',
          '#FFEFD5',
          '#FFFAF0',
          '#FFFAFA',
          '#FFFFE0',
          '#FFFFF0',
          '#FFFFFF',
          '#F0F8FF',
          '#FAEBD7',
          '#FAF0E6',
          '#FAFAD2',
          '#F5FFFA',
          '#F8F8FF',
          '#F0FFF0',
          '#F0FFFF',
          '#F0E68C',
          '#F0F8FF',
          '#F0FFF0',
          '#F0FFFF',
          '#F4A460',
          '#F5DEB3',
          '#F5F5DC',
          '#F5F5F5',
          '#F5FFFA',
          '#F8F8FF',
          '#F9EBEA',
          '#FAD7A0',
          '#FAF0E6',
          '#FAFAD2',
          '#FAF0E6  ',
        ];
        let isJK = false;
        for (let i in data) {
          console.log(i, 'i');
          legendData.push(i);
          let yData = [];
          // legendData=[];
          console.log(data[i], 'data[i]');

          for (let j in data[i]) {
            console.log(data[i][j].college, 'data[i][j].college111');
            // if(data[i][j].college == ''){
            yData.push(data[i][j].passRate);
            // legendData.push(data[i][j].college);
          }
          console.log(yData, 'yData');
          seriesData.push({
            name: i,
            type: 'line',
            //设置柱状图大小
            barWidth: 18,
            data: yData,
            //柱子间距
            barGap: '30%',
            //颜色
            itemStyle: {
              normal: {
                label: {
                  show: true, //开启显示
                  position: 'top', //在上方显示
                  formatter: '{c}%',
                  textStyle: {
                    //数值样式
                    color: 'black',
                    fontSize: 10,
                  },
                },
                color: colors[k++],
              },
            },
          });
        }
        // debugger
        let myChart = null;
        console.log(tab, 'tab');
        myChart = document.getElementById('map3');
        if (myChart) {
          myChart = echarts.getInstanceByDom(myChart);
          if (myChart) {
            myChart.dispose();
          }
        }
        myChart = echarts.init(document.getElementById('map3'));
        // 指定图表的配置项和数据
        let option = {
          legend: {},
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow',
            },
            formatter: function (params) {
              //生成tooltip,加上小圆球
              let result = params[0].name + '级通过率<br>';
              for (let i = 0; i < params.length; i++) {
                result += params[i].marker + legendData[i] + ' : ' + params[i].value + '%' + '<br>';
              }
              return result;
            },
          },
          //toolbox: {
          //  show: true,
          //  feature: {
          //    magicType: { show: true, type: ['line', 'bar'] },
          //    restore: { show: true },
          //    saveAsImage: { show: true },
          //  },
          //},
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true,
          },
          xAxis: [
            {
              type: 'category',
              data: xData,
              axisLabel: {
                interval: 0, //代表显示所有x轴标签显示
                rotate: 0, //代表倾斜0度显示
              },
            },
          ],
          yAxis: [
            {
              type: 'value',
              name: '通过率',
              axisLabel: {
                formatter: '{value} %',
              },
            },
          ],
          series: seriesData,
        };
        // 使用刚指定的配置项和数据显示图表。
        console.log(option, 'option');
        console.log(myChart, 'myChart');
        setTimeout(() => {
          myChart.setOption(option);
        }, 1);
      },
      // 获取批次数据
      async getBatch() {
        const getBatch = await defHttp.get({ url: this.Url.getBatch });
        this.batchOptions = getBatch.batches;
        this.batch = this.batchOptions[0].value;
        this.level = this.levelOptions[0].value;
      },
      //获取年级数据
      async getEntrydate() {
        const getEntrydate = await defHttp.get({ url: this.Url.getEntrydate });
        this.entrydateOptions = getEntrydate.entrydates;
        this.oneentrydate = this.entrydateOptions[0].value;
        // this.collegeentrydate = [this.entrydateOptions[0].value];
        // this.majorentrydate = [this.entrydateOptions[0].value];
        // this.entrydate.push (this.entrydateOptions[0].value);
      },
      // 查询数据
      async query(query, college, entrydate) {
        this.visible = false;
        let result = null;
        // console.log(this.college, this.entrydate);
        // //如果this.college和this.batch为null则先赋静态值
        // if (this.college === null || this.college.length === 0) {
        //   this.college = ['全校'];
        // }

        // if (!this.entrydate || this.entrydate.length === 0) {
        //   this.entrydate = ['2017'];
        // }

        try {
          // console.log(this.college, this.entrydate)
          let params = {
            college: college,
            entrydate: entrydate,
            level: 'cet4',
          };
          let url = query == 'tab1' ? this.Url.getAllRate : this.Url.getCollegeRate;
          console.log(params.college, 'college');
          result = await defHttp.post({ url: url, params });
          //使数据按照passRate从大到小排序
          // result.data.sort((a, b) => {
          //   return b.passRate - a.passRate;
          // });
          console.log('data', result.data);
        } finally {
          query == 'tab1' ? (this.tab1loading = false) : (this.tab2loading = false);
          this.$nextTick(() => {
            if (query == 'tab1') {
              this.dataChart(result.data, query);
            } else if (query == 'tab2') {
              this.drawChart(result.data, query);
              this.drawChart2(result.data, query);
            }
          });
        }
      },
    },
  };
</script>

<style lang="less" scoped>
  .container {
    display: flex;
    background-color: #fff;
    padding: 15px;
    border-radius: 4px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.09);
  }

  .query {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    margin-left: 24px;
  }

  .tab3 {
    justify-content: flex-start;
    align-items: center;
    margin-left: 24px;
  }

  .title {
    font-size: 34px;
    color: rgb(8, 8, 8);
    font-weight: bold;
  }
</style>
