import React,{Component} from 'react'
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import {Input,Table,Steps,Row,Col,Progress,Spin} from 'antd'
import { updateTreeValue,updateRcvBic,initSwiftMessageTree } from '../store/modules/swift/swift_action';

import ServiceAPI from './ServiceAPI'

const Step = Steps.Step;

 class Result extends Component{

    constructor(props)
    {
        super(props)
        this.state={isOK:-1}
    }
    componentWillMount()
    {
        //提交报文
        let datas = {MSGTYP:this.props.extra.msgtyp.substring(2).toUpperCase(),SNDKEY:this.props.sndkey,MCTSWTID:this.props.extra.mctid}
        //取出20，21
        let tags = this.props.swift_message_tree.Tags
        if(tags.length && tags[0].seqlist)
            tags = tags[0].seqs[0]
        if(tags.length && tags[0].tag == '20')
            datas.F20=tags[0].tagValue[1]
        if(tags.length > 1 && tags[1].tag == '21')
            datas.F21=tags[1].tagValue[1]
        ServiceAPI.saveMessage(
            // {
                
            //     smhinr:this.props.extra.smhinr,
            //     sf2msg:this.props.swift_message,
            //     datas:{
            //         msgtyp:this.props.extra.msgtyp,
            //         sndkey:this.props.rcv_bic
            //     }
            // }
            `smhinr=${this.props.extra.smhinr?this.props.extra.smhinr:''}&user=${this.props.extra.mctid||"NONE" }&sf2msg=${encodeURIComponent(this.props.swift_message)}`
            ).then(data=>{
                if(typeof data=='string')
                {
                    data = JSON.parse(data)
                }
                if(data.errorCode == '0000')
                    this.setState({isOK:0}) 
                else
                    this.setState({isOK:0,error:"保存失败！"}) 
        }).catch(reason=>{
            console.log(reason)
            this.setState({isOK:0,error:'保存失败！'})
        });
    }
    
    render()
    {

        return (<div>
                    <Row style={{marginTop:'3em'}}>
                        <Col span={3}/>
                        <Col span={18}>
                            {
                             this.props.params.kind=='0'? 
                                <Steps current={2}>
                                    <Step title="报文编辑" description="编辑报文栏位值" />
                                    <Step title="预览报文" description="预览报文" />
                                    <Step title="编辑完成" description="已完成提交" />
                                </Steps>
                                :
                                <Steps current={3}>
                                    <Step title="选择类型" description="选择报文类型" />
                                    <Step title="报文编辑" description="编辑报文栏位值" />
                                    <Step title="预览报文" description="预览报文" />
                                    <Step title="编辑完成" description="已完成提交" />
                                </Steps>
                            }
                        </Col>
                        <Col span={3}/>
                    </Row>
                    <Row>
                        <Col span={3}></Col>
                        <Col span={18} style={{textAlign:'center',paddingTop:'5em'}}>
                            {
                            this.state.isOK!==0?
                            <Spin size="large" />:
                            <Progress type="circle" percent={!this.state.error?100:70} status={this.state.error?"exception":null} format={() => !this.state.error?'Success!':'Error!'} />
                            }
                            {/*<h3 style={{display:this.state.error?'block':'none',color:'red'}}>{this.state.error}</h3>*/}
                        </Col>
                        <Col span={3}></Col>
                    </Row>
                    
            </div>
            )
    }
} 

const mapStateToProps = (state) => {
  const {swift_message,extra,snd_bic_info,swift_message_tree} = state.swift;
  return {
        swift_message,extra,sndkey:snd_bic_info.snd_bic,swift_message_tree
  };
};

function mapDispatchToProps(dispatch) {
  return {
    actions: bindActionCreators({ updateTreeValue,updateRcvBic,initSwiftMessageTree }, dispatch)
  };
}

export default connect(mapStateToProps, mapDispatchToProps)(Result)