package com.engine.voting.cmd.VotingForm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

import com.api.browser.bean.BrowserBean;
import com.api.browser.bean.SearchConditionItem;
import com.api.browser.bean.SearchConditionOption;
import com.api.browser.util.ConditionType;
import com.api.voting.util.BrowserType;
import com.api.voting.util.ConditionUtil;
import com.api.voting.util.VotingCommonUtil;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.google.common.collect.Maps;

/**
 * 后台 调查 - 网上调查设置 高级搜索条件
 */
public class VotingFormConditionCmd extends AbstractCommonCommand<Map<String, Object>> {
	
	
    public VotingFormConditionCmd(User user, Map<String,Object> params) {
    	this.user = user;
    	this.params = params;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> apidatas = Maps.newHashMap();
        
        String istemplate = Util.null2s(Util.null2String(this.params.get("istemplate")), "0");
        try {

			String votingtype = Util.null2String(this.params.get("votingtype"));

            RecordSet rs = new RecordSet();
            
            List<Map<String,Object>> grouplist = new ArrayList<Map<String,Object>>();
			
			Map<String,Object> groupitem1 = new HashMap<String,Object>();
			List<SearchConditionItem> itemlist1 = new ArrayList<SearchConditionItem>();
			groupitem1.put("title", SystemEnv.getHtmlLabelNames(ConditionUtil.COMMON_CONDITION, user.getLanguage()));
			groupitem1.put("defaultshow", true);
			groupitem1.put("items", itemlist1);
			grouplist.add(groupitem1);
            
			//itemlist1.add(ConditionUtil.getCondition(DocCondition.DOC_SUBJECT,user));
			int lanaguage = user.getLanguage();
			
			//调查名称
			itemlist1.add(getInput(lanaguage,"33439","subject"));
			//创建人
			itemlist1.add(getBrowser(lanaguage,"882","createrid",BrowserType.USER));
			
          	 //调查类型
			List<SearchConditionOption> typeOptions = new ArrayList<SearchConditionOption>();
			List<SearchConditionOption> typeOptionsOfAdd = new ArrayList<SearchConditionOption>();
			String where = "" ;
			if(StringUtils.isNotBlank(votingtype)){
				where = " and id in ("+votingtype+") " ;
			}
			rs.executeQuery("select * from voting_type "+where);
			typeOptions.add(new SearchConditionOption("",""));
			typeOptionsOfAdd.add(new SearchConditionOption("",""));
	        while(rs.next()) {
	        	typeOptions.add(new SearchConditionOption(rs.getString("id"),rs.getString("typename")));
	        	typeOptionsOfAdd.add(new SearchConditionOption(rs.getString("id"),rs.getString("typename")));
	        }
			itemlist1.add(getSelect(lanaguage,"24111","votingtype",typeOptions));
			
            //状态
			if(!"1".equals(istemplate)) {
			    List<SearchConditionOption> statusOptions = new ArrayList<SearchConditionOption>();
	            statusOptions.add(new SearchConditionOption("",""));
	            statusOptions.add(new SearchConditionOption("0",SystemEnv.getHtmlLabelName(83443,weaver.general.Util.getIntValue(user.getLanguage()))));
	            statusOptions.add(new SearchConditionOption("3",SystemEnv.getHtmlLabelName(30835, user.getLanguage())));
	            statusOptions.add(new SearchConditionOption("1",SystemEnv.getHtmlLabelName(18600, user.getLanguage())));
	            statusOptions.add(new SearchConditionOption("2",SystemEnv.getHtmlLabelName(405, user.getLanguage())));
	            itemlist1.add(getSelect(lanaguage,"602","status",statusOptions));
	            
	             //开始日期
	            //itemlist1.add(getDate(lanaguage,"740","begindate"));
	             //截止日期
	            //itemlist1.add(getDate(lanaguage,"741","enddate"));
	            
	            //调查时间（时间选择）
	            itemlist1.add(new SearchConditionItem(ConditionType.DATE, SystemEnv.getHtmlLabelName(500889, user.getLanguage()), "", new String[]{"datetype", "begindate", "enddate"}, 
	                    VotingCommonUtil.getDateSelectOption(user.getLanguage(), false, false), 6, 18, null));
			} else {
			    itemlist1.add(getInput(lanaguage, "433","descr"));
			}
			
            apidatas.put("condition", grouplist);
            
            Map<String,BrowserBean> btnBrowser = new HashMap<String,BrowserBean>();
            btnBrowser.put("votingMould", new BrowserBean(BrowserType.votingMould));
            apidatas.put("btnBrowser",btnBrowser );
            
            apidatas.put("votingType", getSelect(lanaguage,"24111","votingtype",typeOptionsOfAdd));
            
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            return apidatas;
        }
        apidatas.put("api_status", true);
        return apidatas;
    }
    
    public SearchConditionItem getInput(int language,String labelids,String domkey){
    	SearchConditionItem	searchConditionItem = new SearchConditionItem(ConditionType.INPUT, SystemEnv.getHtmlLabelNames(labelids,language),
    			new String[]{domkey}); 
		searchConditionItem.setLabelcol(6);
		searchConditionItem.setFieldcol(18);
		return searchConditionItem;
    }
    
    public SearchConditionItem getBrowser(int language,String labelids,String domkey,String browserType){
    	SearchConditionItem	searchConditionItem = new SearchConditionItem(ConditionType.BROWSER, SystemEnv.getHtmlLabelNames(labelids,language),
    			new String[]{domkey}); 
    	BrowserBean browserBean= new BrowserBean(browserType);
		searchConditionItem.setBrowserConditionParam(browserBean);
		searchConditionItem.setLabelcol(6);
		searchConditionItem.setFieldcol(18);
		return searchConditionItem;
    }
    
    public SearchConditionItem getSelect(int language,String labelids,String domkey,List<SearchConditionOption> options){
    	SearchConditionItem	searchConditionItem = new SearchConditionItem(ConditionType.SELECT, SystemEnv.getHtmlLabelNames(labelids,language),
    			new String[]{domkey}); 
		searchConditionItem.setLabelcol(6);
		searchConditionItem.setFieldcol(18);
		searchConditionItem.setOptions(options);
    	Map<String, Object> otherParams = new HashMap<String, Object>();
    	otherParams.put("dropdownMatchSelectWidth", true);
    	searchConditionItem.setOtherParams(otherParams);
		return searchConditionItem;
    }
    
    public SearchConditionItem getDate(int language,String labelids,String domkey){
    	SearchConditionItem	searchConditionItem = new SearchConditionItem(ConditionType.DATEPICKER, SystemEnv.getHtmlLabelNames(labelids,language),
    			new String[]{domkey}); 
		searchConditionItem.setLabelcol(6);
		searchConditionItem.setFieldcol(18);
		return searchConditionItem;
    }

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

}
