import React, { useState, useEffect } from 'react';
import { Form, Input, Button, Icon, Select, message, Table, Divider, Tag, Pagination, Modal, DatePicker, InputNumber, Checkbox, Row, Col} from 'antd';
import { FormattedMessage } from 'umi-plugin-react/locale';
import XForm, { FieldTypes } from '../../../../components/XForm';
import router from 'umi/router';
import moment from 'moment';
import styles from '../../../style/GoodsList.less';
import BuildSelect from '../../raise/edit/components/BuildSelect/BuildSelect'
import ImageUpload from '../../../../components/XForm/ImageUpload'
import apis from '../../../../services/apis';
import request from '../../../../utils/request';
import AuthButton from '@/components/AuthButton';
import SalesBatch from '../edit/components/SalesBatch/SalesBatch';

const { Option } = Select;
const { MonthPicker, RangePicker, WeekPicker } = DatePicker;




const header = props => {
  const { salesBatchId } = props.location.query
  console.log(salesBatchId)
  const [ saleBatchData, setSaleBatchData ] = useState({})
  const [showHelp, setShowHelp] = useState(false)
  if(salesBatchId){
    useEffect(() => {
      getSaleBatchData(salesBatchId);
    },[])

  // 查询列表
  const getSaleBatchData = (salesBatchId) => {
      request({ ...apis.system.taPolicy, urlData: {id: salesBatchId} }).then((data) => {
          console.log(data)
          setSaleBatchData(data)
      })
    }
  }

  const cancelPage = () =>{
    router.push({
      pathname: '/house/preSelect/list',
    });
  }


  function handleSubmit (e) {
    e.preventDefault();
    props.form.validateFields((err, values) => {
      if (!err){
        console.log(values)
        let {liveTime, ...submitValue} = values
        if (values.preselectionStartTime > values.preselectionEndTime){
          message.info("预选结束时间大于预选开始时间")
          return;
        }
        submitValue.preselectionStartTime = moment(submitValue.preselectionStartTime).format('YYYY-MM-DD HH:mm:ss')
        submitValue.preselectionEndTime = moment(submitValue.preselectionEndTime).format('YYYY-MM-DD HH:mm:ss')
        console.log(submitValue)
        request({ ...apis.house.addTaPreSelection, data: { ...submitValue },}).then((data) => {
          message.info("保存成功")
          router.push({
            pathname: '/house/preSelect/list',
          });
        }).catch((err) => {
          message.info(err.msg || err.message)
        })
      }
    });
  }

  const { getFieldDecorator } = props.form;

  return (
    <>
      <Form labelCol={{ span: 7 }} wrapperCol={{ span: 12 }} onSubmit={handleSubmit}>
        <Form.Item label="销售楼盘">
          {getFieldDecorator('buildingId', {
            rules: [{ required: true, message: ' 请输入销售楼盘' }],
          })(<BuildSelect type='false' salesBatchId={props.form.getFieldValue('salesBatchId')}/>)}
        </Form.Item>
        <Form.Item label="销售批次">
          {getFieldDecorator('salesBatchId', {
            rules: [{ required: true, message: '请输入销售批次名' }],
          })(<SalesBatch type='true' buildingId={props.form.getFieldValue('buildingId')}/>)}
        </Form.Item>
        <Form.Item label="预选开始时间">  
          {getFieldDecorator('preselectionStartTime', {
            rules: [{ required: true, message: ' 请输入预选开始时间' }],
          })(
            <DatePicker showTime={{ format: 'HH:mm:ss' }} placeholder="活动开始时间"/>,
          )}
        </Form.Item>
        <Form.Item label="预选结束时间">
          {getFieldDecorator('preselectionEndTime',{
            rules: [{ required: true, message: ' 请输入预选结束时间' }],
          })(
            <DatePicker showTime={{ format: 'HH:mm:ss' }} placeholder="活动结束时间"/>,
          )}
        </Form.Item>
        <Form.Item wrapperCol={{ span: 15, offset: 7 }}>
          <Button type="primary" htmlType="submit"style={{marginRight:'20px'}}>
            提交
          </Button>
          <Button onClick={() => router.go(-1)}>
            取消
          </Button>
        </Form.Item>
      </Form>
    </>
  )
}

const WrappedHeader = Form.create({ name: 'header' })(header);

export default WrappedHeader
