// 核心库
import React from 'react'
import { connect } from 'dva'
// 组件库
import { Table, Row, Col } from 'antd'
import { IFComponent, LocalSearch, FullDatePicker, LayoutCard } from '@woowalker/feui'
import CommonChart from '../../Chart/CommonChart'
import PieEchart from '../KR/PieEchart'
// 工具库
import { initFilter, addRuleAndGroups } from '../../../utils/common'
import moment from 'moment'

const { GradientCard } = LayoutCard

class EMEnergyConsumptionPage extends React.Component {
  constructor (props) {
    super(props)
    this.state = {
      statisticalDatas: [],
      head: [],
      data: [],
      dateType: '1',
      dateString: moment().format('YYYY-MM'),
      pies: []
    }
  }

  handleSearch = (value) => {
    const {dateType, dateString } = this.state
    value.rules.push({
      field: 'Parameter3',
      operator: 0,
      value: dateString,
      isCustom: true,
      isSysParam: false,
    })
    value.rules.push({
      field: 'Parameter2',
      operator: 0,
      value: dateType,
      isCustom: true,
      isSysParam: false,
    })
    value.rules.push({
      field: 'Parameter1',
      operator: 0,
      value: this.props.data.selectedNodes[0].node.id,
      isCustom: true,
      isSysParam: false,
    })
    const { login, dispatch } = this.props
    const json = initFilter(login.OrgId)
    addRuleAndGroups(json, value)

    dispatch({
      type: 'app/getDataByPost',
      url: 'EM/EMEnergyDosage/GetEnergyConsumptionData',
      payload: json
    }).then(res => {
      if (res) {
        const { chartData } = res
        this.setState({
          ...res,
          chartData: {
            xAxis: chartData.XAxis[0].Data,
            yAxis: chartData.YAxis,
            data: chartData.Data
          }
        })
      }
    })
  }

  render () {
    const { statisticalDatas, head, data, pies, chartData } = this.state
    return (
      <>
         <LocalSearch
          isRemember
          shareKey='EMEnergyConsumptionPage'
          setExtraData={({ dateString, dateType }, callback) => this.setState({ dateString, dateType }, callback)}
          getExtraData={() => ({ dateType: this.state.dateType, dateString: this.state.dateString })}
          onSearch={this.handleSearch}
          extraSearch={[
            <FullDatePicker
              dateType={this.state.dateType}
              dateString={this.state.dateString}
              onlyItem
              onChange={({ dateString, dateType }) => this.setState({ dateString, dateType })}
              options={['0', '1']}
            />          
          ]}
        />
        <LayoutCard title='矿山安全损耗统计' style={{ marginBottom: 16 }}>
          <Row gutter={16}>
            <Col span={19}>
              <CommonChart
                chartId='EMEnergyConsumptionPage'
                {...chartData}
                style={{ height: 360 }}
              />
            </Col>
            <Col span={5}>
              {
                statisticalDatas.map((item, index) => {
                  return (
                    <GradientCard
                      key={'GradientCard' + index}
                      title={item.Name}
                      colorIndex={index}
                      style={{ marginBottom: index === statisticalDatas.length - 1 ? '0' : '0.15rem' }}>
                      <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>
                  )
                })
              }
            </Col>
          </Row>
        </LayoutCard>
        <IFComponent IF={pies.length}>
          <LayoutCard title='损耗占比' style={{ marginBottom: 16 }}>
            <Row>
              {
                pies.map((item, index) => {
                  return (
                    <Col span={4} key={"EMEnergyConsumptionPage_col_" + index}>
                      <PieEchart key={item.Key} data={item} />
                    </Col>
                  )
                })
              }
            </Row>
          </LayoutCard>
        </IFComponent>
        <LayoutCard title='损耗同比'>
          <Table
            size='small'
            columns={head.map(({ Title: title, DataIndex: dataIndex }) => ({ title, dataIndex }))}
            dataSource={data}
            pagination={{ pageSize: 5 }}
            style={{ marginTop: 8 }}
          />
        </LayoutCard>
      </>
    )
  }
}

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