import React, { Component, Fragment } from 'react'
import {
	Page, Validator, Notification, FormItem, Table,
	InputItem, Input, Bind, SelectItem, Button, Label
} from '@/components/Common-Library'
import Api from '@/service/api'
import mDbibcl 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, Divider, Checkbox } from 'antd'

import { TableExpansion } from '@/components/Business-Library'


const CodeTable = CodeTableFactory.getInstance()


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

	constructor(props) {
		super(props)
		this.model = new mDbibcl()
		// this.model.openValidate() //开启校验
		this.bind = Bind.bind(this)
	}

	componentWillUnmount() {
		// this.model.closeValite()//关闭校验
	}
	@observable
	bchlevelLst: []
	async componentWillMount() {
		console.log("init")
		if (this.props.location.query) {
			console.log(this.props.location.query)
			let selinr = this.props.location.query.inr
			const rtnmsg = await Api.post('dbibcl/init', { params: { selinr } })
			if (rtnmsg.retcod == SUCCESS) {
				Object.assign(this.model, rtnmsg.data)
				this.bchlevelLst = rtnmsg.codetable['bchkey'];

				this.bclp_bcllstHeader[3].type.add.code = rtnmsg.codetable['bchkey']
                this.bclp_bcllstHeader[3].type.edit.code = rtnmsg.codetable['bchkey']
                this.setState({})
			}
			else {
				Notification.error({ message: '服务端请求失败!' })
			}
		}
	}
	bclp_bcllstHeader = [
		// {
		// 	title: <Label value="机构" />,
		// 	dataIndex: "bchkey",
		// 	key: "bchkey",
		// 	width: 100,
		// },
		{
			title: <Label value="业务品种" />,
			dataIndex: "busrou",
			key: "busrou",
			width: 100,
			render: (val, row, index) => {
				let text = val
				let em =CodeTable.busrou&& CodeTable.busrou.find(item => item.value == val)
				if (em)
					text = em.label
				return text
			},
			type: {
				add: { type: 'select', code: CodeTable.busrou },
				edit: { type: 'select', code: CodeTable.busrou },
			}
		},
		{
			title: <Label value="经办方式" />,
			dataIndex: "exeflg",
			key: "exeflg",
			width: 100,
			render: (val, row, index) => {
				let text = val
				let em =CodeTable.exetyp&& CodeTable.exetyp.find(item => item.value == val)
				if (em)
					text = em.label
				return text
			},
			type: {
				add: { type: 'select', code: CodeTable.exetyp },
				edit: { type: 'select', code: CodeTable.exetyp },
			}
		},
		{
			title: <Label value="登记方式" />,
			dataIndex: "regflg",
			key: "regflg",
			width: 100,
			render: (val, row, index) => {
				let text = val
				let em =CodeTable.regflg&& CodeTable.regflg.find(item => item.value == val)
				if (em)
					text = em.label
				return text
			},
			type: {
				add: { type: 'select', code: CodeTable.regflg },
				edit: { type: 'select', code: CodeTable.regflg },
			}
		},
		{
			title: <Label value="上收单证中心" />,
			dataIndex: "upbchkey",
			key: "upbchkey",
			width: 160,
			type: {
				add: { type: 'select', code: [] },
				edit: { type: 'select', code: [] },
			}
		},
	]


	//按钮事件
	onSav = () => {
		this.formBlur = true  //已触发表单提交处理
		Validator.validate(this.model, this.model.descriptor, async (errors, values) => {
			if (errors)
				return
			const rtnmsg = await Api.post('dbibcl/sav', { data: this.model })
			if (rtnmsg.retcod == SUCCESS) {
				Notification.success({ message: '数据提交成功!' })

			}
			else {
				Notification.error({ message: '服务端请求失败!' })
			}
			//余下逻辑处理

		})
	}
	onRtn = () => {
		this.props.history.push("dblbcl");
		// this.formBlur = true  //已触发表单提交处理
		// Validator.validate(this.model, this.model.descriptor, async (errors, values) => {
		// 	if (errors)
		// 		return
		// 	const rtnmsg = await Api.post('dbibcl/rtn', { data: this.model })
		// 	if (rtnmsg.retcod == SUCCESS) {
		// 		Notification.success({ message: '数据提交成功!' })

		// 	}
		// 	else {
		// 		Notification.error({ message: '服务端请求失败!' })
		// 	}
		// 	//余下逻辑处理

		// })
	}
	onChk = () => {
		this.formBlur = true  //已触发表单提交处理
		Validator.validate(this.model, this.model.descriptor, async (errors, values) => {
			if (errors)
				return
			const rtnmsg = await Api.post('dbibcl/chk', { data: this.model })
			if (rtnmsg.retcod == SUCCESS) {
				Notification.success({ message: '数据提交成功!' })

			}
			else {
				Notification.error({ message: '服务端请求失败!' })
			}
			//余下逻辑处理

		})
	}


	//交易内实现
	renderPage() {
		const bind = this.bind
		const { i18n } = this.props
		const CodeTable = CodeTableFactory.getInstance()

		return (
			<Card title="自动化交易配置" size="small" className="transTitle">
				<div style={{ position: 'relative' }}>
					<Row gutter={16}>
						<Col span={8} offset={3} className='item' >
							<Col span={7} >
								<Label value={"归属机构"} />
							</Col>
							<Col span={17}>
								<SelectItem InputProps={{...bind("bchkey"),disabled:true}} code={this.bchlevelLst} />
							</Col>
						</Col>
						<Col span={8} offset={2} className='item' >
							<Col span={7}>
								<Label value={"业务品种"} />
							</Col>
							<Col span={17}>
								<SelectItem InputProps={{...bind("busrou"),disabled:true}} code={CodeTable.busrou} />
							</Col>
						</Col>
					</Row>
					<Row gutter={16}>
						<Col span={8} offset={3} className='item' >
							<Col span={7} >
								<Label value={"经办方式"} />
							</Col>
							<Col span={17}>
								<SelectItem InputProps={{...bind("exeflg"),disabled:true}} code={CodeTable.exetyp} />
							</Col>
						</Col>
						<Col span={8} offset={2} className='item' >
							<Col span={7}>
								<Label value={"登记方式"} />
							</Col>
							<Col span={17}>
								<SelectItem InputProps={{...bind("regflg"),disabled:true}} code={CodeTable.regflg} />
							</Col>
						</Col>
					</Row>
					<Row gutter={16}>
						<Col span={8} offset={3} className='item' >
							<Col span={7} >
								<Label value={"上收单证中心"} />
							</Col>
							<Col span={17}>
								<SelectItem InputProps={{...bind("upbchkey"),disabled:true}} code={this.bchlevelLst} />
							</Col>
						</Col>
					</Row>

					<Table
                        delete={true}
                        pagination={{ pageSize: 10, hideOnSinglePage: true }}
                        rowKey="inr"
                        columns={this.bclp_bcllstHeader}
                        model={this.model}
                        dataSource={this.model.bclp_bcllst}
                        onChange={(value) => { this.model.bclp_bcllst = value }}
                        path="/autgrp/audlst"
                    />
					{/* <TableExpansion
                        delete={true}
                        pagination={{ pageSize: 10, hideOnSinglePage: true }}
                        rowKey="inr"
                        columns={this.bclp_bcllstHeader}
                        model={this.model}
                        dataSource={this.model.bclp_bcllst}
                        onChange={(value) => { this.model.bclp_bcllst = value }}
                        path="/autgrp/audlst"
                    /> */}

					{/* <Table columns={this.bclp_bcllstHeader} source={this.model.bclp_bcllst} /> */}

					<div style={{ textAlign: "center" }}>
						<Button style={{ marginLeft: 10, marginRight: 10 }} type="primary" onClick={this.onRtn}>
							<Label i18nId="MTABUT_BT000459" />
						</Button>
						<Button style={{ marginLeft: 10, marginRight: 10 }} type="primary" icon="save"
							onClick={this.onSav}>
							<Label i18nId="MTABUT_BT000470" />
						</Button>
						<Button style={{ marginLeft: 10, marginRight: 10 }} type="primary" onClick={this.onChk}>
							<Label i18nId="MTABUT_BT000472" />
						</Button>
					</div>
				</div>
			</Card>
		)
	}
}
