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

class EmEnergyMaxDemandPage extends React.Component {
  constructor (props) {
    super(props)
    this.state = {
      chartData: {}
    }
  }

  handleSearch = (value) => {
    value.rules.push({
      field: 'Keyword',
      operator: 0,
      value: this.props.data.TreeSelected.join(','),
      isCustom: true,
      isSysParam: false,
    })
    value.rules.push({
      field: 'Parameter1',
      operator: 0,
      value: this.props.formParam.Parameter1,
      isCustom: true,
      isSysParam: false,
    })

    const json = initFilter(this.props.login.OrgId)
    addRuleAndGroups(json, value)
    this.props.dispatch({
      type: 'app/getDataByPost',
      url: 'EM/Monitoring/GetEnergyMaxDemandData',
      payload: json
    }).then(res => {
      if (res && res.chartData) {
        const { chartData } = res
        this.setState({
          chartData: {
            xAxis: chartData.XAxis[0].Data,
            yAxis: chartData.YAxis,
            data: chartData.Data
          }
        })
      }
    })
  }

  render () {
    const { selectedNodes = [] } = this.props.data
    const saveImgName = selectedNodes.length ? `${selectedNodes.map(item => item.node.name).join('-')}最大需量` : '最大需量'
    return (
      <div className='scroll_page'>
        <Search
          formId={this.props.formId}
          formCode={this.props.formCode}
          onSearch={this.handleSearch}
        />
        <LayoutCard style={{ height: 'calc(100% - 88px)' }}>
          <CommonChart
            chartId='EmEnergyMaxDemandPage'
            {...this.state.chartData}
            saveImgName={saveImgName}
          />
        </LayoutCard>
      </div>
    )
  }
}

export default connect(({ login, app }) => ({ login, app }))(EmEnergyMaxDemandPage)
