// 核心库
import React from 'react'
import { connect } from 'dva'
// 组件库
import { Form, Icon, Table } from 'antd'
import { ExportToExcel, LayoutCard, Search } from '@woowalker/feui'
import EnergyDosageModel from './EDBenchMarkingModel'
// 工具库
import { addRuleAndGroups } from '../../../utils/common'
import moment from 'moment'

class EDBenchMarkingPage extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      head: [],
      dataSource: [],
      dateType: '0',
      dateString: moment().format('YYYY'),
      Searchvalue: {}
    }
  }


  onModalRef = (ref) => {
    this.modalChild = ref
  }

  showModal = () => {
    this.modalChild.showModal()
  }

  handleSearch = (value) => {
    this.setState({ Searchvalue: value })
    this.getTableData(value)
  }

  getTableData = (value) => {
    if (!value) value = this.state.Searchvalue
    const json = {
      OrgId: this.props.login.OrgId,
    }
    addRuleAndGroups(json, value)
    this.props.dispatch({
      type: 'app/getDataByPost',
      url: 'ED/EDProdRecord/GetBenchMarkingData',
      payload: json,
      onlyData: false,
      onComplete: (res) => {
        if (res && res.IsSuccessful && res.Data && res.Data.tableHead && res.Data.tableData) {
          const { tableHead, tableData } = res.Data
          this.setState({
            head: tableHead,
            dataSource: tableData
          })
        }
      }
    })
  };

  render() {
    const paginationConfig = {
      pageSizeOptions: ['12', '24', '36'],
      pageSize: 12,
      total: this.state.dataSource.length,
      showSizeChanger: true,
      size: 'small',
      showTotal: () => `共 ${this.state.dataSource.length} 条`
    }
    const { formId, formCode } = this.props
    const columns = []
    this.state.head.forEach((n, i) => {
      var item = {
        title: n.Title,
        dataIndex: n.DataIndex
      }
      columns.push(item)
    })
    const { dispatch, login } = this.props
    const exportInserTitle = this.state.dateType === '0' ? '年' : this.state.dateType === '1' ? '月' : '日'
    return (
      <>
        <LayoutCard style={{ marginBottom: '0.15rem' }}>
          <div className='advanced-search__btns-zone'>
            <Search
              formId={formId}
              formCode={formCode}
              onSearch={this.handleSearch}
            />
            <span
              style={{ cursor: "pointer" }}
              onClick={this.showModal}>
              <Icon type='setting' /> 配置
                </span>
          </div>
        </LayoutCard>
        <LayoutCard style={{ textAlign: 'right' }}>
          <ExportToExcel
            fileName='能耗对标'
            columns={columns}
            dataSource={this.state.dataSource}
            insertRows={[`能耗对标 ${this.state.dateString + exportInserTitle}`]}
          />
          <Table
            bordered
            size="small"
            columns={columns}
            dataSource={this.state.dataSource}
            scroll={{ x: '100%' }}
            pagination={paginationConfig}
            style={{ marginTop: 8 }}
          />
        </LayoutCard>
        <EnergyDosageModel
          onRef={this.onModalRef}
          getTableData={this.getTableData}
          login={login}
          dispatch={dispatch}
        />
      </>
    )
  }
}

EDBenchMarkingPage.propTypes = {
}

export default connect(({ login, app }) => ({ login, app }))(Form.create()(EDBenchMarkingPage))
