package com.gx.obe.evaluation.step.form;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.listener.FloatInputListener;
import org.eclipse.swt.core.listener.IntegerInputListener;
import org.eclipse.swt.core.listener.TextHintListener;
import org.eclipse.swt.core.listener.VerifyRequiredListener;
import org.eclipse.swt.core.utils.ValidateUtils;
import org.eclipse.swt.core.utils.WidgetUtils;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.EDialog;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.button.Tips;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.components.core.enumeration.EvaluationStepEnum;
import com.gx.obe.components.core.enumeration.TenderProjectEnum;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;
import com.gx.obe.web.service.ProjectRuleService;
import com.gx.obe.web.service.evaluation.EvaluationStepService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 推荐中标候选人节点
 * @author guoyr
 */
public class RecommonedEvaluationStepDialog extends EDialog {
	
	private Text stepNameText;
	private Composite roleTypeComposite;
	private EButton save_btn;
	private ImageResolver imageResolver = new ImageResolver();
	private EvaluationStepService evaluationStepService = new EvaluationStepService();
	private ProjectRuleService projectRuleService ;
	private TenderProjectEntity tenderProjectEntity;
	private TenderProjectRuleEntity tenderProjectRuleEntity;
	private EvaluationStep evaluationStep;
	private Text stepMemoText;
	private CallBack callBack;
	private Button button_3;
	private Button button_4;
	private Text recommonedCountText;
	private Combo first_sort_order;
	private Combo second_sort_order;
	private Combo third_sort_order;
	private List<Map<String, String>> orderLevelList = null;
	private Button removeMaxAndMin_btn;
	private Composite composite;
	private Text evalMinScoreText;
	private Label finalistByScoreTips;
	private Label defaultRecommonedTips;
	
