// 核心库
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 moment from 'moment'
// 样式
import './index.less'

const { GradientCard } = LayoutCard

class BDMonitorHisPage extends React.Component {
  constructor (props) {
    super(props)
    this.state = {
      statisticalDatas: [],
      chartData: {},
      type: '1',
      day: moment().format('YYYY-MM-DD')
    }
  }

  handleSearch = (value) => {
    const { data, login, dispatch } = this.props
    value.rules.push({
      field: "Keyword",
      operator: 0,
      value: data.id,
      isCustom: true,
      isSysParam: false,
    })
    value.rules.push({
      field: "Parameter4",
      operator: 0,
      value: data.isNode,
      isCustom: true,
      isSysParam: false,
    })
    value.rules.push({
      field: "Parameter2",
      operator: 0,
      value: this.state.type,
      isCustom: true,
      isSysParam: false,
    })
    value.rules.push({
      field: "Parameter3",
      operator: 0,
      value: this.state.day,
      isCustom: true,
      isSysParam: false,
    })
    const json = initFilter(login.OrgId)
    addRuleAndGroups(json, value)
    dispatch({
      type: 'app/getDataByPost',
      url: 'BD/BDAppliance/GetMeasureHisData',
      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='BDMonitorHisPage'
                {...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-Monitor-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 }))(BDMonitorHisPage)
