import React,{Component} from 'react'
import {Button,Table,Modal,Row,Col,Form,Input,Icon} from 'antd'

import ServiceAPI from '../ServiceAPI'

const FormItem = Form.Item;

const formItemLayout = {
      labelCol: { span: 6 },
      wrapperCol: { span: 14 },
    };
export default class YBIC extends Component
{
    
    constructor(props)
    {
        super(props)
        this.state = {list:[],name:'',bic:'',visible:false,loading:false}
    }
    
    
    cols = [
        {
            title:'选择',
            dataIndex:'action',
            key:'action',
            render:(text, record)=>{
                return <a href="javascript:void(0)" title="选择" onClick={()=>{this.props.onSelect && this.props.onSelect({bic:record.BIC,name:record.NAM1});this.setState({visible:false})}}><Icon type="search" /></a>
            }
        },
        {
            title: 'BIC Code',
            dataIndex: 'BIC',
            key: 'BIC'
        },
        {
            title: 'Name',
            dataIndex: 'NAM1',
            key: 'NAM1'
        }
        // ,
        // {
        //     title: 'Address',
        //     dataIndex: 'adr',
        //     key: 'adr'
        // }
    ]
    
    modalQuery=()=>{
        this.setState({loading:true})
        if(!this.props.pty){
            ServiceAPI.getBIC(this.state.bic,this.state.name).then(data=>{
                console.log(data)
                    if(typeof data=='string')
                    {
                        data = JSON.parse(data)
                    }
                    if(data.errorCode !== '0000')
                        return 
                    data = data.data
                    if(typeof data=='string')
                    {
                        data = JSON.parse(data)
                    }
                    console.log(data)
                    this.setState({list:data,loading:false})
                    
            }).catch(e=>{
                console.log(e)
                this.setState({loading:false})

            })
        }
        else{

            ServiceAPI.getBIC_JZ(this.state.bic,this.state.name).then(data=>{
                console.log(data)
                    if(typeof data=='string')
                    {
                        data = JSON.parse(data)
                    }
                    if(data.errorCode !== '0000')
                        return 
                    data = data.data
                    if(typeof data=='string')
                    {
                        data = JSON.parse(data)
                    }
                    console.log(data)
                    this.setState({list:data,loading:false})
                    
            }).catch(e=>{
                console.log(e)
                this.setState({loading:false})

            })
        }
    }
    
    
    
    render()
    {
        return (
            <div>
                <Button size="small"    type="primary"  shape="circle" icon="search" title="Query Bank"
                    onClick={()=>this.setState({visible:true})} /> 
                <Modal title={this.props.pty?"查询我行机构":"查询银行机构"} visible={this.state.visible} onCancel={()=>this.setState({visible:false})}
                    footer={null}
                    width={800}
                    height={700}
                    >
                    <div>
                        <Row>
                            <Col span={11}>
                                <FormItem label="BIC Code"  {...formItemLayout}>
                                    <Input  value={this.state.bic} onChange={e=>this.setState({bic:e.target.value})} maxLength={11} style={{imeMode:'disabled'}} placeholder="BIC Code"/>
                                </FormItem>
                            </Col>
                            <Col span={11}>
                                <FormItem label="Bank Name" {...formItemLayout}>
                                    <Input  value={this.state.name} onChange={e=>this.setState({name:e.target.value})} maxLength={11} style={{imeMode:'disabled'}} placeholder="Bank Name"/>
                                </FormItem>
                            </Col>
                            <Col span={2}>
                                <Button size="small"
                                        type="primary"   icon="search" title="Query BIC" onClick={this.modalQuery}/>   
                            </Col>
                        </Row>
                        <Table  columns={this.cols} size="middle"  dataSource={this.state.list} loading={this.state.loading} height={500} style={{height:'500px'}}/>
                    </div>
                </Modal>    
            
            </div>
        )

    }
}