import {
	observable,
	action,
	computed
} from 'mobx';
import {
	WeaSwitch
} from 'comsMobx';
import {WeaTools} from 'ecCom';
import {
	Button,
	message
} from 'antd';
import HrmBaseStore from './baseStore';
import * as api from '../apis/orgChart';
import {
	i18n
} from '../public/i18n';

export class HrmOrgChart extends HrmBaseStore {
	/********************* unobservable list *********************/
	topBtnAndMenu = () => {
		let btns = [
		]
		let menus = [];
		btns.map((btn, index) => {
			const {
				props
			} = btn;
			menus.push({
				key: index.toString(),
				content: props.children,
				icon: <i className={this.menuIconCollection[props.op]}/>,
				onClick: props.onClick,
				disabled: props.disabled || false
			});
		})
		return {
			btns,
			menus: [...menus, ...this.getBasicMenus()]
		}
	}

	tabConfig = {
		tabs: [],
		keyParam: 'viewCondition',
		activeTabKey: '1'
	}

	iframe;
	/********************* unobservable list *********************/

	/********************* dialog info setting *********************/

	/********************* dialog info setting *********************/

	/********************* observable list *********************/
	@observable spinning = false;
	@observable refreshOrgForm = new Date().getTime();
	@observable refreshTab = new Date().getTime();

	/********************* observable list *********************/

	/********************* computed list *********************/
	/********************* computed list *********************/

	/********************* action list *********************/
	initData = (refresh = false) => {
		this.iframe = null;
		this.containerInitFinished = {
			...this.containerInitFinished,
			authorized: true,
			init: true
		}
		this.callInitData();
	}

	callInitData = () => {
		if(this.spinning)
			return;
		this.spinning = true;
		api.initData().then(rs => {
			rs.map((result, index) => {
				if (result.status === '1') {
					switch (index) {
						case 0:
							this.setFormData('orgForm', result.conditions);
							this.refreshOrgForm = new Date().getTime();
							break;
						case 1:
							this.setFormData('settingForm', result.conditions);
							break;
						case 2:
							this.tabConfig.tabs.length = 0;
							this.tabConfig.buttons = this.generateTabBtn();
							result.tabs.map(tab => {
								this.tabConfig.tabs.push({
									color: '#000000',
									groupId: tab.key,
									title: tab.title,
									viewCondition: tab.key,
									searchType: ['advanced'],
									doSearch: (params) => this.getChartData(true)
								})
							});
							this.setActiveTab('1');
							break;
					}
				}
			})
		})
	}

	generateTabBtn = () => {
		const {
			orgForm,
			orgFormFields
		} = this.formTarget;
		return [<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@h1wbcq@1`} fieldConfig={orgFormFields[0].items[0]} form={orgForm} onChange={() => this.getChartData()}/>]
	}

	setIframe = (iframe) => {
		if(iframe == null || this.iframe != null)
			return;
		this.iframe = iframe;
		this.callInitData();
	}

	tabChangeHandle = (key) => {
		this.setActiveTab(key);
	}

	@action setActiveTab = (key) => {
		this.tabConfig.activeTabKey = key || this.tabConfig.tabs[0].viewCondition;
		const tabIndex = this.getTabIndex(this.tabConfig.tabs, key);
		if (tabIndex < 0) return;
		let cmd;
		switch(key){
			case '2':
				cmd = 'doc';
				break;
			case '3':
				cmd = 'customer';
				break;
			case '4':
				cmd = 'project';
				break;
		}
		api.getOrgChartSearchCondition({cmd}).then(data => {
			if(data.status === '1'){
				this.setFormData('conditionForm', data.conditions);
				this.getChartData();
			}else{

			}
			this.refreshTab = new Date().getTime();
		}, error => {
			this.refreshTab = new Date().getTime();
		})
	}

	@action getChartData = (reset = false) => {
		this.spinning = true;
		const orgFormParams = this.formTarget.orgForm.getFormParams();
		const settingFormParams = this.formTarget.settingForm.getFormParams();
		const condtionFormParams = this.formTarget.conditionForm.getFormParams();
		const params = Object.assign({}, {
			arg0: orgFormParams.sOrg,
			arg1: settingFormParams.shownum,
			arg3: orgFormParams.sOrg < 0 ? false : true,
			arg11: !reset ? ';;P' : ';;B'
		});

		switch(this.tabConfig.activeTabKey){
			case '1':
				Object.assign(params, {
					arg4: reset ? condtionFormParams.status : ''
				});
				break;
			case '2':
				Object.assign(params, {
					arg5: condtionFormParams.docStatus,
					arg10: 'doc'
				});
				break;
			case '3':
				Object.assign(params, {
					arg6: condtionFormParams.customerType,
					arg7: condtionFormParams.customerStatus,
					arg10: 'customer'
				});
				break;
			case '4':
				Object.assign(params, {
					arg8: condtionFormParams.workType,
					arg9: condtionFormParams.projectStatus,
					arg10: 'project'
				});
				break;
		}

		api.getOrgChartData(params).then(data => {
			if (data.status === '1') {
				const msgData = {
					act: 'orgChart', // 自定义的消息类型、行为，用于switch条件判断等。。
					orgData: data.data,
					params: {
						sorgid: orgFormParams.sOrg,
						isShow: params.arg11,
						showNum: 'true',
						shownum: settingFormParams.shownum
					},
					client: {
						browser: WeaTools.ua.browser,
						browserVersion: {
							browser: WeaTools.ua.browser,
							version: parseInt(WeaTools.ua.version)
						},
						version: parseInt(WeaTools.ua.version),
						os: WeaTools.ua.os
					}
				};
				let tryCount = 0;
				const sendMsg = setInterval(() => {
					if($('#orgChartFrame')[0] != null){
						clearInterval(sendMsg);
						$('#orgChartFrame')[0].contentWindow.postMessage(msgData, '*');
					}
				}, 500);
			} else {
				this.spinning = false;
			}
		}, error => {
			this.spinning = false;
		})
	}

	onInitOrgChart = (status) => {
		this.spinning = false;
		switch (status) {
			case 0:
				message.error(i18n.message.dataNone());
				break;
			case -1:
				message.error(i18n.message.dataConstructError());
				break
		}
	}

	showSpin = (val) => {
		this.spinning = (val === '0');
	}
	/********************* action list *********************/
}

export const hrmOrgChart = new HrmOrgChart();