import React,{Component,Fragment} from 'react'
import PropTypes from 'prop-types'
import {Page,Validator,Notification,FormItem,DatePickerItem,TextAreaItem,CheckboxItem,Checkbox,
    InputItem,Input,Bind,SelectItem,Button,Label,Image,Table} from '@/components/Common-Library'
import Api from '@/service/api'
import mDbiswm from '../model'
import CodeTableFactory from '@/components/CodeTable'
import "./index.less"
import { inject, observer } from 'mobx-react'
import { action, observable,runInAction } from 'mobx'


const CodeTable = CodeTableFactory.getInstance()


@inject('UserContext')
@inject('i18n')
@observer
export default class DbiswmScreen extends Page
{
    static childContextTypes = {
        trn: PropTypes.string,
    }
    getChildContext () {
        return {trn:'dbiswm'}
    }
    
    
    @observable
    formBlur=false

    constructor(props)
    {
        super(props)
        this.model = new mDbiswm()    
        this.bind = Bind.bind(this)
    }
	
	async componentWillMount() 
	{
		/*
		*添加初始化代码
		*/
		const rtnmsg = await Api.post('dbiswm/init',{data:this.model})
		if(rtnmsg.retcod == SUCCESS)
		{
           const {data} = rtnmsg
           runInAction(()=>{
                Object.assign(this.model,data)
           }) 
        }
		//快照模式下，不必开启校验，否则，静默校验，和记录变动
		!this.loadDisplay() && this.model.openMonitor && this.model.openMonitor() //开启校验	
	}
    componentWillUnmount()
    {
        this.model.stopMonitor&&this.model.stopMonitor()//关闭校验
    }

    swmp_swmlstHeader=[
    	{
    		title:<Label itext="Source" />, 
    		dataIndex:"fmt",
    		key:"fmt",
    		width:100,
    		sorter: (r1,r2)=>ColSorter(r1.fmt,r2.fmt),
    		render:(val, row, index)=>{
    			let text = val
    			let em = CodeTable.swmfmt.find(item=>item.value==val)
    			if(em)
    				text= em.label
    			return text
    		}
    	},
    	{
    		title:<Label itext="Msg Type" />, 
    		dataIndex:"mt",
    		key:"mt",
    		width:100,
    		sorter: (r1,r2)=>ColSorter(r1.mt,r2.mt),
    		render:(val, row, index)=>{
    			let text = val
    			let em = CodeTable.swmmt.find(item=>item.value==val)
    			if(em)
    				text= em.label
    			return text
    		}
    	},
    	{
    		title:<Label itext="Transaction" />, 
    		dataIndex:"frm",
    		key:"frm",
    		width:100,
    		sorter: (r1,r2)=>ColSorter(r1.frm,r2.frm),
    	},
    	{
    		title:<Label itext="Field Tag" />, 
    		dataIndex:"tag",
    		key:"tag",
    		width:100,
    		sorter: (r1,r2)=>ColSorter(r1.tag,r2.tag),
    	},
    	{
    		title:<Label itext="Sub ID" />, 
    		dataIndex:"subtag",
    		key:"subtag",
    		width:100,
    		sorter: (r1,r2)=>ColSorter(r1.subtag,r2.subtag),
    	},
    	{
    		title:<Label itext="Destination" />, 
    		dataIndex:"dst",
    		key:"dst",
    		width:100,
    		sorter: (r1,r2)=>ColSorter(r1.dst,r2.dst),
    	},
    	{
    		title:<Label itext="Argument" />, 
    		dataIndex:"ins",
    		key:"ins",
    		width:100,
    		sorter: (r1,r2)=>ColSorter(r1.ins,r2.ins),
    	},
    	{
    		title:<Label itext="Transformation" />, 
    		dataIndex:"met",
    		key:"met",
    		width:100,
    		sorter: (r1,r2)=>ColSorter(r1.met,r2.met),
    		render:(val, row, index)=>{
    			let text = val
    			let em = CodeTable.swmmet.find(item=>item.value==val)
    			if(em)
    				text= em.label
    			return text
    		}
    	},
    	{
    		title:<Label itext="Copy Flag" />, 
    		dataIndex:"cpyflg",
    		key:"cpyflg",
    		width:100,
    		sorter: (r1,r2)=>ColSorter(r1.cpyflg,r2.cpyflg),
    	},
    	{
    		title:<Label itext="Overwrite Flag" />, 
    		dataIndex:"ovwflg",
    		key:"ovwflg",
    		width:100,
    		sorter: (r1,r2)=>ColSorter(r1.ovwflg,r2.ovwflg),
    		render:(val, row, index)=>{
    			let text = val
    			let em = CodeTable.ovwflg.find(item=>item.value==val)
    			if(em)
    				text= em.label
    			return text
    		}
    	},
    	{
    		title:<Label itext="Group" />, 
    		dataIndex:"dstgrp",
    		key:"dstgrp",
    		width:100,
    		sorter: (r1,r2)=>ColSorter(r1.dstgrp,r2.dstgrp),
    	},
    ]


