import {
	observable,
	action,
	computed,
} from 'mobx';
import {
	WeaForm,
} from 'comsMobx';
import * as api from '../apis/holidayRule';


class HrmTest {
	@observable f = {
		form: new WeaForm(),
		conditions: []
	}

	togetherKeys = {
		typeId: ['ruleName', 'scopeType']
	}

	prefix = {
		scopeValue: <span>分部前</span>
	}

	suffix = {
		scopeValue: <span>分部后</span>,
		distributionMode: ['', '1', '2', '3', '4', '5', '6']
	}

	linkage = {
		scopeValue: {
			activeKey: 'scopeType',
			activeValue: '1',
		}
	}

	togetherElements = {};

	@action getLeaveRulesForm = () => {
		api.getLeaveRulesForm().then(datas => {
			const {
				condition,
			} = datas;

			const items = this.collectTogetherElement(condition, this.togetherKeys);
			Object.assign(this.togetherElements, items);

			this.blackList = this.computeBlackList(this.togetherKeys);

			this.f.form = new WeaForm();
			this.f.conditions = condition;
			this.f.form.initFormFields(condition);
		}, error => {})
	}

	collectTogetherElement = (conditions, togetherKeys) => {
		const values = Object.values(togetherKeys); //[['ruleName', 'scopeType']]
		const keys = Object.keys(togetherKeys); //['typeId']

		const items = {};

		conditions.map(c => {
			c.items.map(item => {
				const {
					domkey
				} = item;

				const key = domkey[0];

				values.map((value, index) => {
					if (value.includes(key)) { //['ruleName', 'scopeType']
						const firstKey = keys[index]; //typeId
						if (!items[firstKey]) {
							items[firstKey] = [];
						}
						items[firstKey].push(item);
					}
				});

			});
		})

		return items;
	}

	computeBlackList = (togetherKeys) => {
		const blackList = [];

		Object.values(togetherKeys).forEach(value => {
			value.forEach(key => {
				blackList.push(key);
			});
		});

		return blackList;
	}
}

export const hrmTest = new HrmTest();