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

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.nebula.widgets.grid.Grid;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.nebula.widgets.grid.GridItem;
import org.eclipse.nebula.widgets.grid.listener.GridSelectionListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.listener.FloatInputListener;
import org.eclipse.swt.core.listener.VerifyRequiredListener;
import org.eclipse.swt.core.utils.ValidateUtils;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.EDialog;
import org.eclipse.swt.core.widgets.ELabel;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
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.dialog.SelectFactorDialog;
import com.gx.obe.common.widget.listener.SelectFactorListener;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.components.core.enumeration.EvaluationFactorEnum;
import com.gx.obe.components.core.enumeration.EvaluationStepEnum;
import com.gx.obe.http.json.JsonUtil;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.service.evaluation.EvaluationFactorService;
import com.gx.obe.web.service.evaluation.EvaluationStepService;
import com.gx.obe.web.vo.FixedFactorItemVo;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

public class FixedFactorDialog extends EDialog {

	private ELink addObjectiveScoreBtn;
	private ELink updateObjectiveScoreBtn;
	private ELink delObjectiveScoreBtn;
	private Grid objectiveItemGrid;
	private EvaluationStep evaluationStep;
	private StackLayout stackLayout = new StackLayout();
	private Composite fixedFactorComposite;
	private Composite fixedTableComposite;
	private Composite fixedFormComposite;
	private EvaluationFactorService evaluationFactorService;
	private EvaluationStepService evaluationStepService;
	private EButton selectdFatorBtn;
	private StyledText evaluation_factor_memo;
	private Text itemMinScoreText;
	private Text itemMaxScoreText;
	private Text factorMinScoreText;
	private Text factorMaxScoreText;
	private Combo itemMinScoreCombo;
	private Combo itemMaxScoreCombo;
	private Combo factorMinScoreCombo;
	private Combo factorMaxScoreCombo;
	private List<FixedFactorItemVo> fixedFactorItemVoList = null;
	private FixedFactorItemVo curFixedFactorItemVo;
	private EvaluationFactor fixFactor;
	private boolean isNewItem = false;
	private Text gradeText;

	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public FixedFactorDialog(Shell parentShell, EvaluationStep evaluationStep) {
		super(parentShell);
		this.setTitle(ObjectUtils.getObjString(evaluationStep.getStepName()) + " - "+Messages.FixedGradeRangeRules);
		this.evaluationStep = evaluationStep;
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(1, false));
		
