// 核心库
import React from 'react'
import { connect } from 'dva'
// 组件库
import { Search } from '@woowalker/feui'
import EditableTable from '../../EditListTable/EditableTable'
import { LayoutCard, ExportToExcel } from '@woowalker/feui'
import CommonChart from '../../Chart/CommonChart'
import { initFilter, addRuleAndGroups } from "../../../utils/common"

class EDEnergyPredictionByTimePage extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      searchParams: {},
      columns: [],
      dataSource: [],
      data: [],
      chartData: {},
      chartSumData: {},
      stateData: [],
    }
  }
  onBarRef = (ref) => {
    this.barChild = ref
  }
  onBarReft = (ref) => {
    this.barChildt = ref
  }
  handleSearch = (value) => {
    const { data } = this.props
    value.rules.push({
      field: 'Parameter4',
      operator: 0,
      value: data.selectedNodes ? data.selectedNodes[0].node.id : null,
      isCustom: true,
      isSysParam: false,
    })

    // 获取数据
    const { login, dispatch } = this.props
    const json = initFilter(login.OrgId)
    addRuleAndGroups(json, value)

    dispatch({
      type: 'app/getDataByPost',
      url: 'ED/EDProdRecord/EnergyPredictionByTime',
      payload: json,
      onComplete: (res) => {
        if (res) {
          const { tableData, chartData, chartSumData } = res
          const currState = {
            data: tableData,
            chartData: {
              xAxis: chartData.XAxis[0].Data,
              yAxis: chartData.YAxis,
              data: chartData.Data
            },
            chartSumData: {
              xAxis: chartSumData.XAxis[0].Data,
              yAxis: chartSumData.YAxis,
              data: chartSumData.Data
            },
            columns: tableData.columns,
            dataSource: tableData.data,
          }
          this.setState(currState, () => {
            let data = [];
            for (var i = 0, len = this.state.dataSource.length; i < len; i++) {
              let result = {}
              this.state.dataSource[i].forEach(t => {
                result[t.fieldName] = t.value
              });
              data.push(result);
            }
            this.setState({ stateData: data })
          })
        }
      }
    })
  }

  render() {
    const { formId, formCode } = this.props
    const {
      columns,
      dataSource,
      stateData,
      chartData,
      chartSumData
    } = this.state
    return (
      <div className='scroll_page'>
        <Search
          formId={formId}
          formCode={formCode}
          onSearch={this.handleSearch}
        />
        <LayoutCard title='每日对比' style={{ marginBottom: '0.2rem' }}>
          <CommonChart
            chartId='EDPredByTimePage'
            {...chartData}
            style={{ height: 300 }}
          />
        </LayoutCard>
        <LayoutCard title='累计对比' style={{ marginBottom: '0.2rem' }}>
          <CommonChart
            chartId='EDPredSumByTimePage'
            {...chartSumData}
            style={{ height: 300 }}
          />
        </LayoutCard>
        <LayoutCard style={{ textAlign: 'right' }}>
          <ExportToExcel
            fileName='用能预测按时间'
            columns={columns}
            dataSource={stateData}
            insertRows={[`用能预测按时间`]}
          />
          <EditableTable data={dataSource} columns={columns} />
        </LayoutCard>
      </div>
    )
  }
}
export default connect(({ login }) => ({ login }))(EDEnergyPredictionByTimePage)