import React,{Component,Fragment} from 'react'
import {Page,Validator,Notification,FormItem,DatePickerItem,
    InputItem,Input,Bind,SelectItem,Button,Label,Image,Table} from '@/components/Common-Library'
import Api,{BASE_URI} from '@/service/api'
import mPtybat from '../model'
import CodeTableFactory from '@/components/CodeTable'
import "./index.less"
import { inject, observer } from 'mobx-react'
import { action, observable,runInAction } from 'mobx'
import { Col, Row ,Card,Divider,Upload,Icon} from 'antd'


const CodeTable = CodeTableFactory.getInstance()


@inject('UserContext')
@inject('i18n')
@observer
export default class PtybatScreen extends Page
{
    @observable
	formBlur=false
	
	@observable
	fileLoading=false

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

    componentWillUnmount()
    {
        this.model.closeValidate && this.model.closeValidate()//关闭校验
    }

	ptylstHeader=[
    	{
    		title:<Label itext="客户编号" />, 
    		dataIndex:"extkey",
    		key:"extkey",
    		width:50,
    		sorter: (r1,r2)=>ColSorter(r1.extkey,r2.extkey),
    	},
    	{
    		title:<Label itext="中文名称" />, 
    		dataIndex:"namcn",
    		key:"namcn",
    		width:50,
    		sorter: (r1,r2)=>ColSorter(r1.namcn,r2.namcn),
    	},
    	{
    		title:<Label itext="中文地址" />, 
    		dataIndex:"adrcn",
    		key:"adrcn",
    		width:50,
    		sorter: (r1,r2)=>ColSorter(r1.adrcn,r2.adrcn),
    	},
    	{
    		title:<Label itext="客户经理" />, 
    		dataIndex:"ownusr",
    		key:"ownusr",
    		width:50,
    		sorter: (r1,r2)=>ColSorter(r1.ownusr,r2.ownusr),
    	},
   
    	{
    		title:<Label itext="证件号码" />, 
    		dataIndex:"idcod",
    		key:"idcod",
    		width:50,
    		sorter: (r1,r2)=>ColSorter(r1.idcod,r2.idcod),
    	},
    	{
    		title:<Label itext="统一社会信用证代码" />, 
    		dataIndex:"juscod",
    		key:"juscod",
    		width:50,
    		sorter: (r1,r2)=>ColSorter(r1.juscod,r2.juscod),
    	},
    
    ]

    //按钮事件
    onUpload= ()=>{
        this.formBlur=true  //已触发表单提交处理
        Validator.validate(this.model,this.model.descriptor,async(errors,values)=>{
                if(errors)
                    return 
                const rtnmsg = await Api.post('ptybat/upload',{data:this.model})
                if(rtnmsg.retcod == SUCCESS)
                {
                    Notification.success({message:'数据提交成功!'})
                    
                }
                else
                {
                	Notification.error({message:'服务端请求失败!'})
                }
                //余下逻辑处理
                    
        })
    }
    onImp= ()=>{
        this.formBlur=true  //已触发表单提交处理
        Validator.validate(this.model,this.model.descriptor,async(errors,values)=>{
                if(errors)
                    return 
                const rtnmsg = await Api.post('ptybat/imp',{data:this.model})
                if(rtnmsg.retcod == SUCCESS)
                {
					Notification.success({message:'数据提交成功!'})
					this.props.history.go(-1);
                    
                }
                else
                {
                	Notification.error({message:'服务端请求失败!'})
                }
                //余下逻辑处理
                    
        })
    }
	
	onFileChange=({file,fileList,event})=>{
		if(file.status=='done')//上传成功
		{
			const {error,codetable,data,retcod} = file.response
			if(retcod == SUCCESS)
			{
				//处理成功
				runInAction(()=>{
					Object.assign(this.model,data)
				})
			}
			this.fileLoading=false
		}
		else if(file.status=='uploading') //上传中
		{
			this.fileLoading=true
		}
		else{
			this.fileLoading=false
		}
		console.log("文件上传状态："+file.status)
		console.log(file)
		console.log(event)
		console.log(fileList)
	}
	onReturnData=()=>(
		{data:
			JSON.stringify({
			data:this.model
			})
		}
	)

    //交易内实现
    renderPage(){
    	const bind = this.bind
        const {i18n} = this.props
             
        return (
		     	<Card title="批量用户新增" size="small" className="transTitle">
        	 <div style={{position:'relative'}}>
			 
			     <div className='button0'>
                    <div className='button2'>
					<Button  type="primary"  onClick={this.onImp} desp="导入用户数据" path="/imp">
		                <Label value="导入用户数据"  desp="导入用户数据"/>
	                </Button>
                    </div>
					   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					   <div className='button2'>
						   <Upload action={BASE_URI+"ptybat/upload"} headers={{
							   	token:this.props.UserContext.token,
								userId:  this.props.UserContext.userId,
								terminalType:'WEB'
							   }}
							   accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.openxmlformats-officedocument.wordprocessingml.document"
							   onChange={this.onFileChange}
							   name="file"
							   data={this.onReturnData}
							   >
								   <Button loading={this.fileLoading}>
										{
										this.fileLoading?null:
										<Icon type="upload" />
										 } 选择文件上传
										</Button>
							   </Upload>
                    </div>
                </div>
                <Row >
                    <Col span={24} >
					       <Table rowKey="inr" columns={this.ptylstHeader}  dataSource={this.model.ptylst}   path="/ptylst"/>
					</Col>
				</Row>	
		      </div>
			</Card>
		)
	}
}
