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

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.GridCheckedListener;
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.IntegerInputListener;
import org.eclipse.swt.core.listener.TextHintListener;
import org.eclipse.swt.core.listener.VerifyRequiredListener;
import org.eclipse.swt.core.utils.GridUtils;
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.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
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.EvaluationFactorEnum;
import com.gx.obe.components.core.enumeration.EvaluationStepEnum;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.util.utils.LogUtils;
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.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.EvaluationFactorService;
import com.gx.obe.web.service.evaluation.EvaluationStepFactorService;
import com.gx.obe.web.service.evaluation.EvaluationStepService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 
 * @author guoyr
 */
public class PriceScoreEvaluationStepDialog extends EDialog {
	
	private Text stepNameText;
	private Composite roleTypeComposite;
	private Composite composite;
	private Composite finalistRuleCompose;
	private Grid factorGrid;
	private EButton save_btn;
	private GridColumn gridColumn;
	private ImageResolver imageResolver = new ImageResolver();
	private Image menuOpenImage;
	private Image menuImage;
	private EvaluationFactorService evaluationFactorService = new EvaluationFactorService();
	private EvaluationStepService evaluationStepService = new EvaluationStepService();
	private EvaluationStepFactorService evaluationStepFactorService;
	private ProjectRuleService projectRuleService ;
	private TenderProjectEntity tenderProjectEntity;
	private TenderProjectRuleEntity tenderProjectRuleEntity;
	private EvaluationStep evaluationStep;
	private List<Object> factorList;
	private Text stepMemoText;
//	private List<String> stepExpertUserIdList ;
	private CallBack callBack;
	private Text finalistNumText;
	private boolean isInEvaluation = false;// 是否在评审中
	private Button all_expert;
	private Button operator_expert;
	private Button correctEvaluationPrice_btn;
	private Button confirmEvaluationPrice_btn;
	private Label scoreAndWeight;
	private GridItem topGridItem;
	private Composite evaluationSetComposite;
	private Button changePriceScoreBtn;
	private Button changeBasePriceBtn;
	private Label finalistBySortNoTips;
	private boolean hasNoCheckedFactor = false;
	private Tips tips;
	
