import {
	observable,
	action,
	computed
} from 'mobx';
import {
	WeaHelpfulTip,
	WeaSelect
} from 'ecCom';
import {
	WeaForm,
	WeaTableNew
} from 'comsMobx';
import {
	Button,
	message
} from 'antd';
import FormInfo from '../coms/FormInfo';
import HrmBaseStore from './baseStore';
import * as api from '../apis/leaveType';
import {
	i18n
} from '../public/i18n';
import {
	has,
	indexOf,
	cloneDeep,
	uniq
} from 'lodash';
import {
	convertCalendarDate
} from '../util/index';

const {
	TableStore
} = WeaTableNew;

export class HrmLeaveType extends HrmBaseStore {
	/********************* unobservable list *********************/
	topBtnAndMenu = () => {
		let btns = [
			<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@wv9ydi`} type='primary' op='save' onClick={() => this.save()}>{i18n.button.save()}</Button>
		]
		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('HRM_ENGINE_LEAVETYPE_SETTING')]
		}
	}

	tableRef;
	weekdays;

	columns = [];
	// tableEditDatas = [];
	/********************* unobservable list *********************/

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

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

	/********************* observable list *********************/
	@observable tableConfig = {
		...this.editTableConfig,
		showTitle: true,
		showAdd: true,
		showDelete: true,
		showCopy: false,
		columns: [],
		datas: [],
		selectedData: {},
		onChange: (data) => {
			this.convertTableConfig(cloneDeep(this.columns), data);
		},
		onRowSelect: this.onRowSelect,
		getRowSelection: this.getRowSelection,
		onDelete: this.onDelete
	}
	/********************* observable list *********************/

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

	/********************* action list *********************/
	initData = () => {
		this.containerInitFinished = {
			...this.containerInitFinished,
			init: true
		}
		this.clearTableData();
	}

	clearTableData = () => {
		this.tableConfig.datas.length = 0;
		this.tableConfig.selectedData = {};
		this.getTableData();
	}

	getTableData = () => {
		api.getList().then(data => {
			if (data.status === '1') {
				this.columns = data.items[0].table.columns;
				const tableDatas = data.items[0].table.datas;
				this.weekdays = data.items[0].table.weekdays;
				this.convertTableConfig(cloneDeep(this.columns), tableDatas);
			} else {
				message.error(data.message);
			}
		}, error => {

		})
	}

	setTableRef = (ref) => {
		this.tableRef = ref;
	}

	convertTableConfig = (columns, datas) => {
		let selectedData = {};
		columns.map((column, i) => {
			switch (i) {
				case 0:
					column.width = '25%';
					break;
				case 1:
					column.width = '10%';
					break;
				case 2:
					column.width = '15%';
					break;
				case 3:
					column.width = '25%';
					break;
				case 4:
					column.width = '15%';
					break;
				case 5:
					column.width = '10%';
					break;
			}
			column.useRecord = true;
			if (has(column, 'tips')) {
				column.title = <span>{column.title}<WeaHelpfulTip ecId={`${this && this.props && this.props.ecId || ''}_WeaHelpfulTip@dbalet@${i}`} style={{paddingLeft: '5px'}} title={column.tips}/></span>
			}
			if (has(column, 'checkType') && column.checkType === 'checkbox') {
				Object.assign(selectedData, {
					[column.dataIndex]: []
				})
			}
		})
		const selectedDataKey = Object.keys(selectedData);
		datas.map((data, i) => {
			if (!has(data, 'id')) {
				// data.isCalWorkDayField = '1';
				data.color = data.color || '#000000';
				data.edit = true;
				data.checkProps = {
					isCalWorkDayField: {
						viewAttr: 1
					}
				}
			}
			selectedDataKey.map(key => {
				has(data, key) && data[key] === '1' && selectedData[key].push(i);
			})
			delete data.com;
			if (data.isCalWorkDayField === '0') {
				data.com = data.com || {};
				data.com.isCalWorkDayField = this.weekdays;
			}
		})

		Object.assign(this.tableConfig, {
			columns,
			datas,
			selectedData
		});
		setTimeout(() => $(".column_select").parent().parent().css('display', 'inline-block').css('width', '110px'), 0);
	}

	getRowSelection = rowSelection => {
		const sel = { ...rowSelection
		};
		sel.getCheckboxProps = record => {
			return {
				disabled: !record.edit
			};
		};
		return sel;
	}

	onRowSelect = (sRowKeys, rows, dataIndex, selectedDatas) => {
		if (dataIndex != null && selectedDatas != null && has(selectedDatas, dataIndex)) {
			const arr = selectedDatas[dataIndex];
			this.tableConfig.datas.map((data, index) => {
				data[dataIndex] = indexOf(arr, index) >= 0 ? '1' : '0';
			})
			this.convertTableConfig(cloneDeep(this.columns), this.tableConfig.datas);
		}
	}

	@action save = () => {
		const rs = this.tableRef.refs.edit.doRequiredCheck();
		if (rs.pass) {
			const datas = this.toJS(this.tableConfig.datas);
			let nameArray = [];
			datas.map(data => {
				delete data.com;
				delete data.checkProps;
				nameArray.push(data.selectname);
			})
			let checkSame = uniq(nameArray).length === datas.length;
			if (checkSame) {
				api.save({
					subcompanyid: '0',
					datas: JSON.stringify(datas)
				}).then(data => {
					if (data.status === '1') {
						message.success(i18n.message.saveSuccess());
						this.getTableData();
					} else {
						message.error(data.message);
					}
				}, error => {})
			} else {
				message.error(i18n.confirm.leaveTypeNameExist());
			}
		}
	}

	onDelete = (key, datas) => {
		let ids = [];
		datas.map(data => {
			if (has(data, 'id') && has(data, 'edit') && data.edit)
				ids.push(data.id);
		})

		// let content = i18n.confirm.removeSelected();
		// this.confirmInfo({
		// 	content,
		// 	onOk: () => {
				api.remove({
					subcompanyid: '0',
					ids: ids.join(',')
				}).then(data => {
					if (data.status === '1') {
						message.success(i18n.message.deleteSuccess());
					} else {
						message.error(data.message);
					}
				}, error => {})
		// 	}
		// });
	}
	/********************* action list *********************/
}

export const hrmLeaveType = new HrmLeaveType();