import {
	Modal,
	message,
	Button,
} from 'antd';
import {
	WeaMoreButton,
	WeaLocaleProvider,
} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
const confirm = Modal.confirm;

const fetch = (params) => {
	const {
		asyncFetch,
		name,
		fetchParams = {},
		logic,
	} = params;

	asyncFetch[name](fetchParams).then(cb => {
		const {
			api_status,
			status,
		} = cb;

		if (api_status || status === '1') {
			logic(cb);
		} else {
			message.error();
		}
	}).catch(error => {
		message.error();
	});
}

const getDialogButtons = (params) => {
	const {
		datas,
		dropMenuDatas = [],
	} = params;

	const btns = [];
	datas.map((data, index) => {
		const {
			name,
			disabled,
			onBtnClick
		} = data;
		btns.push(
			<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@x9z7jl@${index}`} type="primary" disabled={disabled} onClick={onBtnClick}>{name}</Button>
		);
	});
	btns.push(
		<WeaMoreButton ecId={`${this && this.props && this.props.ecId || ''}_WeaMoreButton@kt4g0j`} datas={dropMenuDatas}/>
	);
	return btns
}

const getMenusCreationConfig = (datas) => {
	const topMenu = datas.filter(item => {
		const {
			isTop,
			isBatch
		} = item;
		return isTop === '1' || isBatch === '1'
	})

	const rightMenu = datas;

	return {
		topMenu,
		rightMenu
	}
}

function getTopButtons(params) {
	const {
		datas,
		disabled,
	} = params;

	let btns = [];
	datas.map((item, index) => {
		const {
			menuFun,
			menuName,
			title,
		} = item;

		btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@zmsxm1@${index}`} 
			type="primary" 
			disabled={Array.isArray(disabled) ? disabled[index] : disabled } 
			onClick={this[menuFun]}
			title={title}
			>
				{menuName}
			</Button>)
	});

	return btns
}

function getDropMenuDatas(params) {
	const {
		datas,
		disabled,
	} = params;

	let dropMenuDatas = [];

	datas.map((item, index) => {
		const {
			menuFun,
			menuName,
			menuIcon
		} = item;

		dropMenuDatas.push({
			key: index.toString(),
			disabled: Array.isArray(disabled) ? disabled[index] : disabled,
			icon: <i className={`${menuIcon}`} />,
			content: menuName,
			onClick: this[menuFun]
		});
	});

	return dropMenuDatas;
}

const getSearchButtons = (params) => {
	const {
		form,
		search,
		cancel,
		reset,
	} = params;

	const buttons = [];
	const buttonNames = [getLabel(82529, '搜索'), getLabel(27088, '重置'), getLabel(32694, '取消')];

	[0, 1, 2].map((v, index) => {
		buttons.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@2rnt6b@${index}`} type={v===0 ? "primary" : ''} onClick={() => {v===0 ? search() : v=== 1 ? (reset ? reset() :  form.reset()) : cancel()}}>{buttonNames[v]}</Button>);
	});

	return buttons;
}


const showFailMsg = (messages, ratio) => {
	const msgs = [],
		duration = messages.length;

	messages.map((msg, index) => msgs.push(<span style={{marginLeft: (index > 0) ? 22 : 0}}>{`${index + 1}. ${msg}`}<br /></span>));

	message.warning(msgs, duration * ratio);
}

const msg = {
	showFailMsg: showFailMsg
}

const isComplete = (fields, datas) => {
	const unfilledRows = [];

	datas.map((data, index) => {
		fields.some(field => data[field] === undefined) && unfilledRows.push(index + 1);
	});

	(unfilledRows.length > 0) && message.warning(`${getLabel('505567',"第")} ${unfilledRows.toString()} ${getLabel('27592',"行")}${getLabel('505569',"余额发放")}${getLabel('505570',"天数")}${getLabel('21423',"未填写")}`);

	if (unfilledRows.length > 0) return false;

	return true;
}

const validate = {
	isComplete: isComplete
}

const add = (config) => {
	const {
		fields,
		intervals,
		datas,
		msgs
	} = config;

	const last = datas[datas.length - 1];
	const clone = [...datas];

	if (!validate.isComplete(fields, datas)) return datas;

	const newData = {};

	Object.keys(last).map((key, index) => {
		if (!fields.includes(key)) {
			Object.assign(newData, {
				[key]: last[key] + intervals[key],
			});
		} else {
			Object.assign(newData, {
				[key]: last[key] + 1,
			});
		}
	});

	clone.push(newData);

	return clone;

}

const rule = {
	add: add
}

const getArrayDatas = (cb) => {
	let datas = [];

	const keys = Object.keys(cb);

	keys.map(key => {
		if (Array.isArray(cb[key])) {
			datas = cb[key];
		}
	});

	return datas;
}

const getTabId = (cb) => {
	let ids = [];

	const keys = Object.keys(cb);

	keys.map(key => {
		if (Array.isArray(cb[key])) {
			cb[key].map(item => {
				item.id && ids.push(item.id);
			});
		}
	});

	return ids
}

const callback = {
	getArrayDatas: getArrayDatas,
	getTabId: getTabId,
}

const componentReset = {
	resetRadioGroup: (ref) => {
		ref.resetDefault();
	},
	resetBrowser: (ref) => {
		ref.set([])
	}
}

const capitalize = ([first, ...rest], lowerRest = false) => {
	return first.toUpperCase() + (lowerRest ? rest.join('').toLowerCase() : rest.join(''));
}

const string = {
	capitalize: capitalize
}

const executeFunction = (config) => {
	const {
		functions,
		params,
		father
	} = config;

	functions.map((func, index) => {
		const object = Array.isArray(father) ? father[index] : father;
		object[func](params ? params[index] : '');
	});
}

const page = {
	initConfig: (params, pointer) => {
		Object.keys(params).map(key => {
			pointer[key] = params[key];
		});
	}
}

const isUnique = (val, arr) => {
	let flag = true;

	arr.map(ele => {
		if (ele === val) {
			flag = false;
		}
	});

	return flag;
}

const array = {
	isUnique: isUnique
}

const showDelConfirm = (params) => {
	const {
		type,
		logic
	} = params;

	confirm({
		title: getLabel('131329', '信息确认'),
		content: (type === 0) ? getLabel('83877', '确定要删除吗？') : getLabel('385625', '确定要删除选择的记录吗？'),
		okText: getLabel('33703', '确定'),
		cancelText: getLabel('32694', '取消'),
		onOk() {
			logic(type);
		},
		onCancel() {
			return false;
		},
	});
}

const showSaveConfirm = (params) => {
	const {
		content,
		logic
	} = params;

	confirm({
		title: getLabel('131329', '信息确认'),
		content,
		okText: getLabel('33703', '确定'),
		cancelText: getLabel('32694', '取消'),
		onOk() {
			logic();
		},
		onCancel() {
			return false;
		},
	});
}

const confirmation = {
	showDelConfirm: showDelConfirm,
	showSaveConfirm,
}

export {
	fetch,
	getDialogButtons,
	getMenusCreationConfig,
	getTopButtons,
	getDropMenuDatas,
	getSearchButtons,
	msg,
	validate,
	rule,
	callback,
	componentReset,
	string,
	executeFunction,
	page,
	array,
	confirmation,
}