import React, { useEffect, useState, useRef } from 'react'
import router from 'umi/router'
import { fetch, apis } from '@/utils/request'
import { Button, Form, Input, DatePicker, notification, Modal } from 'antd'
import AddOn from './components/AddOn'

const formItemLayout = {
  labelCol: {
    xs: { span: 24 },
    sm: { span: 6 },
  },
  wrapperCol: {
    xs: { span: 24 },
    sm: { span: 12 },
  },
}

const tailFormItemLayout = {
  wrapperCol: {
    xs: {
      span: 24,
      offset: 0,
    },
    sm: {
      span: 16,
      offset: 6,
    },
  },
}

const billUploadExcelAdd = fetch(apis.bill.billUploadExcelAdd)
const billInvoiceGetInvoiceInvalid = fetch(apis.bill.billInvoiceGetInvoiceInvalid)

export default Form.create()(props => {
  const [queryParams, setQueryParams] = useState({ pageNum: 1, pageSize: 10 })
  const [formData, setFormData] = useState()
  const [addOnData, setAddOnData] = useState()
  const file = useRef()
  
  const { id } = props.location.query
  const { getFieldDecorator, setFieldsValue } = props.form

  const handleSubmit = billStatus => {
    props.form.validateFields((err, values) => {
      if (!err) {
        if (!file.current) {
          notification.warn({
            message: '请先上传账单文件'
          })
          return
        }

        const data = new FormData()
        data.append('file', file.current)
        data.append('billExplain', values.billExplain)
        data.append('billName', values.billName)
        data.append('endDate', values.endDate)
        data.append('billStatus', billStatus)

        if (id) {
          data.append('billId', id)
        }

        billUploadExcelAdd({ data }).then(res => {
          Modal.success({
            content: '生成数据成功',
            onOk: () => router.go(-1)
          })
        })
      }
    })
  }

  useEffect(() => {
    if (id) {
      billInvoiceGetInvoiceInvalid({
        data: {
          ...queryParams,
          billId: id,
        }
      }).then(res => {
        setFormData(res.bill)
        setAddOnData(res)
      })
    }
  }, [id, queryParams])

  useEffect(() => {
    setFieldsValue(formData || {})
  }, [formData])

  return (
    <div>
      <Form {...formItemLayout} onSubmit={e => e.preventDefault()}>
        <Form.Item label="收费组名称">
        {
          getFieldDecorator('billName', {
            rules: [
              { required: true, message: '请填写收费组名称', },
            ],
          })(<Input />)
        }
        </Form.Item>
        <Form.Item label="收费组说明">
        {
          getFieldDecorator('billExplain', {
            rules: [
              { required: true, message: '请填写收费组说明', },
            ],
          })(<Input />)
        }
        </Form.Item>
        <Form.Item label="截止时间">
        {
          getFieldDecorator('endDate', {
            rules: [
              { required: true, message: '请填写公告内容', },
            ],
          })(<DatePicker />)
        }
        </Form.Item>
        <Form.Item label=" " colon={false}>
          <AddOn dataSource={addOnData} onFileInput={f => file.current = f}/>
        </Form.Item>
        <Form.Item {...tailFormItemLayout} >
          <Button type="primary" onClick={() => handleSubmit(2)}>草稿</Button>
          <Button type="danger" style={{ marginLeft: '24px' }} onClick={() => handleSubmit(0)}>发布</Button>
          <Button style={{ marginLeft: '24px' }} onClick={() => router.go(-1)}>取消</Button>
        </Form.Item>
      </Form>
    </div>
  )
})
