import React, { useState, useEffect } from 'react';
import { Input, Button, Row, Col, Form, Alert, notification } from 'antd';
import router from 'umi/router';
import Styles from './style.less';
import request from '../../../utils/request';
import apis from '../../../services/apis';

function body(props) {
  const { getFieldDecorator } = props.form;

  // eslint-disable-next-line react-hooks/rules-of-hooks
  let data = {}
  const { id } = props.location.query;

  if (id !== '') {
    // eslint-disable-next-line react-hooks/rules-of-hooks
    useEffect(() => {
      getById(id)
    }, [])
  }

  function handleSubmit(e) {
    e.preventDefault();
    props.form.validateFields((err, values) => {
      if (!err) {
        // 提交数据
        submitData(values)
      }
    });
  }

  // 获取详情信息
  function getById(currentId) {
    request({ ...apis.buildingType.getById, urlData: { id: currentId } }).then(res => {
      data = res
      props.form.setFieldsValue(res)
    })
  }

  const openNotificationWithIcon = (type, message) => {
    notification[type]({
      message,
      description:
        '',
    });
  }

  function submitData(dataSources) {
    if (id !== '') {
      // 修改
      updateType(dataSources)
      return
    }

    dataSources.status = 1
    dataSources.createDate = new Date()
    request({ ...apis.buildingType.add, data: { ...dataSources } }).then(() => {
      // eslint-disable-next-line no-unused-expressions
      openNotificationWithIcon('success', '操作成功')
      router.go(-1)
    }).catch(err => {
      
    })
  }

    // 修改
    function updateType(row) {
      // const { url, method } = apis.buildingType.update
      // const tempUrl = url.substring(0, url.lastIndexOf('id')).concat(row.buildingTypeId)

      // request({ url: tempUrl, method, data: { ...row } }).then(() => {
      //   router.go(-1)
      // })
      request({ ...apis.buildingType.update, urlData: { id: row.buildingTypeId }, data: { ...row } }).then(() => {
        // eslint-disable-next-line no-unused-expressions
        openNotificationWithIcon('success', '操作成功')
        router.go(-1)
      }).catch(err => {
        
      })
    }

  return (
    <>
        <Form onSubmit={handleSubmit} style={{ width: '500px', margin: 'auto' }}>
          <Form.Item style={{ display: 'none' }}>
            {getFieldDecorator('buildingTypeId')(
              <Input
                placeholder="buildingTtypeId"
              />,
            )}
          </Form.Item>
          <Form.Item>
            {getFieldDecorator('buildingTypeName', {
              rules: [{ required: true, message: '请输入名称' }],
            })(
              <Input
                placeholder="名称"
              />,
            )}
          </Form.Item>
          <Form.Item style={{ display: 'flex', justifyContent: 'space-between' }}>
            <Button type="primary" htmlType="submit" className={ Styles.addButton } style={{ margin: '0' }}>
              确定
            </Button>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <Button onClick={() => router.go(-1)}>
              取消
            </Button>
          </Form.Item>
        </Form>
    </>
  )
}

const WrappedBuidingTypeForm = Form.create({ name: 'body' })(body);

export default WrappedBuidingTypeForm
