import React, { useState, useEffect } from 'react';
import { Input, Menu, Dropdown, Button, Icon, message, Table, Divider, Tag, Select, Form, Alert } from 'antd';
import { FormattedMessage } from 'umi-plugin-react/locale';
import channels from './channelList.less';
import router from 'umi/router';
import request from '../../utils/request'

const { TextArea } = Input;
const { Option } = Select;

const header = props => {

  // eslint-disable-next-line react-hooks/rules-of-hooks
  useEffect(() => {
    getById()
  }, [])
  // 查询当前信息
  function getById() {
    request({
      url: `/api/admin/channel/${props.location.query.id}`,
      method: 'GET',
  // eslint-disable-next-line no-shadow
  }).then(data => {
      props.form.setFieldsValue(data)
  })
  }

  // 编辑
  function editChannel(data) {
      request({
        url: `/api/admin/channel/${props.location.query.id}`,
        method: 'PUT',
        data: { ...data },
    // eslint-disable-next-line no-shadow
    }).then(data => {
         // eslint-disable-next-line no-unused-expressions
         router.go(-1)
    })
  }

  function handleSubmit(e) {
    e.preventDefault();
    props.form.validateFields((err, values) => {
      if (!err) {
        console.log('values', values)
        editChannel({ ...values })
      }
    });
  }
  function go() {
    router.push({
      pathname: '/channel/channelList',
    });
  }

  const { getFieldDecorator } = props.form;

  return (
  <>
        <Form labelCol={{ span: 7 }} wrapperCol={{ span: 12 }} onSubmit={handleSubmit}>
        <Form.Item label="渠道名称">
          {getFieldDecorator('channelName', {
            rules: [{ required: true, message: '请输入渠道名称' }],
          })(<Input className={channels.inpuit} />)}
        </Form.Item>
        <Form.Item label="联系人">
          {getFieldDecorator('channelContact', {
            rules: [{ required: true, message: ' 请输入联系人' }],
          })(<Input className={channels.inpuit} />)}
        </Form.Item>
        <Form.Item label="联系电话">
          {getFieldDecorator('contactTel', {
            rules: [{ required: true, message: '请输入联系电话' }],
          })(<Input className={channels.inpuit} />)}
        </Form.Item>
        <Form.Item label="说明描述">
        {getFieldDecorator('explain', {
        })(<TextArea className={channels.inpuitTxt} rows={8} />)}
        </Form.Item>
        <Form.Item wrapperCol={{ span: 15, offset: 7 }}>
          <Button type="primary" htmlType="submit">
            保存
          </Button>
          <Button className={channels.formButton} onClick = { go } type="primary" htmlType="submit">
            取消
          </Button>
        </Form.Item>
      </Form>
  </>
)
}
const WrappedNormalLoginForm = Form.create({ name: 'header' })(header);
export default WrappedNormalLoginForm
