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 XForm, { FieldTypes } from '../../../components/XForm';
import router from 'umi/router';
import apis from '../../../services/apis';
import request from '../../../utils/request';
import Wangedit from '../../../components/Wangedit/Wangedit'
import moment from 'moment';


const header = props => {
    const companyId = props.location.query.id
  
    const [ data, setData ] = useState({})
    if(companyId){
      useEffect(() => {
        getCompanyData(companyId);
      },[])
    
      // 查询列表
      const getCompanyData = (companyId) => {
        request({
            ...apis.company.get,
            urlData: { id: companyId }
        }).then((data) => {
          setData(data)
        }).catch((err) => {
          message.error(err.msg || err.message)
        })
      }
    }else{
      //第一次进入就创建新的企业
      useEffect(() => {
        addCompanyData();
      },[])

      // 查询列表
      const addCompanyData = () => {
        request({
            ...apis.company.add,
        }).then((data) => {
          setData(data)
        }).catch((err) => {
          message.error(err.msg || err.message)
        })
      }
    }
     
    const handleSubmit = (e) => {
      e.preventDefault();
      props.form.validateFields((err, values) => {
        if (!err) {
          console.log(values, "-------------111")
        if(data.companyId){
            request({ ...apis.company.update, urlData: { id: data.companyId }, data: { ...values }}).then((data) => {
              console.log(data,"dadadasdadasdasdasdasda")
              message.info("提交成功，请完成认证信息！")
              window.open(data.certifiedAddress);
              cancelPage();
            }).catch((err) => {
              message.error(err.msg || err.message)
            })
          }
        }
      });
    }
  
    const cancelPage = () => {
      router.push({
        pathname: '/eContract/company/list',
      });
    }
  
    const { getFieldDecorator } = props.form;

    return (
      <>
        <Form labelCol={{ span: 7 }} wrapperCol={{ span: 12 }}>
          <Form.Item label="已自动生成企业编号">
            {getFieldDecorator('companyCode',{
              initialValue: data.companyCode
            })( <span>{data.companyCode}</span>)}
          </Form.Item>
          <Form.Item label="已生成法大大客户编号">
          {getFieldDecorator('fadadaCode',{
              initialValue: data.fadadaCode
            })( <span>{data.fadadaCode}</span>)}
          </Form.Item>
          <Form.Item label="请选择企业管理员身份" help="若企业法人可以配合认证可免于对公账户打款验证，实名认证速度快。若企业法人无法配合，请选择代理人，代理人身份需要对公打款账户验证，耗时较长。">
          {getFieldDecorator('companyManagerType', {
            rules: [ { required: true, message: '请选择企业管理员身份' }],
          })(
                <Select style={{ width: '180px' }} placeholder="请选择企业管理员身份">
                  <Select.Option value="legal">法人</Select.Option>
                  <Select.Option value="agent">代理人</Select.Option>
                </Select>)}
          </Form.Item>
          <Form.Item wrapperCol={{ span: 15, offset: 7 }}>
            <Button type="danger" onClick={handleSubmit} style={{marginRight:'20px'}} >
              下一步
            </Button>
          </Form.Item>
        </Form>
      </>
    )
  }
  
  const WrappedNormalLoginForm = Form.create({ name: 'header' })(header);
  export default WrappedNormalLoginForm