	{
		LogUtils.logClass(PriceScoreEvaluationStepDialog.class);
	}
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public PriceScoreEvaluationStepDialog(Shell parentShell, TenderProjectEntity tenderProjectEntity, TenderProjectRuleEntity tenderProjectRuleEntity, EvaluationStep evaluationStep) {
		super(parentShell);
		setTitle(Messages.Step_step);
		this.tenderProjectEntity = tenderProjectEntity;
		this.tenderProjectRuleEntity = tenderProjectRuleEntity;
		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();
//				if(null != stepExpertUserIdList){
//					stepExpertUserIdList.clear();
//				}
			}
		});
		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 tips = new Tips(container, SWT.WRAP);
		tips.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		tips.setText(MessageFormat.format(Messages.Tips_factorStep, Messages.Factor_price));
		tips.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, EvaluationStepEnum.getEvaluationStepIco(EvaluationStepEnum.PRICE)));
		tips.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(2, 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.BOTTOM, 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(Messages.Step_price);
		
		scoreAndWeight = new Label(composite_2, SWT.NONE);
		scoreAndWeight.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		scoreAndWeight.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		scoreAndWeight.setText("");
		
		
		ESepator label = new ESepator(composite_2, SWT.NONE);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 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.TOP, false, false, 1, 1));
		label_1.setText(Messages.EvalExpert + "：");
		
		Composite composite_7 = new Composite(composite, SWT.NONE);
		composite_7.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_composite_7 = new GridLayout(1, false);
		gl_composite_7.marginHeight = 0;
		gl_composite_7.marginWidth = 0;
		gl_composite_7.verticalSpacing = 0;
		composite_7.setLayout(gl_composite_7);
		
		roleTypeComposite = new Composite(composite_7, SWT.NONE);
		roleTypeComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_roleTypeComposite = new GridLayout(3, false);
		gl_roleTypeComposite.marginHeight = 0;
		gl_roleTypeComposite.marginWidth = 0;
		roleTypeComposite.setLayout(gl_roleTypeComposite);
		
		all_expert = new Button(roleTypeComposite, SWT.RADIO);
		all_expert.setSelection(true);
		all_expert.setText(Messages.ExpertLeader);
		all_expert.setData("roleType", EvaluationStepEnum.EXPERT_LEADER);
		
		operator_expert = new Button(roleTypeComposite, SWT.RADIO);
		operator_expert.setData("roleType", EvaluationStepEnum.OPERATOR);
		operator_expert.setText(Messages.ProjectManager);
		
		Label label_2 = new Label(composite, SWT.NONE);
		label_2.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_2.setText(Messages.ShortlistedRules + "：");
		
		finalistRuleCompose = new Composite(composite, SWT.NONE);
		finalistRuleCompose.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		GridLayout gl_finalistRuleCompose = new GridLayout(2, false);
		gl_finalistRuleCompose.verticalSpacing = 1;
		gl_finalistRuleCompose.marginHeight = 0;
		gl_finalistRuleCompose.horizontalSpacing = 0;
		gl_finalistRuleCompose.marginWidth = 0;
		finalistRuleCompose.setLayout(gl_finalistRuleCompose);
		
		finalistNumText = new Text(finalistRuleCompose, SWT.CENTER);
		finalistNumText.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		finalistNumText.setFont(SWTResourceManager.getFont("微软雅黑", 11, SWT.BOLD));
		GridData gd_finalistNumText = new GridData(SWT.FILL, SWT.BOTTOM, false, false, 1, 1);
		gd_finalistNumText.widthHint = 30;
		finalistNumText.setLayoutData(gd_finalistNumText);
		finalistNumText.setTextLimit(3);
		
		finalistBySortNoTips = new Label(finalistRuleCompose, SWT.WRAP);
		finalistBySortNoTips.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 2));
		finalistBySortNoTips.setText("最终排名前3名入围！");
		
		
		ESepator num_sub_line = new ESepator(finalistRuleCompose, SWT.NONE);
		GridData gd_label_3 = new GridData(SWT.FILL, SWT.TOP, false, true, 1, 1);
		gd_label_3.widthHint = 30;
		num_sub_line.setLayoutData(gd_label_3);
		num_sub_line.setBackground(ColorConstants.BORDER_LINE_COLOR);
		
		Label label_4 = new Label(finalistRuleCompose, SWT.WRAP);
		label_4.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		label_4.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		label_4.setText(Messages.Tips_shortlistEmpty);
		label_4.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		
		Label label_3 = new Label(composite, SWT.NONE);
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_3.setText(Messages.EvaluationSet + "：");
		
		evaluationSetComposite = new Composite(composite, SWT.NONE);
		RowLayout rl_evaluationSetComposite = new RowLayout(SWT.HORIZONTAL);
		rl_evaluationSetComposite.marginRight = 0;
		rl_evaluationSetComposite.marginBottom = 0;
		rl_evaluationSetComposite.marginTop = 0;
		rl_evaluationSetComposite.marginLeft = 0;
		evaluationSetComposite.setLayout(rl_evaluationSetComposite);
		evaluationSetComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		correctEvaluationPrice_btn = new Button(evaluationSetComposite, SWT.CHECK);
		correctEvaluationPrice_btn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				selectCorrectEvalPriceAction();
			}
		});
		correctEvaluationPrice_btn.setText(Messages.AllowChangePrice);
		
		confirmEvaluationPrice_btn = new Button(evaluationSetComposite, SWT.CHECK);
		confirmEvaluationPrice_btn.setLayoutData(new RowData(SWT.DEFAULT, SWT.DEFAULT));
		confirmEvaluationPrice_btn.setText(Messages.ExpertConfirmationResult);
		
		changeBasePriceBtn = new Button(evaluationSetComposite, SWT.CHECK);
		changeBasePriceBtn.setText(Messages.AllowChangeBasePrice);
		changeBasePriceBtn.setLayoutData(new RowData(SWT.DEFAULT, SWT.DEFAULT));
		
		changePriceScoreBtn = new Button(evaluationSetComposite, SWT.CHECK);
		changePriceScoreBtn.setText(Messages.AllowChangePriceScore);
		changePriceScoreBtn.setLayoutData(new RowData(SWT.DEFAULT, SWT.DEFAULT));
		
		
		Label label_5 = new Label(composite, SWT.NONE);
		label_5.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, true, 1, 1));
		label_5.setText(Messages.Note + ": ");
		
		stepMemoText = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		stepMemoText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		stepMemoText.setTextLimit(100);
		
		Composite table_border_composite = new Composite(container, SWT.NONE);
		table_border_composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		GridLayout gl_composite_table = new GridLayout(1, false);
		gl_composite_table.marginHeight = 1;
		gl_composite_table.marginWidth = 1;
		table_border_composite.setLayout(gl_composite_table);
		table_border_composite.setBackground(ColorConstants.BORDER_LINE_COLOR);
		
		Composite table_composite = new Composite(table_border_composite, SWT.NONE);
		table_composite.setBackground(SWTResourceManager.getColor(255, 255, 255));
		table_composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_composite_4 = new GridLayout(1, false);
		gl_composite_4.marginWidth = 0;
		gl_composite_4.marginBottom = 5;
		gl_composite_4.marginHeight = 0;
		gl_composite_4.verticalSpacing = 0;
		table_composite.setLayout(gl_composite_4);
		
		factorGrid = new Grid(table_composite, SWT.H_SCROLL | SWT.V_SCROLL);
		GridData gd_grid = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_grid.widthHint = 250;
		factorGrid.setLayoutData(gd_grid);
		
		gridColumn = new GridColumn(factorGrid, SWT.CHECK);
		gridColumn.setText("New Column");
		gridColumn.setWidth(150);
		
