// 核心库
import React from 'react'
import { connect } from 'dva'
// 组件库
import { Row, Col } from 'antd'
import { Search, LayoutCard, IFComponent } from '@woowalker/feui'
import CommonChart from '../../Chart/CommonChart'
// 工具库
import { initFilter, addRuleAndGroups } from "../../../utils/common"
// 样式
import './index.less'

const { GradientCard } = LayoutCard

class BDHisTStatisticalPage extends React.Component {
  constructor (props) {
    super(props)
    this.state = {
      statisticalDatas: [],
      chartData: {}
    }
  }

  handleSearch = (value) => {
    const { data, formParam, login, dispatch } = this.props
    value.rules.push({
      field: "Keyword",
      operator: 0,
      value: data.id,
      isCustom: true,
      isSysParam: false,
    })
    value.rules.push({
      field: "Parameter1",
      operator: 0,
      value: formParam.Parameter1,
      isCustom: true,
      isSysParam: false,
    })

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

  render () {
    const { statisticalDatas, chartData } = this.state
    const { selectedNodes = [] } = this.props.data
    const titleText = selectedNodes.length ? `${selectedNodes.map(item => item.node.name).join('-')}` : ''
    return (
      <div className='bd-page__monitor'>
        <Search
          formId={this.props.formId}
          formCode={this.props.formCode}
          onSearch={this.handleSearch}
        />
        <LayoutCard title='消耗分析'>
          <Row gutter={16}>
            <Col span={19}>
              <CommonChart
                chartId='BDHisTStatisticalPage'
                {...chartData}
                markLineMax
                markLineMin
                markLineAvg
                magicType={['line', 'bar']}
                titleText={titleText}
                style={{ height: 375 }}
              />
            </Col>
            <Col span={5}>
              {
                statisticalDatas ? statisticalDatas.map((item, index) => {
                  return (
                    <GradientCard
                      key={'BD-Static-GradientCard' + index}
                      title={item.Name}
                      colorIndex={index}
                      style={{ marginBottom: index === statisticalDatas.length - 1 ? 0 : '15px' }}>
                      <span style={{ fontSize: 20, lineHeight: 1 }}>
                        {`${item.Value}${item.Unit ? ' ' + item.Unit : ''}`}
                      </span>
                      <IFComponent IF={!!item.Value2}>
                        <span style={{ marginTop: 5 }}><br />{item.Value2}</span>
                      </IFComponent>
                    </GradientCard>
                  )
                }) : null
              }
            </Col>
          </Row>
        </LayoutCard>
      </div>
    )
  }
}

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