		Composite composite = new Composite(container, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		evaluation_factor_memo = new StyledText(composite, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.READ_ONLY);
		evaluation_factor_memo.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		evaluation_factor_memo.setText("");
		GridData gd_evaluation_factor_memo = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		gd_evaluation_factor_memo.heightHint = 58;
		gd_evaluation_factor_memo.widthHint = 300;
		evaluation_factor_memo.setLayoutData(gd_evaluation_factor_memo);
		
		selectdFatorBtn = new EButton(composite, SWT.NONE | EButton.CSS_BLUE);
		selectdFatorBtn.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, false, true, 1, 1));
		selectdFatorBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				selectFixedFactorAction();
			}
		});
		selectdFatorBtn.setText(Messages.Button_select);
		
		fixedFactorComposite = new Composite(container, SWT.NONE);
		fixedFactorComposite.setBackground(ColorConstants.BORDER_LINE_COLOR);
		fixedFactorComposite.setLayout(stackLayout);
		GridData gd_objectiveScoreComposite = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);
		gd_objectiveScoreComposite.heightHint = 180;
		fixedFactorComposite.setLayoutData(gd_objectiveScoreComposite);
		
		fixedTableComposite = new Composite(fixedFactorComposite, SWT.NONE);
		fixedTableComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		fixedTableComposite.setBackground(ColorConstants.BORDER_LINE_COLOR);
		GridLayout gl_composite_7 = new GridLayout(1, false);
		gl_composite_7.verticalSpacing = 1;
		gl_composite_7.horizontalSpacing = 0;
		gl_composite_7.marginHeight = 1;
		gl_composite_7.marginWidth = 1;
		fixedTableComposite.setLayout(gl_composite_7);
		
		Composite composite_8 = new Composite(fixedTableComposite, SWT.NONE);
		composite_8.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite_8.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_composite_8 = new GridLayout(2, false);
		composite_8.setLayout(gl_composite_8);
		
		Label label_18 = new Label(composite_8, SWT.NONE);
		label_18.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		label_18.setText(Messages.Tips_addGradeLevelHighToLow);
		label_18.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		label_18.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		
		Composite composite_10 = new Composite(composite_8, SWT.NONE);
		GridLayout gl_composite_10 = new GridLayout(3, false);
		gl_composite_10.marginHeight = 0;
		gl_composite_10.marginWidth = 0;
		composite_10.setLayout(gl_composite_10);
		composite_10.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		addObjectiveScoreBtn = new ELink(composite_10, SWT.NONE);
		addObjectiveScoreBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/add.png"));
		addObjectiveScoreBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				addItemAction();
			}
		});
		addObjectiveScoreBtn.setText(Messages.Button_add);
		
		updateObjectiveScoreBtn = new ELink(composite_10, SWT.NONE);
		updateObjectiveScoreBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/edit.png"));
		updateObjectiveScoreBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				updateItemAction();
			}
		});
		updateObjectiveScoreBtn.setText(Messages.Button_modify);
		
		delObjectiveScoreBtn = new ELink(composite_10, SWT.NONE);
		delObjectiveScoreBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/delete.png"));
		delObjectiveScoreBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				delItemAction();
			}
		});
		delObjectiveScoreBtn.setText(Messages.Button_delete);
		
		Composite composite_1 = new Composite(fixedTableComposite, SWT.NONE);
		composite_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite_1.setLayout(new FillLayout(SWT.HORIZONTAL));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		
		objectiveItemGrid = new Grid(composite_1, SWT.H_SCROLL | SWT.V_SCROLL);
		objectiveItemGrid.setHeaderVisible(true);
		
		GridColumn gridColumn_1 = new GridColumn(objectiveItemGrid, SWT.CENTER);
		gridColumn_1.setText(Messages.Num);
		gridColumn_1.setWidth(40);
		gridColumn_1.setMaxmumWidth(50);
		
		GridColumn gridColumn_2 = new GridColumn(objectiveItemGrid, SWT.CENTER);
		gridColumn_2.setText(Messages.FixedGradeName);
		gridColumn_2.setWidth(80);
		
		GridColumn gridColumn_3 = new GridColumn(objectiveItemGrid, SWT.CENTER);
		gridColumn_3.setText(Messages.FixedGradeRange);
		gridColumn_3.setWidth(100);
		
		GridColumn gridColumn_4 = new GridColumn(objectiveItemGrid, SWT.CENTER);
		gridColumn_4.setText(Messages.FixedGradeScoreRange);
		gridColumn_4.setWidth(80);
		
		fixedFormComposite = new Composite(fixedFactorComposite, SWT.NONE);
		fixedFormComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		fixedFormComposite.setLayout(new GridLayout(1, false));
		
		Composite composite_2 = new Composite(fixedFormComposite, SWT.NONE);
		composite_2.setLayout(new GridLayout(2, false));
		composite_2.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, true, 1, 1));
		
		Label tips2 = new Label(composite_2, SWT.NONE);
		tips2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		tips2.setText(Messages.Tips_addGradeLevelHighToLow);
		tips2.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		tips2.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		
		Label gradeLabel = new Label(composite_2, SWT.NONE);
		gradeLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		gradeLabel.setText(Messages.FixedGradeName+"：");
		
		gradeText = new Text(composite_2, SWT.BORDER);
		gradeText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		gradeText.addVerifyListener(new VerifyRequiredListener(gradeText, MessageFormat.format(Messages.Tips_required, Messages.FixedGradeName)));
		gradeText.setText("");
		gradeText.setTextLimit(20);
		
		Label lblNewLabel = new Label(composite_2, SWT.NONE);
		lblNewLabel.setText(Messages.FixedGradeRange + "：");
		
		Composite composite_3 = new Composite(composite_2, SWT.NONE);
		composite_3.setLayout(new GridLayout(3, false));
		
		itemMinScoreCombo = new Combo(composite_3, SWT.NONE | SWT.READ_ONLY | SWT.CENTER);
		GridData gd_combo = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_combo.widthHint = 50;
		itemMinScoreCombo.setLayoutData(gd_combo);
		
		itemMinScoreText = new Text(composite_3, SWT.BORDER | SWT.CENTER);
		itemMinScoreText.addVerifyListener(new VerifyRequiredListener(itemMinScoreText, MessageFormat.format(Messages.Tips_pleaseEnterTheValid, Messages.FixedGradeRange)));
		itemMinScoreText.setText("");
		GridData gd_text = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_text.widthHint = 60;
		itemMinScoreText.setLayoutData(gd_text);
		
		Label lblNewLabel_4 = new Label(composite_3, SWT.NONE);
		lblNewLabel_4.setText(Messages.Points);
		
		itemMaxScoreCombo = new Combo(composite_3, SWT.NONE | SWT.READ_ONLY);
		GridData gd_combo_1 = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_combo_1.widthHint = 50;
		itemMaxScoreCombo.setLayoutData(gd_combo_1);
		
		itemMaxScoreText = new Text(composite_3, SWT.BORDER | SWT.CENTER);
		itemMaxScoreText.addVerifyListener(new VerifyRequiredListener(itemMaxScoreText, MessageFormat.format(Messages.Tips_pleaseEnterTheValid, Messages.FixedGradeRange)));
		itemMaxScoreText.setText("");
		GridData gd_text_1 = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_text_1.widthHint = 60;
		itemMaxScoreText.setLayoutData(gd_text_1);
		
		Label lblNewLabel_5 = new Label(composite_3, SWT.NONE);
		lblNewLabel_5.setText(Messages.Points);
		
		Label lblNewLabel_1 = new Label(composite_2, SWT.NONE);
		lblNewLabel_1.setText(Messages.FixedGradeScoreRange + "：");
		
		Composite composite_4 = new Composite(composite_2, SWT.NONE);
		composite_4.setLayout(new GridLayout(3, false));
		
		factorMinScoreCombo = new Combo(composite_4, SWT.NONE | SWT.READ_ONLY | SWT.CENTER);
		GridData gd_factorMinScoreCombo = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_factorMinScoreCombo.widthHint = 50;
		factorMinScoreCombo.setLayoutData(gd_factorMinScoreCombo);
		
		factorMinScoreText = new Text(composite_4, SWT.BORDER | SWT.CENTER);
		factorMinScoreText.addVerifyListener(new VerifyRequiredListener(itemMaxScoreText, MessageFormat.format(Messages.Tips_pleaseEnterTheValid, Messages.FixedGradeScoreRange)));
		factorMinScoreText.setText("");
		GridData gd_text_2 = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_text_2.widthHint = 60;
		factorMinScoreText.setLayoutData(gd_text_2);
		
		Label factorMinScoreLabel = new Label(composite_4, SWT.NONE);
		factorMinScoreLabel.setText(Messages.Points);
		
		factorMaxScoreCombo = new Combo(composite_4, SWT.NONE | SWT.READ_ONLY | SWT.CENTER);
		GridData gd_factorMaxScoreCombo = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_factorMaxScoreCombo.widthHint = 50;
		factorMaxScoreCombo.setLayoutData(gd_factorMaxScoreCombo);
		
		factorMaxScoreText = new Text(composite_4, SWT.BORDER | SWT.CENTER);
		factorMaxScoreText.setText("");
		factorMaxScoreText.addVerifyListener(new VerifyRequiredListener(itemMaxScoreText, MessageFormat.format(Messages.Tips_pleaseEnterTheValid, Messages.FixedGradeScoreRange)));
		GridData gd_text_3 = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_text_3.widthHint = 60;
		factorMaxScoreText.setLayoutData(gd_text_3);
		
		Label factorMaxScoreLabel = new Label(composite_4, SWT.NONE);
		factorMaxScoreLabel.setText(Messages.Points);
		
		Composite composite_5 = new Composite(fixedFormComposite, SWT.NONE);
		composite_5.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false, 1, 1));
		composite_5.setLayout(new GridLayout(2, false));
		
		EButton saveButton = new EButton(composite_5, SWT.NONE | EButton.CSS_BLUE);
		saveButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		saveButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				saveFixedFactorItemAction();
			}
		});
		saveButton.setText(Messages.Button_save);
		
		EButton btnNewButton = new EButton(composite_5, SWT.NONE | EButton.CSS_BLUE);
		btnNewButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				backAction();
			}
		});
		btnNewButton.setText(Messages.Button_return);
		
		init();
		return container;
	}
	
	private void init() {
		evaluationFactorService = new EvaluationFactorService();
		evaluationStepService = new EvaluationStepService();
		
		itemMinScoreCombo.add(">", 0);
		itemMinScoreCombo.add("≥", 1);
		itemMaxScoreCombo.add("<", 0);
		itemMaxScoreCombo.add("≤", 1);
		
		factorMinScoreCombo.add(">", 0);
		factorMinScoreCombo.add("≥", 1);
		factorMaxScoreCombo.add("<", 0);
		factorMaxScoreCombo.add("≤", 1);
		
		FloatInputListener itemRangeListener = new FloatInputListener();
		itemRangeListener.addMinCheck(0, Messages.Tips_pleaseEnterValidRange);
		itemRangeListener.addMaxCheck(100, Messages.Tips_pleaseEnterValidRange);
		itemMinScoreText.addKeyListener(itemRangeListener);
		itemMaxScoreText.addKeyListener(itemRangeListener);
		
		String PARAM_FIXED_FACTOR_ID = evaluationStep.getEvaluationParamByKey(EvaluationStepEnum.PARAM_FIXED_FACTOR_ID);
		String PARAM_FIXED_FACTOR_CODE = evaluationStep.getEvaluationParamByKey(EvaluationStepEnum.PARAM_FIXED_FACTOR_CODE);
		if(StringUtils.hasLength(PARAM_FIXED_FACTOR_CODE)) {
			fixFactor = evaluationFactorService.getFactorByFactorCode(evaluationStep.getTenderId(), PARAM_FIXED_FACTOR_CODE);
		}
		if(null == fixFactor && StringUtils.hasLength(PARAM_FIXED_FACTOR_ID)) {
			fixFactor = evaluationFactorService.findById(PARAM_FIXED_FACTOR_ID);
		}
		refreshFixedFactor(fixFactor);
		// 客观范围打分
		objectiveItemGrid.setAutoWidth(true);
		objectiveItemGrid.setAutoHeight(true);
		objectiveItemGrid.setItemHeight(20);
		objectiveItemGrid.addListener(SWT.Selection, new GridSelectionListener(){
			@Override
			public void leftMouseButtonDown(GridItem item) {
				selectItemAction();
			}
			@Override
			public void rightMouseButtonDown(GridItem item) {
			}
		});
		if(CommonEnum.COMPLETE.equals(evaluationStep.getStepStatus())){
			addObjectiveScoreBtn.setEnabled(false);
		}
		showItemTable();
	}
	
	/**
	 * @Description: 刷新定档表
	 * @author guoyr 
	 */
	private void showItemTable() {
		stackLayout.topControl = fixedTableComposite;
		fixedFactorComposite.layout();
		objectiveItemGrid.resizeColumnWidth();
		refreshItemGrid();
	}
	
	private void refreshItemGrid() {
		
		String PARAM_FIXED_FACTOR_LIST = evaluationStep.getEvaluationParamByKey(EvaluationStepEnum.PARAM_FIXED_FACTOR_LIST);
		if(StringUtils.hasLength(PARAM_FIXED_FACTOR_LIST)) {
			fixedFactorItemVoList = JsonUtil.strToList(PARAM_FIXED_FACTOR_LIST, FixedFactorItemVo.class);
		}
		if(null != fixedFactorItemVoList) {
			objectiveItemGrid.removeAll();
			int index = 1;
			for(FixedFactorItemVo vo : fixedFactorItemVoList) {
				GridItem gridItem  = new GridItem(objectiveItemGrid, SWT.NONE);
				gridItem.setText(0, index +"");
				gridItem.setText(1, ObjectUtils.getObjString(vo.getName()));
				gridItem.setText(2, vo.getItemMinScore() + (vo.isIncludeMin() ? " ≤" : " <") + " "+vo.getName()+" " + (vo.isIncludeMax() ? "≤ " : "< ") + vo.getItemMaxScore());
				gridItem.setText(3, (vo.isIncludeFactorMinScore() ? "[" : "(") + vo.getFactorMinScore() + " - "+vo.getFactorMaxScore() + (vo.isIncludeFactorMaxScore() ? "]" : ")")+"分");
				index ++;
			}
		}else {
			fixedFactorItemVoList = new ArrayList<FixedFactorItemVo>();
		}
		selectItemAction();
	}
	
	/**
	 * @Description: 刷新定档表单
	 * @author guoyr 
	 */
	private void refreshItemForm(int index) {
		if(null == fixFactor) {
			MessageDialog.openShortMessage("请先选择等定档的指标！");
			return;
		}
		stackLayout.topControl = fixedFormComposite;
		fixedFactorComposite.layout();
		if(index >= 0 && index < fixedFactorItemVoList.size()) {
			isNewItem = false;
			curFixedFactorItemVo = fixedFactorItemVoList.get(index);
			if(index == 0) {
				curFixedFactorItemVo.setIncludeMax(true);
				itemMaxScoreCombo.setEnabled(false);
			}
		}else {
			isNewItem = true;
			curFixedFactorItemVo = new FixedFactorItemVo();
			if(fixedFactorItemVoList.size() == 0) {
				curFixedFactorItemVo.setIncludeMax(true);
				itemMaxScoreCombo.setEnabled(false);
			}else {
				FixedFactorItemVo  preFixedFactorItemVo = fixedFactorItemVoList.get(fixedFactorItemVoList.size() -1);
				itemMaxScoreCombo.setEnabled(false);
				itemMaxScoreText.setEnabled(false);
				curFixedFactorItemVo.setIncludeMax(!preFixedFactorItemVo.isIncludeMin());
				curFixedFactorItemVo.setItemMaxScore(preFixedFactorItemVo.getItemMinScore());
			}
		}
		gradeText.setText(ObjectUtils.getObjString(curFixedFactorItemVo.getName()));
		itemMinScoreText.setText(ObjectUtils.getObjString(curFixedFactorItemVo.getItemMinScore()));
		itemMaxScoreText.setText(ObjectUtils.getObjString(curFixedFactorItemVo.getItemMaxScore()));
		factorMinScoreText.setText(ObjectUtils.getObjString(curFixedFactorItemVo.getFactorMinScore()));
		factorMaxScoreText.setText(ObjectUtils.getObjString(curFixedFactorItemVo.getFactorMaxScore()));
		itemMinScoreCombo.select(curFixedFactorItemVo.isIncludeMin() ? 1 : 0);
		itemMaxScoreCombo.select(curFixedFactorItemVo.isIncludeMax() ? 1 : 0);
		factorMinScoreCombo.select(curFixedFactorItemVo.isIncludeFactorMinScore() ? 1 : 0);
		factorMaxScoreCombo.select(curFixedFactorItemVo.isIncludeFactorMaxScore() ? 1 : 0);
		
	}
	
	/**
	 * @Description: 选择定档指标
	 * @author guoyr 
	 */
	private void selectFixedFactorAction() {
		final List<EvaluationFactor> evaluationFactorList = evaluationFactorService.getAllEvaluationFactorList(evaluationStep.getTenderId(), EvaluationFactorEnum.AUDIT_SCORE);
		List<EvaluationFactor> selectFactorList = null;
		if(null != fixFactor) {
			selectFactorList = new ArrayList<EvaluationFactor>();
			selectFactorList.add(fixFactor);
		}
		
		SelectFactorDialog dialog = new SelectFactorDialog(getShell(), evaluationFactorList, selectFactorList, false, new SelectFactorListener() {
			public void setEvaluationFactor(EvaluationFactor evaluationFactor) {
				evaluationStep.setEvaluationParam(EvaluationStepEnum.PARAM_FIXED_FACTOR_ID, evaluationFactor.getId());
				evaluationStep.setEvaluationParam(EvaluationStepEnum.PARAM_FIXED_FACTOR_CODE, evaluationFactor.getFactorCode());
				if(null != evaluationStep.getId()) {
					if(evaluationStepService.updateAssignProperty(evaluationStep, new String[] {"params"})) {
						refreshFixedFactor(evaluationFactor);
					}
				}else {
					refreshFixedFactor(evaluationFactor);
				}
			}
		});
		dialog.open();
	}
	
	/**
	 * @Description: 保存定档项
	 * @author guoyr 
	 */
	private void saveFixedFactorItemAction() {
		
		if (!ValidateUtils.checkInput(fixedFormComposite)) {
			return;
		}
		BigDecimal itemMinScore = ObjectUtils.getObjBigDecimal(itemMinScoreText.getText().trim(), 2);
		BigDecimal itemMaxScore = ObjectUtils.getObjBigDecimal(itemMaxScoreText.getText().trim(), 2);
		if(itemMinScore.compareTo(itemMaxScore) >= 0) {
			MessageDialog.openWarning(getShell(), "请输入有效的等级得分范围！");
			return;
		}
		BigDecimal factorMinScore = ObjectUtils.getObjBigDecimal(factorMinScoreText.getText().trim(), 2);
		BigDecimal factorMaxScore = ObjectUtils.getObjBigDecimal(factorMaxScoreText.getText().trim(), 2);
		if(null == fixFactor.getFactorMaxScore()) {
			fixFactor.setFactorMaxScore(fixFactor.getFactorScore());
		}
		if(factorMinScore.compareTo(factorMaxScore) >= 0 || factorMaxScore.compareTo(fixFactor.getFactorMaxScore()) > 0) {
			MessageDialog.openWarning(getShell(), "请输入有效的定档得分范围！");
			return;
		}
		int preIndex = -1;
		if(isNewItem) {
			preIndex = fixedFactorItemVoList.size() -1;
			for(FixedFactorItemVo vo : fixedFactorItemVoList) {
				if(vo.getName().equals(gradeText.getText().trim())) {
					MessageDialog.openWarning(getShell(), "当前等级名称已经存在！");
					gradeText.setFocus();
					return;
				}
			}
		}else {
			int curIndex = fixedFactorItemVoList.indexOf(curFixedFactorItemVo);
			for(int i = 0; i < fixedFactorItemVoList.size(); i++) {
				if(i != curIndex) {
					FixedFactorItemVo vo = fixedFactorItemVoList.get(i);
					if(vo.getName().equals(gradeText.getText().trim())) {
						MessageDialog.openWarning(getShell(), "当前等级名称已经存在！");
						gradeText.setFocus();
						return;
					}
				}
			}
			if(curIndex > 0) {
				preIndex = curIndex -1;
			}
		}
		if(preIndex >= 0) {
			FixedFactorItemVo  preFixedFactorItemVo = fixedFactorItemVoList.get(preIndex);
			boolean valItemRangeFlag = true;
			if(preFixedFactorItemVo.isIncludeMin() == (itemMaxScoreCombo.getSelectionIndex() == 1)){
				valItemRangeFlag = false;
			}
			if(!preFixedFactorItemVo.isIncludeMin() == (itemMaxScoreCombo.getSelectionIndex() == 0)){
				valItemRangeFlag = false;
			}
			if(itemMaxScore.compareTo(preFixedFactorItemVo.getItemMinScore()) > 0) {
				valItemRangeFlag = false;
			}
			if(!valItemRangeFlag) {
				MessageDialog.openWarning(getShell(), "请输入有效的等级得分范围！");
				return;
			}
		}
		curFixedFactorItemVo.setName(gradeText.getText().trim());
		curFixedFactorItemVo.setItemMinScore(itemMinScore);
		curFixedFactorItemVo.setIncludeMin(itemMinScoreCombo.getSelectionIndex() == 1);
		curFixedFactorItemVo.setIncludeMax(itemMaxScoreCombo.getSelectionIndex() == 1);
		curFixedFactorItemVo.setItemMaxScore(itemMaxScore);
		curFixedFactorItemVo.setIncludeFactorMinScore(factorMinScoreCombo.getSelectionIndex() == 1);
		curFixedFactorItemVo.setIncludeFactorMaxScore(factorMaxScoreCombo.getSelectionIndex() == 1);
		curFixedFactorItemVo.setFactorMinScore(factorMinScore);
		curFixedFactorItemVo.setFactorMaxScore(factorMaxScore);
		if(isNewItem) {
			fixedFactorItemVoList.add(curFixedFactorItemVo);
		}
		
		String PARAM_FIXED_FACTOR_LIST = JsonUtil.jsonObjectToStr(fixedFactorItemVoList);
		evaluationStep.setEvaluationParam(EvaluationStepEnum.PARAM_FIXED_FACTOR_LIST, PARAM_FIXED_FACTOR_LIST);
		if(null != evaluationStep.getId()) {
			if(evaluationStepService.updateAssignProperty(evaluationStep, new String[] {"params"})) {
				MessageDialog.openShortMessage(Messages.Tips_saveSuccess);
			}else {
				MessageDialog.openWarning(getShell(), Messages.Tips_saveFail);
			}
		}
		showItemTable();
	}
	
	/**
	 * @Description: 返回定档列表
	 * @author guoyr 
	 */
	private void backAction() {
		showItemTable();
	}
	
	/**
	 * @Description: 选择定档项
	 * @author guoyr 
	 */
	private void selectItemAction() {
		updateObjectiveScoreBtn.setEnabled(false);
		delObjectiveScoreBtn.setEnabled(false);
		if(objectiveItemGrid.getSelectionCount() > 0 && !CommonEnum.COMPLETE.equals(evaluationStep.getStepStatus())){
			updateObjectiveScoreBtn.setEnabled(true);
			delObjectiveScoreBtn.setEnabled(true);
		}
	}

	private void addItemAction() {
		refreshItemForm(-1);
	}
	private void updateItemAction() {
		
		if(objectiveItemGrid.getSelectionCount() > 0){
			refreshItemForm(objectiveItemGrid.getSelectionIndex());
		}
	}
	private void delItemAction() {
		if(objectiveItemGrid.getSelectionCount() > 0){
			int selectIndex = objectiveItemGrid.getSelectionIndex();
			fixedFactorItemVoList.remove(selectIndex);
			if(objectiveItemGrid.getItemCount() > selectIndex) {
				objectiveItemGrid.setSelection(selectIndex);
			}else if(objectiveItemGrid.getItemCount() <= selectIndex){
				objectiveItemGrid.setSelection(selectIndex -1);
			}
			String PARAM_FIXED_FACTOR_LIST = JsonUtil.jsonObjectToStr(fixedFactorItemVoList);
			evaluationStep.setEvaluationParam(EvaluationStepEnum.PARAM_FIXED_FACTOR_LIST, PARAM_FIXED_FACTOR_LIST);
			if(null != evaluationStep.getId()) {
				if(evaluationStepService.updateAssignProperty(evaluationStep, new String[] {"params"})) {
					MessageDialog.openShortMessage(Messages.Tips_deleteSuccess);
				}else {
					MessageDialog.openWarning(getShell(), Messages.Tips_deleteFail);
				}
			}
			refreshItemGrid();
		}
	}
	
	/**
	 * @Description: 评审表格点击事件
	 * @author guoyr
	 * @param row
	 * @param col
	 */
	private void refreshFixedFactor(EvaluationFactor evaluationFactor){
		this.fixFactor = evaluationFactor;
		if(null != evaluationFactor){
			StringBuilder evaluationFactorMemo = new StringBuilder();
			evaluationFactorMemo.append(Messages.ScoreRange + "：[").append(NumberFormat.getInstance().format(evaluationFactor.getFactorMinScore())).append("-").append(NumberFormat.getInstance().format(evaluationFactor.getFactorMaxScore())).append(Messages.Points + "]");
			if(null != evaluationFactor.getFactorWeight()){
				evaluationFactorMemo.append("("+Messages.Weight+"").append(BigDecimalUtils.round(evaluationFactor.getFactorWeight(), 2).toString()).append("%)");
			}
			StyleRange styleScoreRange = new StyleRange(); 
			styleScoreRange.start = Messages.ScoreRange.length() + 1; 
			styleScoreRange.length = evaluationFactorMemo.length() - (Messages.ScoreRange.length() + 1); 
			styleScoreRange.foreground = SWTResourceManager.getColor(77, 163, 19); 
			styleScoreRange.fontStyle = SWT.BOLD; 
			evaluationFactorMemo.append("   ");
			
			int startIndex = evaluationFactorMemo.length();
			StyleRange factorNameTitleStyleRange = getTitleStyleRange(startIndex, Messages.EvalFactor.length() + 1);
			
			StyleRange styleFactorNameRange = new StyleRange(); 
			styleFactorNameRange.start = startIndex + Messages.EvalFactor.length() + 1; 
			styleFactorNameRange.length = evaluationFactor.getFactorName().length(); 
			styleFactorNameRange.foreground = SWTResourceManager.getColor(20, 20, 20); 
			styleFactorNameRange.fontStyle = SWT.NORMAL; 
			
			evaluationFactorMemo.append(Messages.EvalFactor + "：").append(evaluationFactor.getFactorName());
			
			StyleRange factorMemoTitleStyleRange = null;
			StyleRange styleFactorMemoRange = null;
			if(null != evaluationFactor.getMemo() && evaluationFactor.getMemo().length() > 0){
				evaluationFactorMemo.append("\r\n");
				startIndex = evaluationFactorMemo.length();
				factorMemoTitleStyleRange = getTitleStyleRange(startIndex, Messages.EvalExplain.length() + 1);
				evaluationFactorMemo.append(Messages.EvalExplain + "：").append(evaluationFactor.getMemo());
				styleFactorMemoRange = new StyleRange(); 
				styleFactorMemoRange.start = startIndex + Messages.EvalExplain.length() + 1; 
				styleFactorMemoRange.length = evaluationFactor.getMemo().length(); 
				styleFactorMemoRange.foreground = SWTResourceManager.getColor(100, 100, 100); 
				styleFactorMemoRange.fontStyle = SWT.NORMAL; 
			}
			evaluation_factor_memo.setText(evaluationFactorMemo.toString());
			evaluation_factor_memo.setStyleRange(getTitleStyleRange(0, Messages.EvalExplain.length() + 1));
			evaluation_factor_memo.setStyleRange(styleScoreRange);
			evaluation_factor_memo.setStyleRange(factorNameTitleStyleRange);
			evaluation_factor_memo.setStyleRange(styleFactorNameRange);
			if(null != factorMemoTitleStyleRange){
				evaluation_factor_memo.setStyleRange(factorMemoTitleStyleRange);
				evaluation_factor_memo.setStyleRange(styleFactorMemoRange);
			}
		}else {
			evaluation_factor_memo.setText(MessageFormat.format(Messages.Tips_pleaseSelect, Messages.EvalFactor));
		}
	}
	
	private StyleRange getTitleStyleRange(int start, int length){
		StyleRange styleRange = new StyleRange(); 
		styleRange.start = start; 
		styleRange.length = length; 
//		styleRange.foreground = SWTResourceManager.getColor(SWT.COLOR_WHITE); 
		styleRange.foreground = SWTResourceManager.getColor(SWT.COLOR_BLUE);
		styleRange.fontStyle = SWT.BOLD; 
		return styleRange;
	}
	
	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(600, 360);
	}
}
