import React from 'react'
import { connect } from 'dva'
import { Button, Row, Col, Form, Input, message } from 'antd'
import { Socket } from '@woowalker/feui'
import config from '../../../config'

import EditBaseComponent from "../../../baseComponents/EditBaseComponent/index.js"

const { TextArea } = Input
class SendInfoToClientPage extends EditBaseComponent {
  constructor (props) {
    super(props)
    this.state = {
      data: {},
      btns: [],
      title: '流程审核',
      historyData: [],
      remark: '',
    }
    this.socketIns = new Socket({ url: config.webSocketHost })
  };

  onLoadData () {
    super.onLoadData()
    const columns = [
      { controlType: 3, field: 'NotificatoinType', label: '公告类型', data: { enumName: 'FMNotificationTypeEnum' } },
      { controlType: 3, field: 'ShowInfoType', label: '显示类型', data: { enumName: 'FMNotificationShowTypeEnum' } },
      { controlType: 3, field: 'CheckOrgType', label: '组织范围', data: { enumName: 'FMNotificationOrgTypeEnum' } },
      { controlType: 3, field: 'ClientType', label: '平台范围', data: { enumName: 'FMNotificationClientTypeEnum' } },
      { controlType: 1, field: 'Title', label: '标题', },
    ]
    this.setEditConfig({
      editConfig: {
        columns,
      },
    })
    this.setState({ btnLoading: false })
    this.setState({
      data: {
        NotificatoinType: 0,
        ShowInfoType: 0,
        CheckOrgType: 0,
        ClientType: 99,
      },
      sendInfo: null,
    })
  }


  getSqlsByFormId = () => {
    const { login, dispatch } = this.props
    const { data, sendInfo } = this.state

    if (!this.state.sendInfo) {
      message.error('请输入公告信息')
      return
    }

    var json = {
      ...data,
      Message: sendInfo,
      UserId: login.userId,
      OrgId: login.OrgId,
    }
    this.setState({ btnLoading: true })

    if (login) {
      this.socketIns.send("M0002", json)
      message.success('发布成功')
      setTimeout(() => { this.setState({ btnLoading: false }) }, 2000)
    }
    else {
      dispatch({
        type: 'PFForm/sendInfoToClient',
        payload: json,
        onComplete: (ret) => {
          if (ret) {
            message.success('发布成功')
          }
          setTimeout(() => { this.setState({ btnLoading: false }) }, 2000)
        }
      })
    }

  }


  render () {
    const { btnLoading, sendInfo, data } = this.state
    const headColumnConfig = this.getEditConfig()
    const columns = headColumnConfig ? headColumnConfig.columns : []

    return (
      <div>
        <Form>
          <Row style={{ marginTop: '20px' }}>
            {
              this.createControls(columns, data)
            }
            <Col span={24}>
              <Form.Item>
                <TextArea rows={10} style={{ width: "100%" }} value={sendInfo}
                  placeholder={'公告信息'}
                  onChange={e => {
                    let tmpValue = (e && e.target && e.target.value ? e.target.value : (typeof e === 'object' ? null : e))
                    this.setState({
                      sendInfo: tmpValue
                    })
                  }} />
              </Form.Item>
            </Col>
          </Row>
          <Row style={{ marginTop: '20px' }}>
            <Col style={{ textAlign: 'right' }}>
              <Button type={'primary'} icon={'eye'} loading={btnLoading} onClick={this.getSqlsByFormId} >发布</Button>
            </Col>
          </Row>
        </Form>
      </div>
    )
  }
}
export default connect(({ login, loading, custom, app }) => ({ login, loading, custom, app }))(Form.create()(SendInfoToClientPage))
