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 apis from '../../services/apis';
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(params) {
    request({ ...apis.channelList.getById, urlData: { id: props.location.query.id } }).then((data) => {
      props.form.setFieldsValue(data)
    }).catch((err) => {
      console.log(err)
      message.info(err.msg || err.message)
    })
  }

  // 编辑
  function editChannel(data) {
    request({ ...apis.channelList.editChannel, urlData: { id: props.location.query.id }, data: { ...data } }).then((data) => {
      router.go(-1)
    }).catch((err) => {
      console.log(err)
      message.info(err.msg || err.message)
    })
  }


  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: 6 }} 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: 8 }}>
          <div style={{ width: '190px' }}>
            <Button type="primary" htmlType="submit">
              保存
          </Button>
            <Button className={channels.formButton} onClick={go} htmlType="submit">
              取消
          </Button>
          </div>
        </Form.Item>
      </Form>
    </>
  )
}
const WrappedNormalLoginForm = Form.create({ name: 'header' })(header);
export default WrappedNormalLoginForm