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 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({
        url: '/api/admin/taNewsType/' + newsId,
        method: 'GET',
    }).then((data) => {
        console.log(data)
        setNewsData(data)
    })
  }
  }

  const fields = [
    {
      label: '所属项目',
      name: 'buildingId',
      render: <BuildSelect />,
      value: newsData.buildingId,
      rules: [
        {required: true, message: '请选择所属项目'},
      ]
    },
    {
      label: '类型图',
      name: 'newsTypeImg',
      type: FieldTypes.ImageUploader,
      value: newsData.newsTypeImg,
    },
    {
      label: '名称',
      name: 'newsTypeName',
      type: FieldTypes.Text,
      value: newsData.newsTypeName,
      rules: [
        {required: true, message: '请输入咨询名称'},
      ]
    },
  ]

   
  const handleSubmit = (values) => {
    if(newsId){
      values.newsTypeId = newsId
      request({
        url: '/api/admin/taNewsType/' + newsId,
        method: 'PUT',
        data: values,
      }).then((data) => {
        cancelPage()
      }).catch((err) => {
        message.info(err.msg || err.message)
      })
      }else{
      request({
        url: '/api/admin/taNewsType',
        method: 'POST',
        data: values,
      }).then((data) => {
        cancelPage()
      }).catch((err) => {
        message.info(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
