import React, { useState, useEffect,useRef } from 'react';
import { Input ,Tabs,message,Form, Button} from 'antd';
const { TabPane } = Tabs;

import {connect} from 'umi';

const layout = {
  labelCol: { span: 2},
  // wrapperCol: { span: 8 },
};

import { RA, ResultClear } from '@/utils/method';
import TitleBack from '@/components/TitleBack/TitleBack';
import FileUpload from '@/components/FileUpload/FileUpload';
import { validateMessages } from '@/utils/params';
import PictureOptionsRow from '@/components/PictureOptions/PictureOptionsRow';
import TextArea from 'antd/lib/input/TextArea';
import SelectCommunity from '@/components/SelectCommunity';
import Line from '@/components/Line/Line';
import TimeComfirm from '@/components/TimeComfirm/TimeComfirm';


const module="CommunityAnnouncement"
const Add = (props:any) => {

  const { dispatch, Result } = props;
  const formRef = useRef(null)

  useEffect(() => {
  //   var obj = {
  //     "noticTitle":"Garden公告",
  //     // "communityNum":"珠江丽景小区",
  //     "noticText":"今晚晚上停水4小时",
  //     "noticScope":"1",
  //     "noticStartTime":"2020-07-01 12:30:00",
  //     "noticEndTime":"2020-08-01 9:30:00",
  //     "file":["A.jpg"]
  //   }
  //  formRef.current.setFieldsValue(obj)
  }, [])

  useEffect(() => {
    if (Result != null) {
      console.log("页面结果")
      console.log(Result)
      if (Result.error_code == undefined) {
        console.log("服务器有问题")
        message.error("服务器有问题，请求失败",5)
      }
      if (Result.error_code == "0000") {
        ResultClear(module,dispatch)
        history.go(-1)
      }
    }
  }, [Result])

  const onFinish = ((values: any) => {
    var tmp = values
     tmp.communityNum = values.community.value
     tmp.noticScope= ""+values.community.index
    // "communityNum":["珠江丽景小区一期","A4"],
    // "noticText":"今晚晚上停水4小时",
    // "noticScope":"1",
    delete  tmp.community 
    console.log(tmp)
    RA(28,values, module, dispatch);
  })

  return (
    <div style={{ width: "100%", minWidth: 1020, padding: 34, backgroundColor: "#ffffff" }}>     

      <TitleBack title="Add Announcement" ></TitleBack>
      
      <Form ref={formRef} {...layout} name="nest-messages" onFinish={onFinish} validateMessages={validateMessages}>
  
        
      <Form.Item name={"community"} label="服务小区" rules={[]}><SelectCommunity /></Form.Item>
      <Form.Item name={"noticTitle"} label="公告标题" rules={[]}><Input style={{width: 200}}  /></Form.Item>
      <Form.Item name={"noticText"} label="公告内容" rules={[]}><TextArea  style={{width: 400,height:100}}  /></Form.Item>
        
        <Form.Item style={{ display: 'inline-block', width: '200', marginLeft: 100 }} name={"file"}>
        <PictureOptionsRow
            action="/tos/image/upload"     
            data={{
              imageType: 'tosNotice',
            }}
          />
        </Form.Item>
        <Form.Item name={"noticStartTime"} label="生效日期" rules={[]}><TimeComfirm /></Form.Item>
        <Form.Item name={"noticEndTime"} label="结束日期" rules={[]}><TimeComfirm /></Form.Item>
        <Line></Line>
        <Form.Item wrapperCol={{ ...layout.wrapperCol, offset: 2 }}><Button type="primary" htmlType="submit">Submit</Button></Form.Item>
      </Form>
    </div>
  );
};

function map(state:any) {
  const { Result} = state[module]
  return { Result}
}
export default connect(map)(Add);