import React, { Component, Fragment } from 'react'
import {
    Page, Validator, Notification, FormItem,
    InputItem, Input, Bind, SelectItem, Button, Label, Table, Section, DatePickerItem
} from '@/components/Common-Library'
import { Divider } from 'antd'
import Api from '@/service/api'
import mDblbch from '../model'
import CodeTableFactory from '@/components/CodeTable'
import "./index.less"
import { inject, observer } from 'mobx-react'
import { action, observable } from 'mobx'
import { Col, Row ,Card,Tooltip } from 'antd'
import { Link } from 'react-router-dom'
import Moment from 'moment'
const CodeTable = CodeTableFactory.getInstance()

@inject('UserContext')
@inject('i18n')
@observer
export default class DblbchScreen extends Page {
    @observable
	formBlur = false
	
	@observable
	upbranch:[]

	@observable
	accbch:[]

    constructor(props) {
        super(props)
        this.model = new mDblbch()
        this.bind = Bind.bind(this)
	}
	
	

    componentWillUnmount() {
      this.model.stopMonitor&&this.model.stopMonitor()//关闭校验
    }
    
	async componentWillMount() {
		 if (this.props.location.state) {
             let selflg = this.props.location.state.selflg
		     if (selflg)
                this.onSel()   
	 }	
	
     const rtnmsg = await Api.post('dblbch/init',{})
		if(rtnmsg.retcod == SUCCESS)
		{
            const { data } = rtnmsg
			Object.assign(this.model, data)
			this.upbranch = rtnmsg.codetable['\\bchgrp\\rec\\upbranch'];
			this.accbch = rtnmsg.codetable['\\bchgrp\\rec\\accbch'];

        }
     //快照模式下，不必开启校验，否则，静默校验，和记录变动
    !this.loadDisplay() && this.model.openMonitor() //开启校验	
	}
	
	bchp_bchlstHeader=[
    	{
    		title:<Label itext="机构号" />, 
    		dataIndex:"branch",
    		key:"branch",
    		width:80,
    		sorter: (r1,r2)=>ColSorter(r1.branch,r2.branch),
    	},
    	{
    		title:<Label itext="机构名称" />, 
    		dataIndex:"namcn",
    		key:"namcn",
    		width:200,
    		sorter: (r1,r2)=>ColSorter(r1.namcn,r2.namcn),
    	},
    	{
    		title:<Label itext="机构级别" />, 
    		dataIndex:"lev",
    		key:"lev",
    		width:80,
    		sorter: (r1,r2)=>ColSorter(r1.lev,r2.lev),
    		render:(val, row, index)=>{
    			let text = val
    			let em = CodeTable.bchlev.find(item=>item.value==val)
    			if(em)
    				text= em.label
    			return text
    		}
    	},
    	{
    		title:<Label itext="上级机构" />, 
    		dataIndex:"upbranch",
    		key:"upbranch",
    		width:80,
    		sorter: (r1,r2)=>ColSorter(r1.upbranch,r2.upbranch),
    	},
    	{
    		title:<Label itext="人行支付行号" />, 
    		dataIndex:"rmbrpt",
    		key:"rmbrpt",
    		width:140,
    		sorter: (r1,r2)=>ColSorter(r1.rmbrpt,r2.rmbrpt),
    	},
    	{
    		title:<Label itext="金融机构标识码" />, 
    		dataIndex:"decnum",
    		key:"decnum",
    		width:150,
    		sorter: (r1,r2)=>ColSorter(r1.decnum,r2.decnum),
    	},
    	{
    		title:<Label itext="机构状态" />, 
    		dataIndex:"sta",
    		key:"sta",
    		width:100,
    		sorter: (r1,r2)=>ColSorter(r1.sta,r2.sta),
    		render:(val, row, index)=>{
    			let text = val
    			let em = CodeTable.bchrelsta.find(item=>item.value==val)
    			if(em)
    				text= em.label
    			return text
    		}
    	},
        {
            title: '操作',
            dataIndex: "op",
            key: "op",
            width: 120,
            render: (val, row, index) => {
                return (
                  <div>
                         {(row.sta == '2'||row.sta == '0' )&& this.model.usr_usg == 'bnkadm' ?
                            <span> <Link to={{ pathname: 'dbebch', query: { bchinr: row.inr,upbranch:this.upbranch,accbch:this.accbch} }} >修改</Link>
                                <Divider type="vertical" /> </span>:null
            }
            {row.sta == '7' && this.model.usr_usg == 'bnkbmg' ?
								<span> <Link to={{ pathname: 'bchrel', query: { bchinr: row.inr } }}>复核</Link>
                                <Divider type="vertical" /> </span>:null
            }
                     

               
                    </div>
                )
            }
        },
    ]

 
   
    onSel = () => {
        this.formBlur = true  //已触发表单提交处理
        Validator.validate(this.model, this.model.descriptor, async (errors, values) => {
            if (errors)
                return
            const rtnmsg = await Api.post('dblbch/sel', { data: this.model })
            if (rtnmsg.retcod == SUCCESS) {
                //Notification.success({message:'数据提交成功!'})
                this.model.bchp_bchlst = rtnmsg.data['bchp_bchlst'];
                console.log(this.model)
            }
            else {
                Notification.error({ message: '查询失败!' })
            }
            //余下逻辑处理

        })
    }


    //交易内实现
    renderPage() {
        const bind = this.bind
        const { i18n } = this.props

        return (
		 <Card title="机构维护" size="small" className="transTitle">
            <div style={{ position: 'relative' }}>
           
                <Row>
                    <Col span={20}>

                        <Row>
                            <Col span={12}>
                                <Row className='row'>
                                    <Col span={5} offset={2}>
                                    <Label   itext="机构编码"/>
                                    </Col>
                                    <Col span={14}>
									<InputItem InputProps={bind("bchp_branch")}  maxLength={8}  desp="机构编码" path="/bchp/branch" />
                                    </Col>
                                </Row>
                            </Col>
                            <Col span={12}>
                                <Row className='row'>
                                    <Col span={5} offset={2}>
                                        <Label itext="中文名称" />
                                    </Col>
                                    <Col span={14}>
									<InputItem InputProps={bind("bchp_namcn")}  maxLength={8}  desp="中文名称" path="/bchp/namcn" />
                                    </Col>
                                </Row>
                            </Col>
                        </Row>


                    </Col>
                </Row>

                <Row>
                    <Col span={20}>

                        <Row>
                            <Col span={12}>
                                <Row className='row'>
                                    <Col span={5} offset={2}>
                                    <Label   itext="机构状态"/>
                                    </Col>
                                    <Col span={14}>
									<SelectItem InputProps={bind("bchp_bchsta")} code={CodeTable.bchrelsta}  desp="机构状态" path="/bchp/bchsta"/>
                                    </Col>
                                </Row>
                            </Col>
                         
                        </Row>


                    </Col>
                </Row>



                <div className='button0'>
                    <div className='button2'>
                        <Button type="primary" icon="search" onClick={this.onSel}>
                            <Label i18nId="MTABUT_BT000022" />
                        </Button>
                    </div>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          
                </div>

                <Row >
                    <Col span={23} style={{marginLeft:40}}>
					<Table rowKey="inr" columns={this.bchp_bchlstHeader}  dataSource={this.model.bchp_bchlst}   path="/bchp/bchlst"/>
                    </Col>
                </Row>
              
            </div >
			  </Card>
        )
    }
}
