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 mTstdem 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 TstdemScreen extends Page
{
    static childContextTypes = {
        trn: PropTypes.string,
    }
    getChildContext () {
        return {trn:'tstdem'}
    }
    
    
    @observable
    formBlur=false

    constructor(props)
    {
        super(props)
        this.model = new mTstdem()    
        this.bind = Bind.bind(this)
    }
	
	async componentWillMount() 
	{
		/*
		*添加初始化代码
		*/
		const rtnmsg = await Api.post('tstdem/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()//关闭校验
    }

    perlstHeader=[
    	{
    		title:<Label itext="序号" />, 
    		dataIndex:"inr",
    		key:"inr",
    		width:100,
    		sorter: (r1,r2)=>ColSorter(r1.inr,r2.inr),
    	},
    	{
    		title:<Label itext="姓名" />, 
    		dataIndex:"nam",
    		key:"nam",
    		width:100,
    		sorter: (r1,r2)=>ColSorter(r1.nam,r2.nam),
    	},
    	{
    		title:<Label itext="年龄" />, 
    		dataIndex:"age",
    		key:"age",
    		width:100,
    		sorter: (r1,r2)=>ColSorter(r1.age,r2.age),
    	},
    	{
    		title:<Label itext="邮箱" />, 
    		dataIndex:"email",
    		key:"email",
    		width:100,
    		sorter: (r1,r2)=>ColSorter(r1.email,r2.email),
    	},
    ]


    //按钮事件
    onAdd= ()=>{
        this.formBlur=true  //已触发表单提交处理
        Validator.validate(this.model,this.model.descriptor,async(errors,values)=>{
                if(errors)
                    return 
                const rtnmsg = await Api.post('tstdem/add',{data:this.model})
                if(rtnmsg.retcod == SUCCESS)
                {
                    Notification.success({message:'数据提交成功!'})
                    
                }
                else
                {
                	Notification.error({message:'服务端请求失败!'})
                }
                //余下逻辑处理
                    
        })
    }
    onSel= ()=>{
        this.formBlur=true  //已触发表单提交处理
        Validator.validate(this.model,this.model.descriptor,async(errors,values)=>{
                if(errors)
                    return 
                const rtnmsg = await Api.post('tstdem/sel',{data:this.model})
                if(rtnmsg.retcod == SUCCESS)
                {
                    this.model.perlst = rtnmsg.data.perlst;
                    Notification.success({message:'数据提交成功!'})
                    
                }
                else
                {
                	Notification.error({message:'服务端请求失败!'})
                }
                //余下逻辑处理
                    
        })
    }
    

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

	
	<Label itext="姓名" trn="tstdem" />	
	<InputItem InputProps={bind("per_nam")}  maxLength={16}  desp="姓名" path="/per/nam" />

	
	<Label itext="年龄" trn="tstdem" />	
	<InputItem InputProps={bind("per_age")}   desp="年龄" path="/per/age" />

	
	<Label itext="邮箱" trn="tstdem" />	
	<InputItem InputProps={bind("per_email")}  maxLength={100}  desp="邮箱" path="/per/email" />
	<Button  type="primary"  onClick={this.onAdd} desp="插入" path="/add">
		<Label   itext="插入"/>
	</Button>
	<Table rowKey="inr" columns={this.perlstHeader}  dataSource={this.model.perlst}   path="/perlst"/>
	<Button  type="primary" icon="search" onClick={this.onSel} desp="查询" path="/sel">
		<Label   itext="查询"/>
	</Button>
		</div>
		)
	}
}
