import {
	inject,
	observer,
} from 'mobx-react'
import {
	WeaFormItem,
	WeaLocaleProvider
} from 'ecCom'
import {
	WeaSwitch
} from 'comsMobx'
import {
	Button
} from 'antd'
const getLabel = WeaLocaleProvider.getLabel;

@inject('hrmContractMaintain')
@observer
export default class FormInfo extends React.Component {
	constructor(props) {
		super(props);
	}

	componentWillUnmount() {
		this.props.hrmContractMaintain.refreshFormInfo();
	}

	getForm = () => {
		const {
			hrmContractMaintain
		} = this.props, {
			formInfo,
		} = hrmContractMaintain, {
			form,
			conditions,
			isHireContract,
		} = formInfo, {
			isFormInit,
		} = form;

		let arr = [];
		isFormInit && conditions.map(c => {
			c.items.map((field, index) => {
				const key = field.domkey[0];

				if (key == 'proEndDate' && !isHireContract) return

				arr.push(<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@kpz1qp@${index}`}
                            label={`${field.label}`}
                            labelCol={{span: 5, offset: 3}}
                            wrapperCol={{span: 13}}
                            error={form.getError(field)}
                            tipPosition="bottom"
                        >
                                <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@qdil3u@${index}`} fieldConfig={field} form={form} formParams={form.getFormParams()} onChange={cb => this.handleChange(cb)}/>
                        </WeaFormItem>)
			})
		})
		arr = [...arr, this.getAttach()];
		return <div>{arr}</div>
	}

	handleChange = (cb) => {
		if (Object.keys(cb)[0] == 'contractTypeId') {
			this.props.hrmContractMaintain.getContractTypeInfo(cb.contractTypeId.value)
		}
	}

	getAttach = () => {
		const {
			hrmContractMaintain
		} = this.props, {
			formInfo,
			delAttach,
		} = hrmContractMaintain, {
			ids,
			names,
		} = formInfo;

		if (names.length > 0) {
			return (
				<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@ejjscl`}
	               label={getLabel(156,"附件")}
	               labelCol={{span: 5, offset: 3}}
	               wrapperCol={{span: 13}}
	           	> 
		           	{
						names.map((name, index) =>{
							return <div style={{marginTop: (index == 0)?6:20,position: 'relative'}}>
									  	<a onClick={() => this.downloadFile(`/weaver/weaver.file.FileDownload?fileid=${ids[index]}`)}>{name.length > 17 ? `${name.slice(0, 17)}...` : name}</a>
								   	    <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@xa0wtp@${index}`} onClick={() => delAttach(index)} style={{position: 'absolute',right: 0, top: -4}}>{getLabel(131966, '删除')}</Button>
								   </div>
						})
					}
				</WeaFormItem>
			)
		} else {
			return
		}
	}

	// <a href={`/spa/document/index.jsp?id=${ids[index]}#/main/document/detail`} target='_blank' title={name}>{name.length > 17 ? `${name.slice(0, 17)}...` : name}</a>

	downloadFile = (sUrl) => {

		//iOS devices do not support downloading. We have to inform user about this.
		if (/(iP)/g.test(navigator.userAgent)) {
			alert('Your device does not support files downloading. Please try again in desktop browser.');
			return false;
		}

		//If in Chrome or Safari - download via virtual link click
		if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1 || navigator.userAgent.toLowerCase().indexOf('safari') > -1) {
			//Creating new link node.
			var link = document.createElement('a');
			link.href = sUrl;

			if (link.download !== undefined) {
				//Set HTML5 download attribute. This will prevent file from opening if supported.
				var fileName = sUrl.substring(sUrl.lastIndexOf('/') + 1, sUrl.length);
				link.download = fileName;
			}

			//Dispatching click event.
			if (document.createEvent) {
				var e = document.createEvent('MouseEvents');
				e.initEvent('click', true, true);
				link.dispatchEvent(e);
				return true;
			}
		}

		// Force file download (whether supported by server).
		if (sUrl.indexOf('?') === -1) {
			sUrl += '?download';
		}

		window.open(sUrl, '_self');
		return true;
	}

	render() {
		return this.getForm()
	}
}