import React from "react";
import { Spin } from "antd";
import { connect } from "dva";
import RadioButtonGroup from "./RadioButtonGroup";
import TrafficLineChart from "./TrafficLineChart";
import PanelNav from "./PanelNav";
import styles from "../styles/common.less";

const relation = {
  enquiryTrendList: {
    name: "海外门户询盘数",
    color: "#7ECEF4",
    index: "viewNum",
  },
  enquiryTrendCNList: {
    name: "分销门户询盘数",
    color: "#036EB8",
    index: "viewNum",
  },
};

const EnquireBasic = ({ data, options, current, loading, dispatch }) => {
  const onChange = (val) => {
    dispatch({
      type: "bigscreen/setEnquireData",
      payload: { current: val },
    });
    dispatch({
      type: "bigscreen/load_enquire_data",
    });
  };

  const parseEnquireData = (lineData) => {
    const legendData = [];
    let xAxisData = [];
    const seriesData = [];
    Object.keys(relation).forEach((key) => {
      if (lineData[key]) {
        legendData.push(relation[key].name);
        xAxisData = lineData[key].map((item) => item.statsTime);
        seriesData.push({
          name: relation[key].name,
          type: "line",
          smooth: false,
          showSymbol: true,
          symbolSize: 10,
          symbol: "circle",
          itemStyle: { color: relation[key].color },
          lineStyle: { width: 2 },
          data: lineData[key].map((item) => item[relation[key].index]),
        });
      }
    });

    return {
      legendData,
      xAxisData,
      seriesData,
    };
  };

  return (
    <Spin spinning={loading}>
      <div className={styles.common}>
        <div className={styles.common_header}>
          <PanelNav title="询盘概况" />
          <div className={styles.common_header_conditions}>
            <RadioButtonGroup
              options={options}
              label={current}
              onChange={onChange}
            />
          </div>
        </div>
        <div className={styles.common_content}>
          <TrafficLineChart {...parseEnquireData(data)} />
        </div>
      </div>
    </Spin>
  );
};

export default connect(({ bigscreen }) => ({
  data: bigscreen.enquireData.data,
  options: bigscreen.enquireData.options,
  current: bigscreen.enquireData.current,
  loading: bigscreen.enquireData.loading,
}))(EnquireBasic);
