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 mBchreg 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 BchregScreen extends Page {
    @observable
    formBlur = false

    constructor(props) {
        super(props)
        this.model = new mBchreg()
        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('bchreg/init',{})
       if(rtnmsg.retcod == SUCCESS)
       {
           const { data } = rtnmsg
           Object.assign(this.model, data)

       }
    //快照模式下，不必开启校验，否则，静默校验，和记录变动
   !this.loadDisplay() && this.model.openMonitor() //开启校验	
	}
	
    recp_bchtmplstHeader=[
    	{
    		title:<Label itext="联行行号" />, 
    		dataIndex:"rmbrpt",
    		key:"rmbrpt",
    		width:100,
    		sorter: (r1,r2)=>ColSorter(r1.rmbrpt,r2.rmbrpt),
    	},
    	{
    		title:<Label itext="机构名称" />, 
    		dataIndex:"namcn",
    		key:"namcn",
    		width:100,
    		sorter: (r1,r2)=>ColSorter(r1.namcn,r2.namcn),
    	},
    	{
    		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.bchsta.find(item=>item.value==val)
    			if(em)
    				text= em.label
    			return text
    		}
    	},
    	{
    		title:<Label itext="创建时间" />, 
    		dataIndex:"credat",
    		key:"credat",
    		width:100,
    		sorter: (r1,r2)=>ColSorter(r1.credat,r2.credat),
    	},
        {
            title: '操作',
            dataIndex: "op",
            key: "op",
            width: 120,
            render: (val, row, index) => {
                return (
                    <div>
                        {/* {row.relflg !== 'R' ?
                            <Link to={{ pathname: 'bhgame', query: { bchtmpinr: row.inr } }}>重新注册</Link> :
                            <Tooltip title='复核过的不可修改'>
                                <span style={{ cursor: 'pointer', color: '#999' }}>重新注册</span>
                            </Tooltip>
                        }
                        <Divider type="vertical" />
                        {row.relflg=='W'?
                        <Link to={{ pathname: 'bhgrel', query: { bchtmpinr: row.inr } }}>复核</Link>:
                        <Tooltip title='该数据非待复核状态'>
                        <span style={{ cursor: 'pointer', color: '#999' }}>复核</span>
                    </Tooltip>
                }

                        <Divider type="vertical" />
                        {row.relflg!=='R'?
                       (<a href='javascript:;' onClick={() => { this.onRegdel( row.inr) }} >删除</a>):
                        <Tooltip title='已复核，不允许删除'>
                        <span style={{ cursor: 'pointer', color: '#999' }}>删除</span>
                    </Tooltip>
                }
                 <Divider type="vertical" />
                     <Link to={{ pathname: 'bhgdel', query: { bchtmpinr: row.inr } }}>机构注销</Link> */}
                          {row.sta == '4' && this.model.usr_usg == 'pbcopr' ?
                            <span> <Link to={{ pathname: 'bhgame', query: { bchtmpinr: row.inr } }}>重新注册</Link>
                                <Divider type="vertical" /> </span> : null
                        }
                        {(row.sta == '3' || row.sta == '5') && this.model.usr_usg == 'pbcrel' ?
                            <span>   <Link to={{ pathname: 'bhgrel', query: { bchtmpinr: row.inr} }}>复核</Link>
                                <Divider type="vertical" /></span> : null
                        }
                        {row.sta == '0' && this.model.usr_usg == 'pbcopr' ?
                            <span>    <Link to={{ pathname: 'bhgdel', query: { bchtmpinr: row.inr} }}>用户注销</Link>
                                <Divider type="vertical" /></span> : null
                        }

                  {row.sta == '4' && this.model.usr_usg == 'pbcopr' ?
                              <a href='javascript:;' onClick={() => { this.onRegdel( row.inr) }} >删除</a>
                                : null
                        }
               
                    </div>
                )
            }
        },
    ]

    link = (path) => {
        this.props.history.push({ pathname: path,  query: { bchtmpinr: row.inr }  });

    }
    onRegdel= ( bchtmpinr)=>{
        this.formBlur=true  //已触发表单提交处理
        Validator.validate(this.model,this.model.descriptor,async(errors,values)=>{
                if(errors)
                    return 
                const rtnmsg = await Api.post('bchreg/regdel',{data:this.model, params:{bchtmpinr}})
                if(rtnmsg.retcod == SUCCESS)
                {
                    Notification.success({message:'删除成功!'})
                    this.onSel();
                    
                }
                else
                {
                	Notification.error({message:'服务端请求失败!'})
                }
                //余下逻辑处理
                    
        })
    }
    onAdd = () => {
        this.props.history.push('bhgadd')
    }
    onSel = () => {
        this.formBlur = true  //已触发表单提交处理
        Validator.validate(this.model, this.model.descriptor, async (errors, values) => {
            if (errors)
                return
            const rtnmsg = await Api.post('bchreg//regsel', { data: this.model })
            if (rtnmsg.retcod == SUCCESS) {
                //Notification.success({message:'数据提交成功!'})
                this.model.recp_bchtmplst = rtnmsg.data['recp_bchtmplst'];
                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("namcn")}  maxLength={60}  desp="机构名称" path="/namcn" />
                                    </Col>
                                </Row>
                            </Col>
                            <Col span={12}>
                                <Row className='row'>
                                    <Col span={5} offset={2}>
                                        <Label itext="联行行号" />
                                    </Col>
                                    <Col span={14}>
                                    <InputItem InputProps={bind("rmbrpt")}  maxLength={30}  desp="联行行号" path="/rmbrpt" />
                                    </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}>
                                    <DatePickerItem InputProps={bind("strdat")}  desp="开始日期" path="/strdat" />
                                    </Col>
                                </Row>
                            </Col>
                            <Col span={12}>
                                <Row className='row'>
                                    <Col span={5} offset={2}>
                                        <Label itext="复核状态" />
                                    </Col>
                                    <Col span={14}>
                                    <SelectItem InputProps={bind("bchsta")}  code={CodeTable.bchsta1}  desp="复核状态" path="/bchsta"/>
                                    </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}>
                                    <DatePickerItem InputProps={bind("enddat")}  desp="结束日期" path="/enddat" />
                                    </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 className='button2'>
                        <Button type="primary" onClick={this.onAdd}>
                        <Label   itext="机构注册"/>
                        </Button>
                    </div>
                </div>

                <Row >
                    <Col span={23} style={{marginLeft:40}}>
                        <Table pagination={{ pageSize: 10, hideOnSinglePage: true }} columns={this.recp_bchtmplstHeader} dataSource={this.model.recp_bchtmplst}   path="/recp/bchtmplst"/>
                    </Col>
                </Row>
              
            </div >
			  </Card>
        )
    }
}
