import React, { useState, useEffect } from 'react';
import { Input, Menu, Dropdown, Button, Icon, message, Table, Divider, Tag, Select, Form, Alert, Tabs, Row, Col, Radio, tab } 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 AuthRes from './AuthRes';
import moment from 'moment';

const { TabPane } = Tabs;

const header = props => {
    const companyId = props.companyId
  
    const [ data, setData ] = useState({})
    const [ authData, setAuthData ] = useState({})
    const [ authRes, setAuthRes ] = useState({visable: false, data: ''})

    if(companyId){
      useEffect(() => {
        getCompanyData(companyId);
        auth(companyId);
      },[])
    
      // 查询列表
    const getCompanyData = (companyId) => {
      request({ ...apis.company.companyById, urlData: { id: companyId } }).then((data) => {
        setData(data)
      }).catch((err) => {
        message.error(err.msg || err.message)
      })
     }
    }

    const auth = (companyId) => {
      request({ ...apis.company.getAuthStatus, urlData: { id: companyId } }).then((data) => {
        setAuthData(data)
      }).catch((err) => {
        message.error(err.msg || err.message)
      })
    }

     //打开新页面
   const openIndexImg = (e) => {
    const newWin=window.open('about:blank');
    newWin.location.href = e
  }
  
    const openFddUrl = (data) => {
      window.open(data)
    }
  
    function back(){
      router.go(-1)
    }

    function applyAuth(){
      request({ ...apis.company.autoAuthSeal, urlData: { id: companyId } }).then((data) => {
        console.log(data, 'datatatata')
        openFddUrl(data)
        
      }).catch((err) => {
        message.error(err.msg || err.message)
      })
    }

    function queryAuth(e){
      setAuthRes({visable:e.visable, data: authData})
    }

    function closeAuth(e){
      setAuthRes({visable:e.visable, data: ''})
    }
    return (
      <>
        <Form labelCol={{ span: 7 }} wrapperCol={{ span: 12 }}>
        <Form.Item label="企业名称">
          <span>{data.companyName}</span>
        </Form.Item>
        <Form.Item label="流水号/交易号">
          <span>{data.authorizeTransactionId}</span>
        </Form.Item>
        <Form.Item label="授权签署地址">
        <span style={{wordWrap: 'break-word'}}><a onClick={(e) => openIndexImg(data.authorizeAddress)} style={{ color: 'blue' }}> {data.authorizeAddress}</a></span>
        </Form.Item>
        <Form.Item label="合同编号">
        <span>{data.contractId}</span>
        </Form.Item>
        <Form.Item label="签章结果">
        <span>{data.authorizeStatus === '3000' ? '签章成功': data.authorizeStatus === '3001'? '签章失败' :''}</span>
        </Form.Item>
        <Form.Item label="签章结果描述">
        <span>{data.authorizeDescription}</span>
        </Form.Item>
        <Form.Item label="下载地址">
        <span style={{wordWrap: 'break-word'}}>{data.authorizeDownloadUrl}</span>
        </Form.Item>
        <Form.Item label="查看地址">
        <span style={{wordWrap: 'break-word'}}>{data.authorizeViewUrl}</span>
        </Form.Item>
        {data.authorizeStatus === '3000' &&
        <Form.Item label="授权状态">
        <span>{data.authorizeStatus === '3000'? '已授权':'未授权'}</span>
        </Form.Item>}
        {data.authorizeStatus === '3000' &&
        <Form.Item label="授权类型">
        <span>{data.authorizeType === 'onLine' ? '接口线上授权':'线下管理员授权'}</span>
        </Form.Item>}
        {data.authorizeStatus === '3000' &&
        <Form.Item label="授权流水号">
        <span>{data.authorizeTransactionId}</span>
        </Form.Item>}
        {data.authorizeStatus === '3000' &&
        <Form.Item label="授权合同编号">
        <span>{data.contractId}</span>
        </Form.Item>}
      </Form>
      <div style={{marginTop:'20px', marginBottom:'20px',textAlign:'center'}}>
        {data.authorizeStatus != '3000' && <Button style={{margin:'20px 10px'}} onClick={() => applyAuth()} >申请授权</Button>}
        <Button style={{margin:'20px 10px'}} onClick={(e) => queryAuth({visable:true})}>查看授权</Button>
        <Button style={{margin:'20px 10px'}} onClick={() => back()} >返回</Button>
      </div>
      <AuthRes visable={authRes.visable} authInfo={authRes.data} onCancel={() => closeAuth({visable:false})}></AuthRes>
      </>
    )
  }
  
  const WrappedNormalLoginForm = Form.create({ name: 'header' })(header);
  export default WrappedNormalLoginForm