//		GridItem topGridItem = new GridItem(factorGrid, SWT.NONE);
//		topGridItem.setText(Messages.Factor_price);
//		topGridItem.setImage(0, menuOpenImage);
//		topGridItem.setExpanded(true);
		
		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(){
		evaluationStepFactorService = new EvaluationStepFactorService();
		projectRuleService = new ProjectRuleService();
		addShortListRuleListener();
		if(null == evaluationStep){
			evaluationStep = new EvaluationStep();
			evaluationStep.setStepName(Messages.Step_price);
			evaluationStep.setFilterType(EvaluationStepEnum.FILTER_NO);
			evaluationStep.setPassType(EvaluationStepEnum.PASS_ALL);
			evaluationStep.setRoleType(EvaluationStepEnum.EXPERT_LEADER);
			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));
			if(ObjectUtils.equals(evaluationStep.getStepStatus(), CommonEnum.COMPLETE)){
//				composite.setEnabled(false);
				save_btn.setEnabled(false);
				correctEvaluationPrice_btn.setEnabled(false);
				confirmEvaluationPrice_btn.setEnabled(false);
				changeBasePriceBtn.setEnabled(false);
				changePriceScoreBtn.setEnabled(false);
				isInEvaluation = true;
			}else {
				isInEvaluation = evaluationStepService.isInEvaluationStep(tenderProjectEntity.getId(), evaluationStep.getId());
			}
			if(isInEvaluation){
				for(Control control : roleTypeComposite.getChildren()){
					control.setEnabled(false);
				}
				
			}
		}
		factorGrid.setAutoHeight(true);
		gridColumn.setWordWrap(true);
		gridColumn.setShowTreeTogglePic(true);
		gridColumn.setTreeTogglePic(ImageResolver.RESOLVER_INSTANCE.loadImage(IconsActivator.PLUGIN_ID,"icons/tree_expend.png"), ImageResolver.RESOLVER_INSTANCE.loadImage(IconsActivator.PLUGIN_ID,"icons/tree_close.png"));
		gridColumn.setTree(true);
		menuOpenImage = imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/tree_parent.png");
		menuImage = imageResolver.loadImage(IconsActivator.PLUGIN_ID,"icons/tree_item.png");
		refreshFactorTree();
		// 添加选中监听事件
		factorGrid.addListener(SWT.Selection, new GridCheckedListener(false));
		GridUtils.refreshGridCheckedStatus(factorGrid, 0);
		factorGrid.addListener(SWT.Selection, new Listener(){
			public void handleEvent(Event event) {
				// 选中了复选框事件
				if(event.detail == SWT.CHECK){
//					GridItem item = (GridItem)event.item;
					checkFactorItemAction();
				}
			}
		});
		refreshEvaluationStepForm();
		//TOTO 暂不支持修改报价