	{
		LogUtils.logClass(RecommonedEvaluationStepDialog.class);
	}
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public RecommonedEvaluationStepDialog(Shell parentShell, TenderProjectEntity tenderProjectEntity, EvaluationStep evaluationStep) {
		super(parentShell);
		setTitle(Messages.Step_step);
		this.tenderProjectEntity = tenderProjectEntity;
		this.evaluationStep = evaluationStep;
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite topContainer = (Composite) super.createDialogArea(parent);
		topContainer.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				imageResolver.disposite();
			}
		});
		GridLayout gl_topContainer = new GridLayout(1, false);
		gl_topContainer.verticalSpacing = 0;
		gl_topContainer.horizontalSpacing = 0;
		gl_topContainer.marginHeight = 0;
		gl_topContainer.marginWidth = 0;
		topContainer.setLayout(gl_topContainer);
		
		Composite container = new Composite(topContainer, SWT.NONE);
		container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_container = new GridLayout(3, false);
		gl_container.marginHeight = 10;
		gl_container.marginWidth = 10;
		container.setLayout(gl_container);
		this.setOperator(SWT.MAX|SWT.CLOSE);
		
		Tips label_6 = new Tips(container, SWT.WRAP);
		label_6.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		label_6.setText(Messages.Tips_winnerRule);
		label_6.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, EvaluationStepEnum.getEvaluationStepIco(EvaluationStepEnum.RECOMMONED)));
		label_6.setSurroundColor(SWTResourceManager.getColor(60, 138, 226));
		
		composite = new Composite(container, SWT.NONE);
		GridLayout gl_composite = new GridLayout(2, false);
		gl_composite.verticalSpacing = 10;
		composite.setLayout(gl_composite);
		GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_composite.widthHint = 414;
		composite.setLayoutData(gd_composite);
		
		Label step_name = new Label(composite, SWT.NONE);
		step_name.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		step_name.setAlignment(SWT.RIGHT);
		step_name.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		step_name.setText(Messages.StepName + ": ");
		
		Composite composite_2 = new Composite(composite, SWT.NONE);
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		GridLayout gl_composite_2 = new GridLayout(1, false);
		gl_composite_2.marginWidth = 0;
		gl_composite_2.verticalSpacing = 1;
		gl_composite_2.marginHeight = 0;
		composite_2.setLayout(gl_composite_2);
		
		stepNameText = new Text(composite_2, SWT.NONE);
		stepNameText.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		stepNameText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		stepNameText.addPaintListener(new TextHintListener(Messages.Tips_EnterEvaluationStepName));
		stepNameText.addVerifyListener(new VerifyRequiredListener(stepNameText, MessageFormat.format(Messages.Tips_required, Messages.StepName)));
		stepNameText.setTextLimit(20);
		stepNameText.setText("");
		
		ESepator label = new ESepator(composite_2, SWT.NONE);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		label.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		Label label_1 = new Label(composite, SWT.NONE);
		label_1.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_1.setText(Messages.EvalExpert + "：");
		
		roleTypeComposite = new Composite(composite, SWT.NONE);
		GridLayout gl_roleTypeComposite = new GridLayout(2, false);
		gl_roleTypeComposite.marginHeight = 0;
		gl_roleTypeComposite.marginWidth = 0;
		roleTypeComposite.setLayout(gl_roleTypeComposite);
		roleTypeComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		button_3 = new Button(roleTypeComposite, SWT.RADIO);
		button_3.setSelection(true);
		button_3.setText(Messages.ExpertLeader);
		button_3.setData("roleType", EvaluationStepEnum.EXPERT_LEADER);
		
		button_4 = new Button(roleTypeComposite, SWT.RADIO);
		button_4.setData("roleType", EvaluationStepEnum.OPERATOR);
		button_4.setText(Messages.ProjectManager);
		
		Label label_3 = new Label(composite, SWT.NONE);
		label_3.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_3.setText(Messages.RecommendedPriority + "：");
		
		Composite composite_3 = new Composite(composite, SWT.NONE);
		GridLayout gl_composite_3 = new GridLayout(4, false);
		gl_composite_3.marginWidth = 0;
		gl_composite_3.marginHeight = 0;
		composite_3.setLayout(gl_composite_3);
		
		Label label_7 = new Label(composite_3, SWT.NONE);
		label_7.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		label_7.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_7.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		label_7.setText(Messages.FirstPriority );
		first_sort_order = new Combo(composite_3, SWT.READ_ONLY);
		GridData gd_priorityOneCombo = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_priorityOneCombo.widthHint = 120;
		first_sort_order.setLayoutData(gd_priorityOneCombo);
		
		Label label_8 = new Label(composite_3, SWT.NONE);
		label_8.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		label_8.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_8.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		label_8.setText(Messages.SecondPriority);
		
		second_sort_order = new Combo(composite_3, SWT.READ_ONLY);
		GridData gd_priorityTwoCombo = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_priorityTwoCombo.widthHint = 120;
		second_sort_order.setLayoutData(gd_priorityTwoCombo);
		
		Label label_9 = new Label(composite_3, SWT.NONE);
		label_9.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		label_9.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_9.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		label_9.setText(Messages.ThirdPriority);
		
		third_sort_order = new Combo(composite_3, SWT.READ_ONLY);
		GridData gd_priorityThreeCombo = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_priorityThreeCombo.widthHint = 120;
		third_sort_order.setLayoutData(gd_priorityThreeCombo);
		
		Label label_2 = new Label(composite, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_2.setText(Messages.EvaluationSet + "：");
		
		Composite composite_4 = new Composite(composite, SWT.NONE);
		composite_4.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		GridLayout gl_composite_4 = new GridLayout(2, false);
		gl_composite_4.verticalSpacing = 1;
		gl_composite_4.marginWidth = 0;
		gl_composite_4.marginHeight = 0;
		composite_4.setLayout(gl_composite_4);
		
		removeMaxAndMin_btn = new Button(composite_4, SWT.CHECK);
		removeMaxAndMin_btn.setText(Messages.RemoveHighestLowestScore);
		removeMaxAndMin_btn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		
		recommonedCountText = new Text(composite_4, SWT.CENTER);
		recommonedCountText.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		recommonedCountText.setFont(SWTResourceManager.getFont("微软雅黑", 11, SWT.BOLD));
		GridData gd_recommonedCountText = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_recommonedCountText.widthHint = 30;
		recommonedCountText.setLayoutData(gd_recommonedCountText);
		recommonedCountText.setText("3");
		recommonedCountText.setTextLimit(3);
		
		defaultRecommonedTips = new Label(composite_4, SWT.NONE);
		defaultRecommonedTips.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 2));
		defaultRecommonedTips.setText(MessageFormat.format(Messages.DefaultRecommendNum, 3));
		
		ESepator num_sub_line = new ESepator(composite_4, SWT.NONE);
		GridData gd_label_3 = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_label_3.widthHint = 30;
		num_sub_line.setLayoutData(gd_label_3);
		num_sub_line.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		Label label_11 = new Label(composite, SWT.NONE);
		label_11.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_11.setText(Messages.ShortlistedRules + "：");
		WidgetUtils.setControlVisible(label_11, false);
		
		Composite filterScoreComposite = new Composite(composite, SWT.NONE);
		filterScoreComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		GridLayout gl_filterScoreCompose = new GridLayout(2, false);
		gl_filterScoreCompose.verticalSpacing = 1;
		gl_filterScoreCompose.marginHeight = 0;
		gl_filterScoreCompose.horizontalSpacing = 0;
		gl_filterScoreCompose.marginWidth = 0;
		filterScoreComposite.setLayout(gl_filterScoreCompose);
		WidgetUtils.setControlVisible(filterScoreComposite, false);
		
		evalMinScoreText = new Text(filterScoreComposite, SWT.CENTER);
		evalMinScoreText.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		evalMinScoreText.setFont(SWTResourceManager.getFont("微软雅黑", 11, SWT.BOLD));
		GridData gd_filterScoreText = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_filterScoreText.widthHint = 30;
		evalMinScoreText.setLayoutData(gd_filterScoreText);
		evalMinScoreText.setTextLimit(5);
		
		finalistByScoreTips = new Label(filterScoreComposite, SWT.NONE);
		finalistByScoreTips.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 2));
		finalistByScoreTips.setText("最终得分>=80 分入围！");
		
		ESepator score_sub_line = new ESepator(filterScoreComposite, SWT.NONE);
		GridData gd_label_score = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_label_score.widthHint = 30;
		score_sub_line.setLayoutData(gd_label_score);
		score_sub_line.setBackground(ColorConstants.BORDER_LINE_COLOR);
		
		Label filterTips = new Label(filterScoreComposite, SWT.WRAP);
		filterTips.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		filterTips.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		filterTips.setText(Messages.Tips_shortlistEmpty);
		filterTips.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		
		Label label_5 = new Label(composite, SWT.NONE);
		label_5.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, true, 1, 1));
		label_5.setText(Messages.Note + ": ");
		
		stepMemoText = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		stepMemoText.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		stepMemoText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		stepMemoText.setTextLimit(100);
		
		ESepator but_sep = new ESepator(topContainer, SWT.SEPARATOR | SWT.HORIZONTAL);
		but_sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		but_sep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		Composite composite_1 = new Composite(topContainer, SWT.NONE);
		composite_1.setBackgroundMode(SWT.INHERIT_FORCE);
		composite_1.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_bg.png"));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		GridLayout gl_composite_1 = new GridLayout(2, false);
		composite_1.setLayout(gl_composite_1);
		
		save_btn = new EButton(composite_1, SWT.NONE);
		save_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/save.png"));
		GridData gd_confirmBtn = new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1);
		gd_confirmBtn.widthHint = 90;
		save_btn.setLayoutData(gd_confirmBtn);
		save_btn.setText(Messages.Button_save);
		save_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				saveEvaluationStepAction();
			}
		});
		EButton cancel_btn = new EButton(composite_1, SWT.NONE);
		cancel_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/close.png"));
		cancel_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				cancelAction();
			}
		});
		GridData gd_button = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_button.widthHint = 90;
		cancel_btn.setLayoutData(gd_button);
		cancel_btn.setText(Messages.Button_close);

		init();
		return container;
	}

	/**
	 * @Description: 初始化
	 * @author guoyr
	 */
	private void init(){
		projectRuleService = new ProjectRuleService();
		tenderProjectRuleEntity = projectRuleService.getProjectRule(tenderProjectEntity.getId());
		if(null == evaluationStep){
			evaluationStep = new EvaluationStep();
			evaluationStep.setStepName(Messages.Step_recommoned);
			evaluationStep.setRoleType(EvaluationStepEnum.EXPERT_LEADER);
//			evaluationStep.setRoleType(EvaluationStepEnum.OPERATOR);// 推荐中标候选人默认由项目经理操作
			evaluationStep.setStepStatus(CommonEnum.NOT_START);
			int maxSortNo = evaluationStepService.getMaxEvaluationStepSortNo(tenderProjectEntity.getId());
			evaluationStep.setSortNo(maxSortNo + 1);
			this.setTitle(MessageFormat.format(Messages.Title_create, Messages.Step_step));
		}else {
			this.setTitle(MessageFormat.format(Messages.Title_edit, Messages.Step_step));
			boolean isInEvaluation = false;
			if(ObjectUtils.equals(evaluationStep.getStepStatus(), CommonEnum.COMPLETE)){
				composite.setEnabled(false);
				save_btn.setEnabled(false);
				isInEvaluation = true;
			}else {
				isInEvaluation = evaluationStepService.isInEvaluationStep(tenderProjectEntity.getId(), evaluationStep.getId());
			}
			if(isInEvaluation){
				for(Control control : roleTypeComposite.getChildren()){
					control.setEnabled(false);
				}
			}
		}
		addRecommonedCountListener();
		addShortListRuleListener();
		addComboListener();
		refreshEvaluationStepForm();
		initOrderLevelAction();
		
	}
	
	private void addRecommonedCountListener(){
		IntegerInputListener integerInputListener = new IntegerInputListener();
		integerInputListener.addMinEqCheck(1, null);
		recommonedCountText.addKeyListener(integerInputListener);
		final ModifyListener modifyListener = new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				int value = ObjectUtils.getIntValue(recommonedCountText.getText().trim(), 0);
				if(value < 1){
					value = 0;
					recommonedCountText.removeModifyListener(this);
					recommonedCountText.setText("");
					recommonedCountText.addModifyListener(this);
				}
				defaultRecommonedTips.setText(MessageFormat.format(Messages.DefaultRecommendNum, value > 0 ? value : "___"));
				recommonedCountText.getParent().layout();
			}
		};
		recommonedCountText.addModifyListener(modifyListener);
	}
	
	
	private void addShortListRuleListener(){
		FloatInputListener filterScoreInputListener = new FloatInputListener();
		filterScoreInputListener.addMinEqCheck(0, null);
		filterScoreInputListener.addMaxEqCheck(100, null);
		evalMinScoreText.addKeyListener(filterScoreInputListener);
		
		final ModifyListener bySortListener = new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				int value = ObjectUtils.getIntValue(evalMinScoreText.getText().trim(), 0);
				if(value < 1){
					value = 0;
					evalMinScoreText.removeModifyListener(this);
					evalMinScoreText.setText("");
					evalMinScoreText.addModifyListener(this);
				}
				finalistByScoreTips.setText(MessageFormat.format(Messages.Tips_shortlistByMinScore, value > 0 ? value : "___"));
				evalMinScoreText.getParent().layout();
			}
		};
		evalMinScoreText.addModifyListener(bySortListener);
	}
	
	/**
	 * @Description: 刷新评标步骤表单
	 * @author guoyr
	 */
	private void refreshEvaluationStepForm(){
		stepNameText.setText(ObjectUtils.getObjString(evaluationStep.getStepName()));
		stepMemoText.setText(ObjectUtils.getObjString(evaluationStep.getStepMemo()));
		evalMinScoreText.setText(ObjectUtils.getObjString(evaluationStep.getEvaluationParamByKey(EvaluationStepEnum.EVALUATION_MIN_SCORE, BigDecimal.class)));
		Integer recommendCount = tenderProjectRuleEntity.getRecommendCountNotNull();
		if(null != recommendCount && recommendCount > 0){
			recommonedCountText.setText(recommendCount+"");
		}else {
			recommonedCountText.setText("");
		}
		WidgetUtils.setRadioChecked(roleTypeComposite, ObjectUtils.getObjString(evaluationStep.getRoleType()), "roleType");
		if(null != evaluationStep.getSummaryType() && evaluationStep.getSummaryType().equals(EvaluationStepEnum.SUMMARY_REMOVE_MAX_MIN)){
			removeMaxAndMin_btn.setSelection(true);
		}else {
			removeMaxAndMin_btn.setSelection(false);
		}
	}
	
	/**
	 * @Description: 获得优先级集合
	 * @author guoyr
	 * @return 
	 */
	private List<Map<String, String>> getOrderLevelList(){
		List<Map<String, String>> orderLevelList = new ArrayList<Map<String, String>>();
		
		Map<String, String> orderEmptyMap = new HashMap<String, String>();
		orderEmptyMap.put("value", "");
		orderEmptyMap.put("title", Messages.DefaultPriority);
		orderLevelList.add(orderEmptyMap);
		// 最终报价
		Map<String, String> orderFirstMap = new HashMap<String, String>();
		orderFirstMap.put("value", TenderProjectEnum.EVALUATION_PRICE);
		orderFirstMap.put("title", MessageFormat.format(Messages.PriorityLowToHightFormat, Messages.EvaluationPrice));
		orderLevelList.add(orderFirstMap);
		
		Map<String, String> orderFirstDescMap = new HashMap<String, String>();
		orderFirstDescMap.put("value", TenderProjectEnum.EVALUATION_PRICE_DESC);
		orderFirstDescMap.put("title", MessageFormat.format(Messages.PriorityHighToLowFormat, Messages.EvaluationPrice));
		orderLevelList.add(orderFirstDescMap);
		// 最低报价法只按"最终报价"排名
		if(ObjectUtils.equals(tenderProjectRuleEntity.getEvalMethodType(), TenderProjectEnum.MIN_METHOD)){
			tenderProjectRuleEntity.setOrderSecond(null);
			tenderProjectRuleEntity.setOrderThird(null);
		}else if(ObjectUtils.equals(tenderProjectRuleEntity.getEvalMethodType(), TenderProjectEnum.METHOD_COST_PERFORMANCE)){
			//京能合格法屏蔽
//			tenderProjectRuleEntity.setOrderFirst(TenderProjectEnum.COST_PERFORMANCE);
		}else {
			// 最终得分
			Map<String, String> orderSecondMap = new HashMap<String, String>();
			orderSecondMap.put("value", TenderProjectEnum.EVAL_TOTAL_SCORE);
			orderSecondMap.put("title", MessageFormat.format(Messages.PriorityHighToLowFormat, Messages.FinalScore));
			orderLevelList.add(orderSecondMap);
			
			// 商务技术得分由高到低
			Map<String, String> orderThirdMap = new HashMap<String, String>();
			orderThirdMap.put("value", TenderProjectEnum.DETAIL_SCORE);
			orderThirdMap.put("title", MessageFormat.format(Messages.PriorityHighToLowFormat, Messages.BusAndTecScore));
			orderLevelList.add(orderThirdMap);
			
			// 价格得分
			Map<String, String> orderFourthMap = new HashMap<String, String>();
			orderFourthMap.put("value", TenderProjectEnum.PRICE_SCORE);
			orderFourthMap.put("title", MessageFormat.format(Messages.PriorityHighToLowFormat, Messages.PriceScore));
			orderLevelList.add(orderFourthMap);
		}
		//性价比
		Map<String, String> orderFourthMap = new HashMap<String, String>();
		orderFourthMap.put("value", TenderProjectEnum.COST_PERFORMANCE);
		orderFourthMap.put("title", MessageFormat.format(Messages.PriorityHighToLowFormat, Messages.CostPerformance));
		orderLevelList.add(orderFourthMap);
		return orderLevelList;
	}
	
	/**
	 * @Description: 加载推荐优先级选项
	 * @author guoyr 
	 */
	private void initOrderLevelAction(){
		// 获得优先级集合
		orderLevelList = getOrderLevelList();
		first_sort_order.setEnabled(false);
		second_sort_order.setEnabled(false);
		third_sort_order.setEnabled(false);
		if(null != orderLevelList && orderLevelList.size() > 0){
			first_sort_order.setEnabled(true);
			int index = 0;
			for(Map<String, String> orderMap : orderLevelList){
				String value = orderMap.get("value");
				first_sort_order.add(orderMap.get("title"), index);
				first_sort_order.setData(ObjectUtils.getObjString(index), value);
				index++;
			}
			WidgetUtils.setComboSelection(first_sort_order, tenderProjectRuleEntity.getOrderFirst());
			first_sort_order.notifyListeners(SWT.Selection, null);
		}
	}
	
	/**
	 * @Description: 选中优先级事件
	 * @author guoyr 
	 */
	private void addComboListener(){
		// "优先级一"Combo添加选中监听器
		first_sort_order.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				firstSortOrderSelection();
			}
		});
		// "优先级二"Combo添加选中监听器
		second_sort_order.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				secondSortOrderSelection();
			}
		});
	}
	
	/**
	 * @Description: 设置第一级优先级
	 * @author guoyr 
	 */
	private void firstSortOrderSelection(){
		Object firstOrder = WidgetUtils.getComboSelection(first_sort_order);
		second_sort_order.removeAll();
		if(null != firstOrder && !"".equals(firstOrder.toString())){
			second_sort_order.setEnabled(true);
			int index = 0;
			for(Map<String, String> orderMap : orderLevelList){
				String value = orderMap.get("value");
				if(firstOrder.equals(value)){
					continue;
				}
				second_sort_order.add(orderMap.get("title"), index);
				second_sort_order.setData(ObjectUtils.getObjString(index), value);
				index++;
			}
			WidgetUtils.setComboSelection(second_sort_order, tenderProjectRuleEntity.getOrderSecond());
			secondSortOrderSelection();
		}else {
			second_sort_order.setEnabled(false);
			third_sort_order.removeAll();
			third_sort_order.setEnabled(false);
		}
	}
	
	/**
	 * @Description: 设置第二级优先级
	 * @author guoyr 
	 */
	private void secondSortOrderSelection(){
		Object secondOrder = WidgetUtils.getComboSelection(second_sort_order);
		third_sort_order.removeAll();
		if(null != secondOrder && !"".equals(secondOrder.toString())){
			// 获得"优先级一"Combo选中值
			String selectOrderFirst = ObjectUtils.getObjString(WidgetUtils.getComboSelection(first_sort_order));
			// 获得"优先级二"Combo选中值
			String selectOrderSecond = ObjectUtils.getObjString(WidgetUtils.getComboSelection(second_sort_order));
			
			int index = 0;
			// 遍历"优先级"
			for(Map<String, String> orderMap : orderLevelList){
				String value = orderMap.get("value");
				// 如果是设置第二个Combo，则过滤第一个Combo选中的值；如果是设置第三Combo，则过滤第一个和第二Combo选中的值。
				if(selectOrderFirst.equals(value) || selectOrderSecond.equals(value)){
					continue;
				}
				third_sort_order.add(orderMap.get("title"), index);
				third_sort_order.setData(ObjectUtils.getObjString(index), value);
				index++;
			}
			WidgetUtils.setComboSelection(third_sort_order, tenderProjectRuleEntity.getOrderThird());
			third_sort_order.setEnabled(true);
		}else {
			third_sort_order.setEnabled(false);
		}
	}
	
	/**
	 * @Description: 确定
	 * @author guoyr
	 */
	private void saveEvaluationStepAction(){
		if(save_btn.getEnabled()){
			if(!ValidateUtils.checkInput(stepNameText.getParent())){
				return;
			}
			// 验证评标步骤名称是否已经存在
			if(evaluationStepService.valEvaluationStepNameExist(tenderProjectEntity.getId(), evaluationStep.getId(), stepNameText.getText().trim())){
				MessageDialog.openWarning(getShell(), Messages.Step_step +"["+stepNameText.getText().trim()+"]" + Messages.Tips_AlreadyExistsPleaseRetype);
				stepNameText.setFocus();
				stepNameText.setSelection(stepNameText.getText().length());
				return;
			}
			if(!"".equals(recommonedCountText.getText().trim())){
				tenderProjectRuleEntity.setRecommendCount(Integer.parseInt(recommonedCountText.getText().trim()));
			}else {
				tenderProjectRuleEntity.setRecommendCount(0);
			}
			// 优先级一
			tenderProjectRuleEntity.setOrderFirst(ObjectUtils.getObjString(WidgetUtils.getComboSelection(first_sort_order)));
			// 优先级二
			tenderProjectRuleEntity.setOrderSecond(ObjectUtils.getObjString(WidgetUtils.getComboSelection(second_sort_order)));
			// 优先级三
			tenderProjectRuleEntity.setOrderThird(ObjectUtils.getObjString(WidgetUtils.getComboSelection(third_sort_order)));
			
			if(removeMaxAndMin_btn.getSelection()){
				evaluationStep.setSummaryType(EvaluationStepEnum.SUMMARY_REMOVE_MAX_MIN);
			}else {
				evaluationStep.setSummaryType(EvaluationStepEnum.SUMMARY_ALL);
			}
			projectRuleService.updateAssignProperty(tenderProjectRuleEntity, new String[]{"id","recommendCount", "editRecommonedSort", "orderFirst", "orderSecond", "orderThird"});
			
			evaluationStep.setStepName(stepNameText.getText().trim());
			evaluationStep.setStepType(EvaluationStepEnum.RECOMMONED);//价格评审
			evaluationStep.setStepMemo(stepMemoText.getText().trim());
			evaluationStep.setTenderId(tenderProjectEntity.getId());
			evaluationStep.setRoleType(ObjectUtils.getObjString(WidgetUtils.getRadioVal(roleTypeComposite,"roleType")));
			evaluationStep.setEvaluationParam(EvaluationStepEnum.EVALUATION_MIN_SCORE, ObjectUtils.getObjBigDecimal(evalMinScoreText.getText().trim()));
			evaluationStep = evaluationStepService.saveEvaluationStep(evaluationStep);
			if(null != evaluationStep){
				if(null != callBack){
					callBack.callBackObject(evaluationStep);
				}
			}else {
				MessageDialog.openWarning(getShell(), stepNameText.getText()+Messages.Tips_saveFail);
			}
			this.getShell().close();
		}
	}
	
	/**
	 * @Description: 取消
	 * @author guoyr
	 */
	private void cancelAction(){
		this.getShell().close();
	}

	
	public CallBack getCallBack() {
		return callBack;
	}

	public void setCallBack(CallBack callBack) {
		this.callBack = callBack;
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(600, 450);
	}
}
