package com.engine.kq.cmd.group;

import com.api.browser.bean.BrowserBean;
import com.api.browser.bean.SearchConditionItem;
import com.api.browser.bean.SearchConditionOption;
import com.api.hrm.bean.HrmFieldBean;
import com.api.hrm.util.HrmFieldSearchConditionComInfo;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

import java.util.*;

public class GetAddMemberConditionCmd extends AbstractCommonCommand<Map<String, Object>>{

	public GetAddMemberConditionCmd(Map<String, Object> params, User user) {
		this.user = user;
		this.params = params;
	}

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
		List<String> fields = new ArrayList<>(Arrays.asList(
				"groupId,515195,3,kqgroup"
		));

		Map<String,Object> retmap = new HashMap<String,Object>();
		List condition = new ArrayList();
		Map<String, Object> groupitem = null;
		try {
			if(!HrmUserVarify.checkUserRight("HrmKQGroup:Add", user)){
				retmap.put("status", "-1");
				retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
				return retmap;
			}

			HrmFieldSearchConditionComInfo hrmFieldSearchConditionComInfo = new HrmFieldSearchConditionComInfo();

			List<SearchConditionItem> items = new ArrayList<>();

			HrmFieldBean hrmFieldBean = null;
			SearchConditionItem searchConditionItem = null;
			List<SearchConditionOption> options = null;

			groupitem = new HashMap<>();
			groupitem.put("title", "");
			groupitem.put("defaultshow", true);

			for(String f : fields){
				String [] fieldConfig = f.split(",");
				String fieldname = fieldConfig[0];
				String fieldlabel = fieldConfig[1];
				String fieldhtmltype = fieldConfig[2];
				String type = fieldConfig[3];

				hrmFieldBean = new HrmFieldBean();
				hrmFieldBean.setFieldname(fieldname);
				hrmFieldBean.setFieldlabel(fieldlabel);
				hrmFieldBean.setFieldhtmltype(fieldhtmltype);
				hrmFieldBean.setType(type);
				hrmFieldBean.setIsFormField(true);
				hrmFieldBean.setViewAttr(3);

				if(fieldname.equals("sendDate")){
					hrmFieldBean.setIsFormField(false);
				}

				searchConditionItem = hrmFieldSearchConditionComInfo.getSearchConditionItem(hrmFieldBean, user);
				searchConditionItem.setRules("required|string");
				Map<String, Object> conditionParams = new HashMap<>();
				conditionParams.put("advance", "1");
				Map<String, Object> dataParams = new HashMap<>();
				dataParams.put("advance", "1");
				Map<String, Object> completeParams = new HashMap<>();
				completeParams.put("includeAll", "1");
				searchConditionItem.getBrowserConditionParam().setConditionDataParams(conditionParams);
				searchConditionItem.getBrowserConditionParam().setDataParams(dataParams);
				searchConditionItem.getBrowserConditionParam().setCompleteParams(completeParams);
				searchConditionItem.getBrowserConditionParam().setQuickSearchName("groupname");
				items.add(searchConditionItem);
			}
			groupitem.put("items", items);
			condition.add(groupitem);
			retmap.put("condition", condition);
			retmap.put("status", "1");
		} catch (Exception e) {
			writeLog(e);
			retmap.put("status", "-1");
			retmap.put("message", SystemEnv.getHtmlLabelName(382661, user.getLanguage()));
		}
		return retmap;
	}

	@Override
	public BizLogContext getLogContext() {
		return null;
	}

}
