import React from 'react';
import { Button, Modal, Table, } from 'antd';
import { observer } from 'mobx-react'
import Api from '@/service/api'
import { observable } from 'mobx'
import { Notification,Label } from '@/components/Common-Library'
import CodeTableFactory from '@/components/CodeTable'

const CodeTable = CodeTableFactory.getInstance()

@observer
export default class Cankaohao extends React.Component{
  constructor(props){
    super(props)
		this.model=props.model
		this.state={
			canClick:true
		}
	}
	
	@observable
  visible = false

  @observable
	dataSource = []
	
	@observable
	columns=[]

	@observable
	checkData=""

	componentDidMount(){
		this.props.childEvevnt && this.props.childEvevnt(this);
	}
	
	conget_didlstHeader=[
		{
			title:<Label itext="信用证编号" />, 
			dataIndex:"ownref",
			key:"ownref",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.ownref,r2.ownref),
		},
		{
			title:<Label itext="业务概要" />, 
			dataIndex:"nam",
			key:"nam",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.nam,r2.nam),
		},
		{
			title:<Label itext="开立日期" />, 
			dataIndex:"opndat",
			key:"opndat",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.opndat,r2.opndat),
		},
		{
			title:<Label itext="开立方式" />, 
			dataIndex:"isstyp",
			key:"isstyp",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.isstyp,r2.isstyp),
			render:(val, row, index)=>{
				let text = val
				let em = CodeTable.isstyp.find(item=>item.value==val)
				if(em)
					text= em.label
				return text
			}
		},
		{
			title:<Label itext="贸易类型" />, 
			dataIndex:"trdtyp",
			key:"trdtyp",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.trdtyp,r2.trdtyp),
			render:(val, row, index)=>{
				let text = val
				let em = CodeTable.trdtyp.find(item=>item.value==val)
				if(em)
					text= em.label
				return text
			}
		},
		{
			title:<Label itext="有效日期" />, 
			dataIndex:"expdat",
			key:"expdat",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.expdat,r2.expdat),
		},
		{
			title:<Label itext="兑付方式" />, 
			dataIndex:"avbby",
			key:"avbby",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.avbby,r2.avbby),
			render:(val, row, index)=>{
				let text = val
				let em = CodeTable.avbby.find(item=>item.value==val)
				if(em)
					text= em.label
				return text
			}
		},
		{
			title:<Label itext="期限" />, 
			dataIndex:"tenmaxday",
			key:"tenmaxday",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.tenmaxday,r2.tenmaxday),
		},
		{
			title:<Label itext="柜员" />, 
			dataIndex:"ownusr",
			key:"ownusr",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.ownusr,r2.ownusr),
		},
	]

	conget_dedlstHeader=[
		{
			title:<Label itext="业务编号" />, 
			dataIndex:"ownref",
			key:"ownref",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.ownref,r2.ownref),
		},
		{
			title:<Label itext="概要描述" />, 
			dataIndex:"nam",
			key:"nam",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.nam,r2.nam),
		},
		{
			title:<Label itext="开立日期" />, 
			dataIndex:"opndat",
			key:"opndat",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.opndat,r2.opndat),
		},
		{
			title:<Label itext="开证类型" />, 
			dataIndex:"isstyp",
			key:"isstyp",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.isstyp,r2.isstyp),
			render:(val, row, index)=>{
				let text = val
				let em = CodeTable.isstyp.find(item=>item.value==val)
				if(em)
					text= em.label
				return text
			}
		},
		{
			title:<Label itext="贸易方式" />, 
			dataIndex:"trdtyp",
			key:"trdtyp",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.trdtyp,r2.trdtyp),
			render:(val, row, index)=>{
				let text = val
				let em = CodeTable.tratyp.find(item=>item.value==val)
				if(em)
					text= em.label
				return text
			}
		},
		{
			title:<Label itext="到期日期" />, 
			dataIndex:"expdat",
			key:"expdat",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.expdat,r2.expdat),
		},
		{
			title:<Label itext="兑付方式" />, 
			dataIndex:"avbby",
			key:"avbby",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.avbby,r2.avbby),
			render:(val, row, index)=>{
				let text = val
				let em = CodeTable.avbby.find(item=>item.value==val)
				if(em)
					text= em.label
				return text
			}
		},
		{
			title:<Label itext="期限" />, 
			dataIndex:"tenmaxday",
			key:"tenmaxday",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.tenmaxday,r2.tenmaxday),
		},
		{
			title:<Label itext="闭卷日期" />, 
			dataIndex:"clsdat",
			key:"clsdat",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.clsdat,r2.clsdat),
		},
		{
			title:<Label itext="柜员" />, 
			dataIndex:"ownusr",
			key:"ownusr",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.ownusr,r2.ownusr),
		},
	]

	conget_bmdlstHeader=[
		{
			title:<Label itext="业务编号" />, 
			dataIndex:"ownref",
			key:"ownref",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.ownref,r2.ownref),
		},
		{
			title:<Label itext="概要描述" />, 
			dataIndex:"nam",
			key:"nam",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.nam,r2.nam),
		},
		{
			title:<Label itext="交单日期" />, 
			dataIndex:"predat",
			key:"predat",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.predat,r2.predat),
		},
		{
			title:<Label itext="装运日期" />, 
			dataIndex:"shpdat",
			key:"shpdat",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.shpdat,r2.shpdat),
		},
		{
			title:<Label itext="到期日期" />, 
			dataIndex:"matdat",
			key:"matdat",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.matdat,r2.matdat),
		},
		{
			title:<Label itext="承兑日期" />, 
			dataIndex:"accdat",
			key:"accdat",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.accdat,r2.accdat),
		},
		{
			title:<Label itext="闭卷日期" />, 
			dataIndex:"clsdat",
			key:"clsdat",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.clsdat,r2.clsdat),
		},
		{
			title:<Label itext="柜员" />, 
			dataIndex:"ownusr",
			key:"ownusr",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.ownusr,r2.ownusr),
		},
	]

	conget_bddlstHeader=[
		{
			title:<Label itext="业务编号" />, 
			dataIndex:"ownref",
			key:"ownref",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.ownref,r2.ownref),
		},
		{
			title:<Label itext="概要描述" />, 
			dataIndex:"nam",
			key:"nam",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.nam,r2.nam),
		},
		{
			title:<Label itext="到单日期" />, 
			dataIndex:"opndat",
			key:"opndat",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.opndat,r2.opndat),
		},
		{
			title:<Label itext="到期日期" />, 
			dataIndex:"matdat",
			key:"matdat",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.matdat,r2.matdat),
		},
		{
			title:<Label itext="期限" />, 
			dataIndex:"tenmaxday",
			key:"tenmaxday",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.tenmaxday,r2.tenmaxday),
		},
		{
			title:<Label itext="承兑日期" />, 
			dataIndex:"accdat",
			key:"accdat",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.accdat,r2.accdat),
		},
		{
			title:<Label itext="闭卷日期" />, 
			dataIndex:"clsdat",
			key:"clsdat",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.clsdat,r2.clsdat),
		},
		{
			title:<Label itext="柜员" />, 
			dataIndex:"ownusr",
			key:"ownusr",
			width:100,
			sorter: (r1,r2)=>ColSorter(r1.ownusr,r2.ownusr),
		},
	]

	conget_cpdlstHeader=[]

	getDatasource(val){
		this.dataSource=val
	}

	async onConget_search(){
		if(!this.state.canClick){
			return
		}
		this.setState({canClick:false})

		//每次请求之前就清空上一次的数据
		this.dataSource=[]

		const rtnmsg = await Api.post('sptrim/conget_search',{
			params:{
				sector:this.model.conget_sector,
				frm:this.model.spt_frm,
				selref:this.model.conget_selref
			},
		})
		if(rtnmsg.retcod == SUCCESS)
		{
			this.model.conget_lnkobj=rtnmsg.data.conget_lnkobj
			switch(this.model.conget_lnkobj){
				case "di":
					this.columns=this.conget_didlstHeader
					rtnmsg.data.conget_didlst && this.getDatasource(rtnmsg.data.conget_didlst)
					break
				case "bd":
					this.columns=this.conget_bddlstHeader
					rtnmsg.data.conget_bddlst && this.getDatasource(rtnmsg.data.conget_bddlst)
					break
				case "de":
					this.columns=this.conget_dedlstHeader
					rtnmsg.data.conget_dedlst && this.getDatasource(rtnmsg.data.conget_dedlst)
					break
				case "bm":
					this.columns=this.conget_bmdlstHeader
					rtnmsg.data.conget_bmdlst && this.getDatasource(rtnmsg.data.conget_bmdlst)
					break
				case "cp":
					this.columns=this.conget_cpdlstHeader
					rtnmsg.data.conget_cpdlst && this.getDatasource(rtnmsg.data.conget_cpdlst)
					break
			}
			
			// 如果this.dataSource没有数据就不展示子窗口
			if(this.dataSource.length>0){
				this.visible=true;
			}else{
				Notification.error({message:'未查询到相关数据!'})
			}
		}
		else
		{
			Notification.error({message:'服务端请求失败!'})
		}
		
		setTimeout(()=>{
			this.setState({canClick:true})
		},500)
	}

	
	handleCancel() {
		this.visible = false;
	}

	async onSelect(row) {
		const rtnmsg = await Api.post("sptrim/conget_choose", { 
			params:{
				inr:row.inr,
				lnkobj:this.model.conget_lnkobj
			}
		})
		rtnmsg.data && Object.assign(this.model, rtnmsg.data);
		this.checkData=rtnmsg.data.conget_selref
		this.props.childEvevnt && this.props.childEvevnt(this);
		this.visible = false
  }
  
  rowClass = (row,index)=>{
    if(index %2)
    {
      return "tableEvenStyle"
    }
    return "tableOddStyle"
  }

  render(){
		const pagination={
			pageSize:5,
			hideOnSinglePage:true
		}

    return (
      <div style={{ width: 1200 }}>
        <Button type="primary" size="small" icon="search" {...this.props} onClick={() => this.onConget_search()} />
        <Modal
          visible={this.visible}
          onCancel={() => this.handleCancel()}
          footer={<Button onClick={() => this.handleCancel()}>取消</Button>}
					width={1200}
					destroyOnClose={true}
        >
					<Table rowKey='inr'
						onRow={ record=>({onDoubleClick:()=>this.onSelect(record)}) }
						columns={this.columns}
						dataSource={this.dataSource}
						pagination={ pagination }
            rowClassName={this.rowClass}
					/>
        </Modal>
      </div>
    )
  }

}