import React, { Component, Fragment } from 'react'
import {
	Page, Validator, Notification, FormItem,
	InputItem, Input, Bind, SelectItem, Button, Label, DatePickerItem, Section, Table, TextAreaItem, ConfirmButton, Steps
} from '@/components/Common-Library'
import Api from '@/service/api'
import CodeTableFactory from '@/components/CodeTable'
import "./index.less"
import { inject, observer } from 'mobx-react'
import { action, observable,autorun } from 'mobx'
import { Row, Col, Popconfirm, Modal } from 'antd';
import { PtyPicker } from '@/components/Business-Library'
import { Link, withRouter } from 'react-router-dom'


const { Pre, Next, StepWrapper } = Steps
const CodeTable = CodeTableFactory.getInstance()
const confirm = Modal.confirm

@withRouter
@inject('UserContext')
@inject('i18n')
@observer
export default class Ptspta1Screen extends Component {
	@observable
	formBlur = false

	constructor(props) {
		super(props)
		this.model = props.model
		this.bind = Bind.bind(this)
  }
  
	// 1，将字符串val按照 \n  截取为多个字符串保存到数组中
	// 2，将数组中长度超过指定长度的字符串分成两个字符串保存到数组中，将长度为0的字符串从数组中删除掉
	// 3，将数组以 \n 为连接点 重新 组装成字符串
	// 4，将得到的字符串赋值给 this.model..... 进行显示 
	hasCharOfArray = (array, char) => {
		for (let i = 0; i < array.length; i++) {
			if (char == array[i]) {
				return true;
			}
		}
		return false;
	}
	huan = (val) => {
		const allowChar = ['.', ',', '-', '(', ')', '/', '=', "'", '+', ':', '?', '!', '"', '%', '&', '*', '<', '>', ';', '{',
			'@', '#', '_', '\n', ' ', '\r'];
		for (let i = 0; i < val.length; i++) {
			if (
				(
					(val.charCodeAt(i) >= '0'.charCodeAt(0) && val.charCodeAt(i) <= '9'.charCodeAt(0)) ||
					(val.charCodeAt(i) >= 'a'.charCodeAt(0) && val.charCodeAt(i) <= 'z'.charCodeAt(0)) ||
					(val.charCodeAt(i) >= 'A'.charCodeAt(0) && val.charCodeAt(i) <= 'Z'.charCodeAt(o)) ||
					this.hasCharOfArray(allowChar, val[i])
				) == false
			) {
				console.log(val[i]);
				return;
			}
		}
		const MAX = 35;
		const COUNT = 4;
		let dataArrayTemp = [];
		let dataResult = [];
		// 1，将字符串val按照 \n  截取为多个字符串保存到数组中
		dataArrayTemp = val.split("\n");
		// 2，将数组中长度超过指定长度的字符串分成两个字符串保存到数组中，将长度为0的字符串从数组中删除掉
		for (let item of dataArrayTemp) {
			// if (item.length > 0 && item.length <= MAX) {
			if (item.length <= MAX) {
				dataResult.push(item);
			} else if (item.length > MAX) {
				dataResult.push(item.substr(0, MAX));
				dataResult.push(item.substr(MAX));
			}
		}
		// 3，将数组以 \n 为连接点 重新 组装成字符串 // 4，将得到的字符串赋值给 this.model...... 进行显示 
		if (dataResult.length > COUNT) {
			return;
		} else {
			this.model[this.props.argArr.prefix + '_nam'+ '_adr'] = dataResult.join("\n");
		}
	}

	clearTxt=()=>{
		let prefix = this.props.argArr.prefix
		if(this.model[prefix + '_extkey']==''){
			this.model[prefix + '_namcn']=''
			this.model[prefix + '_adrcn']=''
		}
	}

	childEvevnt = childData => {
		this.child = childData;
	}

	handleClick=()=>{
		this.child.handleClick();
  }

	render() {
		const bind = this.bind
		const { argArr } = this.props
		let desc = argArr.desc
		let prefix = argArr.prefix
		let url = argArr.url
		let flg = argArr.flg
	let datakey = argArr.datakey

		return (
			<Row>
			<Col span={24}>
				<Row className="row">
					<Col span={3} offset={2}>
						<Label value={desc} />
					</Col>
					<Col span={12} offset={2}>
						<InputItem InputProps={{...bind(prefix + '_extkey'),onAfterChange:this.clearTxt,onPressEnter:this.handleClick,formBlur:this.props.formBlur}} disabled={this.props.isDisabled} />
					</Col>
					<Col span={1} offset={1}>
						<PtyPicker url={url} flg={flg} model={this.model} dataKey={datakey} prefix={prefix} disabled={this.props.isDisabled} extkey={this.model[prefix + '_extkey']} childEvevnt={this.childEvevnt} />
					</Col>
				</Row>
				<Col span={24}>
				<Row className="row">
				<Col span={3} offset={2}>
						<Label itext="名称"  />
					</Col>
					<Col span={14} offset={2}>
						<InputItem InputProps={{...bind(prefix + '_namcn'),formBlur:this.props.formBlur}} onChange={(val) => { this.huan(val) }}  disabled={!this.props.isOpen && true} />
					</Col>
				</Row></Col>
				<Col span={24}>
				<Row className="row">
				<Col span={3} offset={2}>
						<Label itext="地址"  />
					</Col>
					<Col span={14} offset={2}>
						<InputItem InputProps={{...bind(prefix + '_adrcn'),formBlur:this.props.formBlur}} onChange={(val) => { this.huan(val) }} disabled={!this.props.isOpen && true}   />
					</Col>
				</Row></Col>
			</Col>
			</Row>
		)

	}
}
