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

class KrMonthReportPage extends React.Component {
  constructor (props) {
    super(props)
    this.state = {
      dataSource: [],
      days: 31
    }
  }

  handleSearch = (value) => {
    this.setState({
      searchRules: value
    }, () => {
      this.getRunData(value)
    })
  };

  getRunData = (value) => {
    const { login, dispatch } = this.props
    const json = initFilter(login.OrgId)
    addRuleAndGroups(json, value)
    const days = moment(json.Parameter1, "YYYY-MM-DD").daysInMonth()
    this.setState({ days })
    dispatch({
      type: 'app/getDataByPost',
      url: 'KR/Statistics/GetRunMonthData',
      payload: json,
      onlyData: false,
      onComplete: (res) => {
        if (res && res.IsSuccessful && res.Data) {
          const currState = {
            dataSource: res.Data,
          }
          this.setState(currState)
        }
      }
    })
  };

  getInsertTitle = () => {
    const { formCode } = this.props
    const fieldConfigs = this.props.search.fieldConfigs[formCode] || []
    const exportInserTitle = ['月电量汇总表']
    fieldConfigs.forEach(fc => {
      const { label, value, dataSource } = fc
      const cellText = renderExportCell(value, dataSource, fc, this.props.app.enums || {})
      cellText && exportInserTitle.push(`${label}：${cellText}`)
    })
    return exportInserTitle.join(' ')
  }

  render () {
    const { dataSource, days } = this.state
    const { formId, formCode } = this.props
    const arrs = new Array(days).fill(false)
    const children = arrs.map((item, i) => ({
      title: i + 1,
      dataIndex: "Day" + (i + 1),
      key: "Day" + (i + 1),
    }))
    children.push({
      title: '本月累计',
      dataIndex: 'Total',
      key: 'Total',
    })

    const columns = [
      {
        title: "类别",
        dataIndex: "Category",
        key: "Category",
        render: (value, row) => {
          return {
            children: value,
            props: {
              rowSpan: row.RowSpan,
            },
          }
        }
      },
      {
        title: "母线",
        dataIndex: "Generatrix",
        key: "Generatrix",
        render: (value, row) => {
          return {
            children: value,
            props: {
              rowSpan: row.RowSpan2,
              colSpan: row.ColSpan
            },
          }
        }
      },
      {
        title: "柜号",
        dataIndex: "Container",
        key: "Container",
        render: (value, row) => {
          return {
            children: value,
            props: {
              colSpan: row.ColSpan > 1 ? 0 : row.ColSpan
            },
          }
        }
      },
      {
        title: "电量（KWh）",
        children: children,
      }
    ]
    const exportInserTitle = this.getInsertTitle()
    return (
      <>
        <Search
          formId={formId}
          formCode={formCode}
          onSearch={this.handleSearch}
        />
        <LayoutCard style={{ textAlign: 'right' }}>
          <ExportToExcel
            fileName='月电量汇总表'
            insertRows={[exportInserTitle]}
            tableId='KrMonthReportPageExport'
          />
          <Table
            id='KrMonthReportPageExport'
            size='small'
            bordered
            columns={columns}
            dataSource={dataSource}
            pagination={false}
            scroll={{ x: '100%' }}
            style={{ marginTop: 8 }}
          />
        </LayoutCard>
      </>
    )
  }
}

KrMonthReportPage.propTypes = {
}

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