// 绘制echarts图表
import getMonthRange from './getMonthRange'
import * as echarts from 'echarts'
import 'echarts-wordcloud';
import router from '@/router/index'
const setChart = (option, chartId, allowClick, selectParam) => {
  let chartDom = document.getElementById(chartId);
  if (!chartDom) {
    return null;
  }
  chartDom.removeAttribute("_echarts_instance_");
  let chart = echarts.init(chartDom);
  chart.setOption(option);
  if (allowClick) {
    chart.on('click', function (params) {
      switch (selectParam.moduleType) {
        case '国会法案':
          // 判断点击的是否为饼图的数据项
          if (params.componentType === 'series' && params.seriesType === 'pie') {
            console.log('点击的扇形名称：', params.name);
            if (selectParam.key === '领域') {
              selectParam.domains = params.name
              if (selectParam.selectedDate.length === 4) {
                selectParam.selectedDate = JSON.stringify([selectParam.selectedDate + '-01-01', selectParam.selectedDate + '-12-31'])
              }
            } else if (selectParam.key === '议院委员会') {
              if (params.name === '众议院' || params.name === '参议院') {
                selectParam.selectedCongress = params.name
                selectParam.selectedOrg = ''
                if (selectParam.selectedDate.length === 4) {
                  selectParam.selectedDate = JSON.stringify([selectParam.selectedDate + '-01-01', selectParam.selectedDate + '-12-31'])
                }
              } else {
                selectParam.selectedOrg = params.name
                selectParam.selectedCongress = ''
                if (selectParam.selectedDate.length === 4) {
                  selectParam.selectedDate = JSON.stringify([selectParam.selectedDate + '-01-01', selectParam.selectedDate + '-12-31'])
                }
              }

            }

            const route = router.resolve({
              path: "/dataLibrary/countryBill",
              query: selectParam
            });
            window.open(route.href, "_blank");
          } else if (params.componentType === 'series' && params.seriesType === 'bar') {
            if (params.name === '已立法') {
              selectParam.selectedStatus = 1
            } else {
              selectParam.selectedStatus = 0
            }
            if (selectParam.selectedDate.length === 4) {
              selectParam.selectedDate = JSON.stringify([selectParam.selectedDate + '-01-01', selectParam.selectedDate + '-12-31'])
            }
            const route = router.resolve({
              path: "/dataLibrary/countryBill",
              query: selectParam
            });
            window.open(route.href, "_blank");
          } else {
            console.log('当前点击', selectParam, params.seriesName, params.name);
            if (params.seriesName !== '通过率') {
              selectParam.selectedDate = JSON.stringify(getMonthRange(params.name))
              if (params.seriesName === '通过法案') {
                selectParam.selectedStatus = 1
              } else {
                selectParam.selectedStatus = null
              }

              const route = router.resolve({
                path: "/dataLibrary/countryBill",
                query: selectParam
              });
              window.open(route.href, "_blank");
            }

          }

      }

    });
  }
  // 容器可能受布局/异步渲染影响，强制一次 resize 保证 canvas 与容器一致
  setTimeout(() => {
    chart.resize();
  }, 0);
  return chart;
};

export default setChart