// 绘制echarts图表
import getMonthRange from './getMonthRange'
import getQuarterRange from './getQuarterRange';
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 (selectParam.key === 1) {
            // console.log('当前点击', selectParam, params.seriesName, params.name);
            selectParam.selectedStatus = params.seriesName
            selectParam.selectedDate = JSON.stringify(getMonthRange(params.name))
            const route = router.resolve({
              path: "/dataLibrary/countryBill",
              query: selectParam
            });
            window.open(route.href, "_blank");
            return
          } else if (selectParam.key === 2) {
            selectParam.domains = params.name
            const route = router.resolve({
              path: "/dataLibrary/countryBill",
              query: selectParam
            });
            window.open(route.href, "_blank");
            return
          } else if (selectParam.key === 3) {
            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");
            return
          } else {
            selectParam.selectedStatus = params.name
            const route = router.resolve({
              path: "/dataLibrary/countryBill",
              query: selectParam
            });
            window.open(route.href, "_blank");
          }
          break

        case '政令':
          if (params.componentType === 'series' && params.seriesType === 'pie') {
            selectParam.domains = params.name
            const route = router.resolve({
              path: "/dataLibrary/dataDecree",
              query: selectParam
            });
            window.open(route.href, "_blank");
          } else if (params.componentType === 'series' && params.seriesType === 'bar') {
            const year = params.name.slice(0, 4)
            const quatarNum = Number(params.name[params.name.length - 1])
            selectParam.selectedDate = JSON.stringify(getQuarterRange(year, quatarNum))
            const route = router.resolve({
              path: "/dataLibrary/dataDecree",
              query: selectParam
            });
            window.open(route.href, "_blank");
          }
          break

        case '科技智库报告':
          if (selectParam.key === 1) {
            selectParam.domains = params.seriesName
            const year = params.name.slice(0, 4)
            const quatarNum = Number(params.name[params.name.length - 1])
            selectParam.selectedDate = JSON.stringify(getQuarterRange(year, quatarNum))
            const route = router.resolve({
              path: "/dataLibrary/dataThinkTank",
              query: selectParam
            });
            window.open(route.href, "_blank");
            return
          } else if (selectParam.key === 2) {
            selectParam.domains = params.name
            const route = router.resolve({
              path: "/dataLibrary/dataThinkTank",
              query: selectParam
            });
            window.open(route.href, "_blank");
            return
          }
      }

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

export default setChart