    //按钮事件
    onSwmp_del= ()=>{
        this.formBlur=true  //已触发表单提交处理
        Validator.validate(this.model,this.model.descriptor,async(errors,values)=>{
                if(errors)
                    return 
                const rtnmsg = await Api.post('dbiswm/swmp_del',{data:this.model})
                if(rtnmsg.retcod == SUCCESS)
                {
                    Notification.success({message:'数据提交成功!'})
                    
                }
                else
                {
                	Notification.error({message:'服务端请求失败!'})
                }
                //余下逻辑处理
                    
        })
    }
    onFrmsel_frmpop= ()=>{
        this.formBlur=true  //已触发表单提交处理
        Validator.validate(this.model,this.model.descriptor,async(errors,values)=>{
                if(errors)
                    return 
                const rtnmsg = await Api.post('dbiswm/frmsel_frmpop',{data:this.model})
                if(rtnmsg.retcod == SUCCESS)
                {
                    Notification.success({message:'数据提交成功!'})
                    
                }
                else
                {
                	Notification.error({message:'服务端请求失败!'})
                }
                //余下逻辑处理
                    
        })
    }
    onSwmp_ame= ()=>{
        this.formBlur=true  //已触发表单提交处理
        Validator.validate(this.model,this.model.descriptor,async(errors,values)=>{
                if(errors)
                    return 
                const rtnmsg = await Api.post('dbiswm/swmp_ame',{data:this.model})
                if(rtnmsg.retcod == SUCCESS)
                {
                    Notification.success({message:'数据提交成功!'})
                    
                }
                else
                {
                	Notification.error({message:'服务端请求失败!'})
                }
                //余下逻辑处理
                    
        })
    }
    onSwmp_add= ()=>{
        this.formBlur=true  //已触发表单提交处理
        Validator.validate(this.model,this.model.descriptor,async(errors,values)=>{
                if(errors)
                    return 
                const rtnmsg = await Api.post('dbiswm/swmp_add',{data:this.model})
                if(rtnmsg.retcod == SUCCESS)
                {
                    Notification.success({message:'数据提交成功!'})
                    
                }
                else
                {
                	Notification.error({message:'服务端请求失败!'})
                }
                //余下逻辑处理
                    
        })
    }
    onSwmp_sel= ()=>{
        this.formBlur=true  //已触发表单提交处理
        Validator.validate(this.model,this.model.descriptor,async(errors,values)=>{
                if(errors)
                    return 
                const rtnmsg = await Api.post('dbiswm/swmp_sel',{data:this.model})
                if(rtnmsg.retcod == SUCCESS)
                {
                    Notification.success({message:'数据提交成功!'})
                    
                }
                else
                {
                	Notification.error({message:'服务端请求失败!'})
                }
                //余下逻辑处理
                    
        })
    }
    

    //交易内实现
    renderPage(){
    	const bind = this.bind
        const {i18n} = this.props
             
        return (
        	<div className="transPage">

	
	<Label itext="Source" trn="dbiswm" />	
	<SelectItem InputProps={bind("swmp_fmt")} code={CodeTable.swmfmt}  desp="Message Format and Source SWIFT, TradeConnect, EDIFACT" path="/swmp/fmt"/>
	<Button  type="primary" icon="search" onClick={this.onSwmp_sel} desp="查询" path="/swmp/sel">
		<Label   itext="查询"/>
	</Button>

	
	<Label itext="Msg Type" trn="dbiswm" />	
	<SelectItem InputProps={bind("swmp_mt")} code={CodeTable.swmmt}  desp="Message Type" path="/swmp/mt"/>

	
	<Label itext="Transaction" trn="dbiswm" />	
	<InputItem InputProps={bind("swmp_frmcod")}  maxLength={6}  desp="Destination Transaction" path="/swmp/frmcod" />
	<Button  type="primary" icon="search" onClick={this.onFrmsel_frmpop} desp="交易代码弹出" path="/swmp/frmsel/frmpop">
		<Label   itext="选择"/>
	</Button>
	<InputItem InputProps={bind("swmp_frmdsp")}  maxLength={200}  desp="交易描述" path="/swmp/frmdsp" />
	<Table rowKey="inr" columns={this.swmp_swmlstHeader}  dataSource={this.model.swmp_swmlst}   path="/swmp/swmlst"/>
	<Button  type="primary"  onClick={this.onSwmp_add} desp="新增" path="/swmp/add">
		<Label   itext="新增"/>
	</Button>
	<Button  type="primary"  onClick={this.onSwmp_ame} desp="修改" path="/swmp/ame">
		<Label   itext="修改"/>
	</Button>
	<Button  icon="delete" onClick={this.onSwmp_del} desp="删除" path="/swmp/del">
		<Label   itext="删除"/>
	</Button>
		</div>
		)
	}
}
