import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import 'echarts/map/js/china';
import { AjaxService } from '../../../../../core/service/ajax.service';
import { FramePageComponent } from '../../../../frame/core/page/frame.page';
import { FrameService } from 'src/app/core/service/frame.service';

@Component({
  selector: 'app-enrol-report-porvince',
  templateUrl: './enrol-report-porvince.component.html',
  styleUrls: ['./enrol-report-porvince.component.scss']
})
export class EnrolReportPorvinceComponent extends FramePageComponent implements OnInit {
  chart: any;
  chartOption: any;
  dataMap: any;
  batch_id: any;
  batchArr = [];

  constructor() {
    super();
    this.batchArr = FrameService.DictMap['ExamBatchName'];
    this.setChartOption();
  }

  setChartOption() {
    this.chartOption = {
      title: {
        // text: '生源地汇总',
        text: this.dataMap ? ('共【' + this.dataMap['EnrolTotal'].aspect_total + '】人次，【' + this.dataMap['EnrolTotal'].std_total + '】人数') : '',
        left: 'center'
      },
      tooltip: {
        trigger: 'item'
      },
      visualMap: {
        min: 0,
        max: this.dataMap ? this.dataMap['EnrolTotal'].max_province_total : 100,
        left: 'left',
        top: 'top',
        text: ['高', '低'], // 文本，默认为数值文本
        calculable: true
      },
      series: [
        {
          name: '报考人数',
          type: 'map',
          mapType: 'china',
          label: {
            normal: {
              show: true
            },
            emphasis: {
              show: true
            }
          },
          data: this.dataMap ? this.dataMap['ProvinceArray'] : []
        }]
    };
  }

  refreshChart() {
    this.setChartOption();
    if (this.chart)
      this.chart.setOption(this.chartOption);
  }


  requestData() {
    AjaxService.requestMap('enrol/report/province.htm', {batch_id: this.batch_id}, (map) => {
      let max_value = 0;
      for (const s of map.ProvinceArray) {
        s.id = s.province_id + '0000';
        s.name = s.province_abbr;
        s.value = s.std_total;
        if (s.std_total > max_value)
          max_value = s.std_total;
      }
      this.dataMap = map;
      this.dataMap['EnrolTotal'].max_province_total = max_value;
      this.refreshChart();
    });
  }

  onChartInit(chart) {
    this.chart = chart;
  }


  ngOnInit() {
    this.requestData();
  }

  downloadReport() {
    this.FuncService.ajaxDownload('enrol/report/province/download.htm', {batch_id: this.batch_id});
  }
}