//		correctEvaluationPrice_composite.setEnabled(false);
	}
	
	private void addShortListRuleListener(){
		IntegerInputListener integerInputListener = new IntegerInputListener();
		integerInputListener.addMinEqCheck(0, null);
		finalistNumText.addKeyListener(integerInputListener);
		final ModifyListener bySortNoListener = new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				int value = ObjectUtils.getIntValue(finalistNumText.getText().trim(), 0);
				if(value < 1){
					value = 0;
					finalistNumText.removeModifyListener(this);
					finalistNumText.setText("");
					finalistNumText.addModifyListener(this);
				}
				finalistBySortNoTips.setText(MessageFormat.format(Messages.Tips_shortlistBySortNo, value > 0 ? value : "___"));
				finalistNumText.getParent().layout();
			}
		};
		finalistNumText.addModifyListener(bySortNoListener);
	}
	
	/**
	 * @Description: 刷新指标树
	 * @author guoyr
	 * @param factorId
	 */
	public void refreshFactorTree(){
		factorGrid.removeAll();
		List<EvaluationFactor> evaluationFactorList = evaluationFactorService.getFactorListOfEvaluationStep(tenderProjectEntity.getId(), EvaluationFactorEnum.AUDIT_PRICE, evaluationStep.getId());
		if(null != evaluationFactorList && evaluationFactorList.size() > 0){
			
			if(StringUtils.isNotEmpty(evaluationStep.getId())){
				factorList = evaluationStepFactorService.getFactorIdLsitByStepId(evaluationStep.getId());
			}
			topGridItem = new GridItem(factorGrid, SWT.NONE);
			topGridItem.setText(Messages.Factor_price);
			topGridItem.setImage(0, menuOpenImage);
			topGridItem.setExpanded(true);
			if(isInEvaluation){
				topGridItem.setCheckable(0, false);
//				topGridItem.setCheckVisible(0, false);
				topGridItem.setGrayed(0, true);
			}
			for(EvaluationFactor factor : evaluationFactorList){
				GridItem gridItem = new GridItem(topGridItem, SWT.NONE);
				gridItem.setText(0, factor.getFactorName());
				gridItem.setData("evaluationFactor", factor);
				if(isInEvaluation){
					gridItem.setCheckable(0, false);
//					gridItem.setCheckVisible(0, false);
					gridItem.setGrayed(0, true);
				}
//				gridItem.setCheckable(0, true);
				initChildGridItem(factor, gridItem);
			}
		}else {
			save_btn.setEnabled(false);
		}
		if(StringUtils.isNotEmpty(evaluationStep.getId())){
			GridUtils.refreshGridCheckedStatus(factorGrid, 0);
			hasNoCheckedFactor = false;
			getCheckedEvaluationFactorList(topGridItem.getItems());
			if(hasNoCheckedFactor) {
				tips.setText(tips.getText()+"当前步骤存在部分未选择的子级指标！");
				tips.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
			}
		}else {
			GridUtils.checkAll(factorGrid, 0, true);
			checkFactorItemAction();
		}
	}
	
	/**
	 * @Description: 递归实现指标树
	 * @author guoyr
	 * @param factor
	 * @param parentGridItem
	 * @param selectId
	 */
	private void initChildGridItem(EvaluationFactor factor, GridItem parentGridItem){
		if(null != factor.getChildFactorList() && factor.getChildFactorList().size() > 0){
			for(EvaluationFactor childFactor : factor.getChildFactorList()){
				if(ObjectUtils.equals(childFactor.getFactorType(), EvaluationFactorEnum.FACTOR_FLOAT)){
					continue;
				}
				GridItem gridItem = new GridItem(parentGridItem, SWT.NONE);
				gridItem.setText(0, childFactor.getFactorName());
				gridItem.setData("evaluationFactor", childFactor);
//				gridItem.setGrayed(0, true);
//				gridItem.setCheckable(0, false);
				gridItem.setCheckVisible(0, false);
				gridItem.setGrayed(0, true);
				initChildGridItem(childFactor, gridItem);
			}
			parentGridItem.setExpanded(true);
			parentGridItem.setImage(0, menuOpenImage);
		}else {
			parentGridItem.setImage(0, menuImage);
		}
		if(null != factorList && factorList.contains(factor.getId())){
			parentGridItem.setChecked(0, true);
		}
		parentGridItem.setToolTipText(0, parentGridItem.getText());
	}
	
	/**
	 * @Description: 选择修正评标价事件
	 * @author guoyr
	 */
	private void selectCorrectEvalPriceAction(){
		confirmEvaluationPrice_btn.setVisible(correctEvaluationPrice_btn.getSelection());
		
		// 暂不支持确认
		confirmEvaluationPrice_btn.setVisible(false);
		confirmEvaluationPrice_btn.setSelection(false);
		((RowData)confirmEvaluationPrice_btn.getLayoutData()).exclude = true;
		
	}
	
	/**
	 * @Description: 刷新评标步骤表单
	 * @author guoyr
	 */
	private void refreshEvaluationStepForm(){
		stepNameText.setText(ObjectUtils.getObjString(evaluationStep.getStepName()));
		stepMemoText.setText(ObjectUtils.getObjString(evaluationStep.getStepMemo()));
		if(evaluationStep.getFinalistNumber() > 0){
			finalistNumText.setText(evaluationStep.getFinalistNumber()+"");
		}else {
			finalistNumText.setText("");
		}
		WidgetUtils.setRadioChecked(roleTypeComposite, ObjectUtils.getObjString(evaluationStep.getRoleType()), "roleType");
		if(ObjectUtils.equals(tenderProjectRuleEntity.getCorrectPrice(), CommonEnum.YES)){
			correctEvaluationPrice_btn.setSelection(true);
			confirmEvaluationPrice_btn.setVisible(true);
		}else {
			confirmEvaluationPrice_btn.setVisible(false);
			correctEvaluationPrice_btn.setSelection(false);
		}
		showScoreAndWeight(evaluationStep.getScore(), evaluationStep.getWeight());
		confirmEvaluationPrice_btn.setSelection(ObjectUtils.equals(tenderProjectRuleEntity.getConfirmPrice(), CommonEnum.YES));
		
		boolean changeBasePrice = evaluationStep.getEvaluationParamByKey(EvaluationStepEnum.PARAM_CHANGEBASEPRICE, boolean.class);
		boolean changePriceScore = evaluationStep.getEvaluationParamByKey(EvaluationStepEnum.PARAM_CHANGEPRICESCORE, boolean.class);
		changeBasePriceBtn.setSelection(changeBasePrice);
		changePriceScoreBtn.setSelection(changePriceScore);
		
		// 暂不支持确认
		confirmEvaluationPrice_btn.setSelection(false);
		WidgetUtils.setControlVisible(confirmEvaluationPrice_btn, false);
	}
	
	/**
	 * @Description: 显示分值和权重
	 * @author guoyr
	 * @param score
	 * @param weight 
	 */
	private void showScoreAndWeight(BigDecimal score, BigDecimal weight){
		if(null != score){
			if(null != weight && (weight.compareTo(BigDecimal.ZERO) == 0 || weight.compareTo(new BigDecimal(100)) == 0)){
				weight = null;
			}
			scoreAndWeight.setText(NumberFormat.getInstance().format(score) + Messages.TextScore +(null !=  weight ? " （" + (NumberFormat.getInstance().format(weight)) +"%）": ""));
		}else {
			scoreAndWeight.setText("0" + Messages.TextScore);
		}
		evaluationStep.setScore(score);
		evaluationStep.setWeight(weight);
		scoreAndWeight.getParent().layout();
	}
	
	/**
	 * @Description: 选择指标事件
	 * @author guoyr
	 * @param gridItem 
	 */
	private void checkFactorItemAction(){
		BigDecimal score = null;
		BigDecimal weight = null;
		if(null != topGridItem && !topGridItem.isDisposed()){
			List<EvaluationFactor> checkedTopFactorList = new ArrayList<EvaluationFactor>();
			for(GridItem topItem : topGridItem.getItems()){
				if(topItem.getCheckVisible(0) && topItem.getChecked(0)){
					EvaluationFactor topFactor = (EvaluationFactor)topItem.getData("evaluationFactor");
					checkedTopFactorList.add(topFactor);
				}
			}
			
			if(checkedTopFactorList.size() > 0){
				if(stepNameText.getText().length() == 0){
					String firstFactorName = checkedTopFactorList.get(0).getFactorName();
					stepNameText.setText(firstFactorName.length() > 9 ? firstFactorName.substring(0,9):firstFactorName);
				}
				for(EvaluationFactor topFactor : checkedTopFactorList){
					BigDecimal factorScore = topFactor.getFactorScore();
					BigDecimal factorWeight = topFactor.getFactorWeight();
					if(null != factorScore){
						if(checkedTopFactorList.size() > 1) {
							BigDecimal thisWeight = BigDecimalUtils.div(BigDecimalUtils.mul(factorScore, factorWeight), new BigDecimal(100));
							weight = null != weight ? BigDecimalUtils.add(thisWeight, weight) : thisWeight;
						}else {
							weight = factorWeight; 
						}
					}
				}
			}
		}
		showScoreAndWeight(score, weight);
	}
	
	private List<EvaluationFactor> getCheckedEvaluationFactorList(GridItem[] gridItems){
		List<EvaluationFactor> checkedValuesList = new ArrayList<EvaluationFactor>();
		if(null != gridItems) {
			for(GridItem item : gridItems){
				if(item.getChecked(0)) {
					EvaluationFactor evaluationFactor = (EvaluationFactor)item.getData("evaluationFactor");
					EvaluationFactor checkFactor = new EvaluationFactor();
					checkFactor.setId(evaluationFactor.getId());
					checkFactor.setFactorName(evaluationFactor.getFactorName());
					checkFactor.setTenderId(evaluationFactor.getTenderId());
					checkedValuesList.add(checkFactor);
					
					if(item.getItemCount() > 0) {
						List<EvaluationFactor> subCheckedValuesList = getCheckedEvaluationFactorList(item.getItems());
						if(null != subCheckedValuesList && subCheckedValuesList.size() > 0) {
							checkedValuesList.addAll(subCheckedValuesList);
							for(GridItem subItem : item.getItems()) {
								if(!subItem.getChecked(0)) {
									hasNoCheckedFactor = true; 
									if(!isInEvaluation){
										subItem.setCheckable(0, true);
									}
									subItem.setCheckVisible(0, true);
									subItem.setForeground(0, SWTResourceManager.getColor(SWT.COLOR_RED));
								}
							}
						}
					}
				}
				
			}
		}
		return checkedValuesList;
	} 
	
	/**
	 * @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;
			}
			evaluationStep.setEvalBidType(EvaluationFactorEnum.EVAL_BID_PRICE);
			evaluationStep.setStepName(stepNameText.getText().trim());
			evaluationStep.setStepType(EvaluationStepEnum.PRICE);//价格评审
			evaluationStep.setStepMemo(stepMemoText.getText().trim());
			evaluationStep.setTenderId(tenderProjectEntity.getId());
			evaluationStep.setRoleType(ObjectUtils.getObjString(WidgetUtils.getRadioVal(roleTypeComposite,"roleType")));
			evaluationStep.setFilterType(EvaluationStepEnum.FILTER_NO);
			try {
				evaluationStep.setFinalistNumber(Integer.parseInt(finalistNumText.getText().trim()));
			} catch (NumberFormatException e) {
				evaluationStep.setFinalistNumber(0);
			}
			
			evaluationStep.setEvaluationParam(EvaluationStepEnum.PARAM_CHANGEBASEPRICE, changeBasePriceBtn.getSelection());
			evaluationStep.setEvaluationParam(EvaluationStepEnum.PARAM_CHANGEPRICESCORE, changePriceScoreBtn.getSelection());
			

			tenderProjectRuleEntity.setCorrectPrice(correctEvaluationPrice_btn.getSelection()?CommonEnum.YES : CommonEnum.NO);
			tenderProjectRuleEntity.setConfirmPrice(confirmEvaluationPrice_btn.getSelection()?CommonEnum.YES : CommonEnum.NO);
			projectRuleService.updateAssignProperty(tenderProjectRuleEntity, new String[]{"id","correctPrice", "confirmPrice"});
			
			
			if(isInEvaluation){
				evaluationStepService.updateAssignProperty(evaluationStep, new String[]{"stepName", "finalistNumber", "stepMemo", "params"});
				if(null != callBack){
					callBack.callBackObject(evaluationStep);
				}
				this.getShell().close();
				return;
			}
			
			hasNoCheckedFactor = false;
			List<EvaluationFactor> checkedEvaluationFactorList = getCheckedEvaluationFactorList(topGridItem.getItems());//GridUtils.getRealCheckedValues(grid, 0, "evaluationFactor");
			if(hasNoCheckedFactor) {
				MessageDialog.openWarning(getShell(), "当前步骤存在部分未选择的子级指标！");
				return;
			}
//			List<EvaluationFactor> checkedEvaluationFactorList = GridUtils.getRealCheckedValues(grid, 0, "evaluationFactor");
			if(null == checkedEvaluationFactorList || checkedEvaluationFactorList.size() == 0){
				MessageDialog.openWarning(getShell(), Messages.Tips_select +stepNameText.getText().trim()+"的评审指标！");
				return;
			}
//			List<String> factorWeightList = new ArrayList<String>();
//			List<EvaluationFactor> checkedTopFactorList = GridUtils.getCheckedValues(factorGrid, 0, "evaluationFactor", true);
//			for(EvaluationFactor factor : checkedTopFactorList){
//				if(null != factor && null != factor.getFactorWeight()){
//					factorWeightList.add(factor.getId());
//				}
//			}
			evaluationStep = (EvaluationStep) evaluationStepService.saveEvaluationStepAndFactors(evaluationStep, checkedEvaluationFactorList, null, null);
			if(null != evaluationStep && null != callBack){
				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(700, 500);
	}
}
