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 '../../channel/channelList.less';
import BuildSelect from '../../../components/SelectButton/BuildSelect'
import XForm, { FieldTypes } from '../../../components/XForm';
import router from 'umi/router';
import apis from '../../../services/apis';
import request from '../../../utils/request'

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

const header = props => {
  const newsId = props.location.query.id
  console.log("newsId" + newsId);
  const [ newsData, setNewsData ] = useState({})
  if(newsId){
    useEffect(() => {
      getNewsData(newsId);
    },[])

  // 查询列表
  const getNewsData = (newsId) => {
    request({
        ...apis.newsType.get,
        urlData: { id: newsId }
    }).then((data) => {
        setNewsData(data)
    }).catch((err) => {
      message.error(err.msg || err.message)
    })
  }
  }

  const fields = [
    // {
    //   label: '所属项目',
    //   name: 'buildingId',
    //   render: <BuildSelect />,
    //   value: newsData.buildingId,
    //   rules: [
    //     {required: true, message: '请选择所属项目'},
    //   ]
    // },
    {
      label: '类型图',
      name: 'newsTypeImg',
      type: FieldTypes.ImageUploader,
      value: newsData.newsTypeImg,
      help: '建议图片尺寸：660px*416px',
    },
    {
      label: '名称',
      name: 'newsTypeName',
      type: FieldTypes.Text,
      value: newsData.newsTypeName,
      rules: [
        {required: true, message: '请输入资讯名称'},
      ]
    },
  ]

   
  const handleSubmit = (values) => {
    if(newsId){
        values.newsTypeId = newsId
        request({ ...apis.newsType.put, urlData: { id: newsId }, data: { ...values }}).then((data) => {
          cancelPage();
        }).catch((err) => {
          message.error(err.msg || err.message)
        })
      }else{
        request({ ...apis.newsType.post, data: { ...values } }).then((data) => {
          cancelPage();
        }).catch((err) => {
          message.error(err.msg || err.message)
        })
      }
  }

  const cancelPage = () => {
    router.push({
      pathname: '/news/type/NewsType',
    });
  }

  return (
    <XForm onSubmit={handleSubmit} onCancel={cancelPage} fields={fields}></XForm>
  )
}

const WrappedNormalLoginForm = Form.create({ name: 'header' })(header);
export default WrappedNormalLoginForm
