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

import java.math.BigDecimal;
import java.text.MessageFormat;
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.ELink;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
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.RowLayout;
import org.eclipse.swt.widgets.Button;
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.business.vo.BillFilter;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.button.Tips;
import com.gx.obe.common.widget.dialog.SelectStepExpertDialog;
import com.gx.obe.common.widget.listener.SelectExpertListener;
import com.gx.obe.components.core.I18NEnum;
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.components.core.enumeration.TenderProjectEnum;
import com.gx.obe.config.utils.PropertiesUtils;
import com.gx.obe.evaluation.step.dialog.BillFilterDialog;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.util.utils.CollectionUtils;
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.expert.Expert;
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.EvaluationStepExpertService;
import com.gx.obe.web.service.evaluation.EvaluationStepFactorService;
import com.gx.obe.web.service.evaluation.EvaluationStepService;
import com.gx.obe.web.service.expert.ExpertService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 
 * @author guoyr
 */
public class BillEvaluationStepDialog extends EDialog {
	
	private Text stepNameText;
	private Composite composite;
	private Grid grid;
	private EButton save_btn;
	private GridColumn gridColumn;
	private ImageResolver imageResolver = new ImageResolver();
	private Image menuOpenImage;
	private Image menuImage;
	private ProjectRuleService projectRuleService;
	private EvaluationFactorService evaluationFactorService = new EvaluationFactorService();
	private EvaluationStepService evaluationStepService = new EvaluationStepService();
	private EvaluationStepFactorService evaluationStepFactorService ;
	private EvaluationStepExpertService evaluationStepExpertService;
	private ExpertService expertService;
	private TenderProjectEntity tenderProjectEntity;
	private EvaluationStep evaluationStep;
	private List<Object> factorList;
	private Text stepMemoText;
	private List<Expert> stepExpertUserIdList ;
	private CallBack callBack;
	private boolean isInEvaluation = false;// 是否在评审中
	private CLabel addItemizeFilterLabel;
	private CLabel addMeasureFilterLabel;
	private Text messageItemizeText;
	private Text messageMeasureText;
	private String evalMethod;
	private Label itemizeLabel;
	private Label measureLabel;
	private Composite itemizeComposite;
	private Composite measureComposite;
	private Composite roleTypeComposite;
	private Button button_2;
	private Button button_3;
	private Button button_4;
	private Button expertRoleNameBtn;
	private Label customExpertLabel;
	private Composite customComposite;
	private Label expertNamesLabel;
	
	{
		LogUtils.logClass(BillEvaluationStepDialog.class);
	}
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public BillEvaluationStepDialog(Shell parentShell, TenderProjectEntity tenderProjectEntity, EvaluationStep evaluationStep) {
		super(parentShell);
		setTitle(Messages.Step_step);
		this.tenderProjectEntity = tenderProjectEntity;
		this.evaluationStep = evaluationStep;
		expertService = new ExpertService();
	}

	/**
	 * 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 label_6 = new Tips(container, SWT.WRAP);
		label_6.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		label_6.setText("");
		label_6.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, EvaluationStepEnum.getEvaluationStepIco(EvaluationStepEnum.BILL)));
		label_6.setText(MessageFormat.format(Messages.Tips_factorStep, Messages.Factor_bill));
		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.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(PropertiesUtils.getEnum(I18NEnum.BILL_FACTOR, "清单") + "评审");
		
		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);
		
		CLabel label_1 = new CLabel(composite, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_1.setText(Messages.EvalExpert + "：");
		
		roleTypeComposite = new Composite(composite, SWT.NONE);
		roleTypeComposite.setLayout(new RowLayout(SWT.HORIZONTAL));
		roleTypeComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		button_2 = new Button(roleTypeComposite, SWT.RADIO);
		button_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				selectRoleTypeAction(EvaluationStepEnum.EXPERT);
			}
		});
		button_2.setSelection(true);
		button_2.setText(Messages.AllExperts);
		button_2.setData("roleType", EvaluationStepEnum.EXPERT);
		
		button_3 = new Button(roleTypeComposite, SWT.RADIO);
		button_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				selectRoleTypeAction(EvaluationStepEnum.EXPERT_LEADER);
			}
		});
		button_3.setText(Messages.ExpertLeader);
		button_3.setData("roleType", EvaluationStepEnum.EXPERT_LEADER);
		
		expertRoleNameBtn = new Button(roleTypeComposite, SWT.RADIO);
		expertRoleNameBtn.setData("roleType", EvaluationStepEnum.EVAL_BID_TYPE);
		expertRoleNameBtn.setText("商务专家");
		expertRoleNameBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				selectRoleTypeAction(EvaluationStepEnum.EVAL_BID_TYPE);
			}
		});
		
		button_4 = new Button(roleTypeComposite, SWT.RADIO);
		button_4.setData("roleType", EvaluationStepEnum.CUSTOM);
		button_4.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				selectRoleTypeAction(EvaluationStepEnum.CUSTOM);
			}
		});
		button_4.setText(Messages.SpecifyExpert);
		
		customExpertLabel = new Label(composite, SWT.NONE);
		customExpertLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		
		customComposite = new Composite(composite, SWT.NONE);
		customComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_customComposite = new GridLayout(2, false);
		gl_customComposite.verticalSpacing = 0;
		gl_customComposite.horizontalSpacing = 0;
		gl_customComposite.marginHeight = 0;
		gl_customComposite.marginWidth = 0;
		customComposite.setLayout(gl_customComposite);
		
		expertNamesLabel = new Label(customComposite, SWT.WRAP);
		expertNamesLabel.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, true, 1, 1));
		expertNamesLabel.setText("郭永荣");
		
		ELink selectExpertLabel = new ELink(customComposite, SWT.NONE);
		selectExpertLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/search.png"));
		selectExpertLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				selectStepExpert();
			}
		});
		selectExpertLabel.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM, false, true, 1, 2));
		selectExpertLabel.setText(MessageFormat.format(Messages.Button_selectFormat, Messages.Expert));
		
		ESepator exp_sep = new ESepator(customComposite, SWT.SEPARATOR | SWT.HORIZONTAL);
		exp_sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		exp_sep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		itemizeLabel = new Label(composite, SWT.NONE);
		itemizeLabel.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		itemizeLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		itemizeLabel.setText("分部分项\r\n筛选条件：");
		
		itemizeComposite = new Composite(composite, SWT.NONE);
		GridLayout gl_composite_3 = new GridLayout(2, false);
		gl_composite_3.marginHeight = 0;
		gl_composite_3.marginWidth = 0;
		itemizeComposite.setLayout(gl_composite_3);
		itemizeComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		messageItemizeText = new Text(itemizeComposite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		GridData gd_messageItemizeText = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_messageItemizeText.heightHint = 35;
		messageItemizeText.setLayoutData(gd_messageItemizeText);
		messageItemizeText.setEnabled(false);
		
		addItemizeFilterLabel = new CLabel(itemizeComposite, SWT.NONE);
		addItemizeFilterLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/query.png"));
		
		measureLabel = new Label(composite, SWT.NONE);
		measureLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		measureLabel.setText("措施项目\r\n筛选条件：");
		
		measureComposite = new Composite(composite, SWT.NONE);
		measureComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		GridLayout gl_composite_4 = new GridLayout(2, false);
		gl_composite_4.marginHeight = 0;
		gl_composite_4.marginWidth = 0;
		measureComposite.setLayout(gl_composite_4);
		
		messageMeasureText = new Text(measureComposite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		GridData gd_messageMeasureText = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_messageMeasureText.heightHint = 35;
		messageMeasureText.setLayoutData(gd_messageMeasureText);
		messageMeasureText.setEnabled(false);
				
		addMeasureFilterLabel = new CLabel(measureComposite, SWT.NONE);
		addMeasureFilterLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/query.png"));
		
//		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 + "：");
//		new Label(composite, SWT.NONE);
		
		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_table_composite = new GridLayout(1, false);
		gl_table_composite.marginWidth = 0;
		gl_table_composite.marginBottom = 5;
		gl_table_composite.marginHeight = 0;
		gl_table_composite.verticalSpacing = 0;
		table_composite.setLayout(gl_table_composite);
		
		grid = 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 = 200;
		grid.setLayoutData(gd_grid);
		
		gridColumn = new GridColumn(grid, SWT.CHECK);
		gridColumn.setText("New Column");
		gridColumn.setWidth(150);
		
		GridItem topGridItem = new GridItem(grid, SWT.NONE);
		topGridItem.setText("清单指标");
		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);
		projectRuleService = new ProjectRuleService();
		init();
		return container;
	}

	/**
	 * @Description: 初始化
	 * @author guoyr
	 */
	private void init(){
		evaluationStepFactorService = new EvaluationStepFactorService();
		evaluationStepExpertService = new EvaluationStepExpertService();
		IntegerInputListener integerInputListener = new IntegerInputListener();
		integerInputListener.addMinEqCheck(0, null);
		if(null == evaluationStep){
			evaluationStep = new EvaluationStep();
			evaluationStep.setStepName(PropertiesUtils.getEnum(I18NEnum.BILL_FACTOR, "清单") + "评审");
			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);
				isInEvaluation = true;
			}else {
				isInEvaluation = evaluationStepService.isInEvaluationStep(tenderProjectEntity.getId(), evaluationStep.getId());
			}
			if(isInEvaluation){
				roleTypeComposite.setEnabled(false);
			}
		}
		refreshEvaluationStepForm();
		grid.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();
		// 添加选中监听事件
		grid.addListener(SWT.Selection, new GridCheckedListener(false));
		GridUtils.refreshGridCheckedStatus(grid, 0);
		
		addItemizeFilterLabel.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				addFilterAction(messageItemizeText);
			}
		});
		addMeasureFilterLabel.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				addFilterAction(messageMeasureText);
			}
		});
		
		TenderProjectRuleEntity tenderProjectRuleEntity = projectRuleService.getProjectRule(tenderProjectEntity.getId());
		
		evalMethod = tenderProjectRuleEntity.getEvalMethodType();
		if (null == evalMethod) {
			evalMethod = TenderProjectEnum.MIN_METHOD;
		}
		
		if (!evalMethod.equals(TenderProjectEnum.SUB_LOW_PRICE)) {
			itemizeLabel.dispose();
			itemizeComposite.dispose();
			measureLabel.dispose();
			measureComposite.dispose();
		}
	}
	
	/**
	 * @Description: 添加筛选条件事件
	 * @author chenxw 
	 * @param messageText
	 */
	private void addFilterAction(Text messageText) {
		BillFilter billFilter = (BillFilter) messageText.getData();
		BillFilterDialog dialog = new BillFilterDialog(getShell(), billFilter);
		dialog.open();
		billFilter = dialog.getBillFilter();
		if (null == billFilter.getFilterType()) {
			return;
		}
		setFilterLabel(messageText, billFilter);
	}

	/**
	 * @Description: 添加筛选按钮
	 * @author chenxw
	 * @param messageText 
	 * @param billFilter 
	 */
	private void setFilterLabel(Text messageText, BillFilter billFilter) {
		StringBuffer name = new StringBuffer();
		if (billFilter.getFilterType().equals(BillFilter.ALL)) {
			name.append("所有清单");
		} else if (billFilter.getFilterType().equals(BillFilter.UNDER_CONTROL_LINE)) {
			name.append("低于控制线的清单");
		} else if (billFilter.getFilterType().equals(BillFilter.EXCEED_DOUBLE_CONTROL_LINE)) {
			name.append("超过评标控制线200%的清单");
		} else if (billFilter.getFilterType().equals(BillFilter.AMONG_DEVIATION_PERCENTAGE)) {
			if (null == billFilter.getMinDeviationPercentage()) {
				billFilter.setMinDeviationPercentage(BigDecimal.ZERO);
			}
			if (null == billFilter.getMaxDeviationPercentage()) {
				billFilter.setMaxDeviationPercentage(BigDecimal.ZERO);
			}
			name.append("偏差在");
			name.append(BigDecimalUtils.round(billFilter.getMinDeviationPercentage().multiply(BigDecimal.valueOf(100)), 2) + "%");
			name.append("到 ");
			name.append(BigDecimalUtils.round(billFilter.getMaxDeviationPercentage().multiply(BigDecimal.valueOf(100)), 2) + "%");
			name.append("之间");
		}
		if (billFilter.isWeightAmong() && (null != billFilter.getMinWeight() || null != billFilter.getMaxWeight())) {
			if ((null == billFilter.getMinWeight() || billFilter.getMinWeight().compareTo(BigDecimal.ZERO) == 0) && (null == billFilter.getMaxWeight() || billFilter.getMaxWeight().compareTo(BigDecimal.valueOf(1)) == 0)) {
				
			} else if (null == billFilter.getMinWeight() || billFilter.getMinWeight().compareTo(BigDecimal.ZERO) == 0) {
				name.append("，权重小于等于");
				name.append(BigDecimalUtils.round(billFilter.getMaxWeight().multiply(BigDecimal.valueOf(100)), 2) + "%");
			} else if (null == billFilter.getMaxWeight() || billFilter.getMaxWeight().compareTo(BigDecimal.valueOf(1)) == 0) {
				name.append("，权重大于等于");
				name.append(BigDecimalUtils.round(billFilter.getMinWeight().multiply(BigDecimal.valueOf(100)), 2) + "%");
			} else {
				name.append("，权重在");
				name.append(BigDecimalUtils.round(billFilter.getMinWeight().multiply(BigDecimal.valueOf(100)), 2) + "%");
				name.append("到 ");
				name.append(BigDecimalUtils.round(billFilter.getMaxWeight().multiply(BigDecimal.valueOf(100)), 2) + "%");
				name.append("之间");
			}
			if (billFilter.getFilterType().equals(BillFilter.AMONG_DEVIATION_PERCENTAGE)) {
				name.append("的清单");
			}
		}
		messageText.setText(name.toString());
		messageText.setData(billFilter);
		messageText.pack();
		messageText.getParent().layout();
	}
	
	/**
	 * @Description: 刷新指标树
	 * @author guoyr
	 * @param factorId
	 */
	public void refreshFactorTree(){
		grid.removeAll();
		List<EvaluationFactor> evaluationFactorList = evaluationFactorService.getFactorListOfEvaluationStep(tenderProjectEntity.getId(), EvaluationFactorEnum.AUDIT_BILL, evaluationStep.getId());
		if(null != evaluationFactorList && evaluationFactorList.size() > 0){
			
			if(StringUtils.isNotEmpty(evaluationStep.getId())){
				factorList = evaluationStepFactorService.getFactorIdLsitByStepId(evaluationStep.getId());
			}
			GridItem topGridItem = new GridItem(grid, SWT.NONE);
			topGridItem.setText(PropertiesUtils.getEnum(I18NEnum.BILL_FACTOR, "清单") + "指标");
			topGridItem.setImage(0, menuOpenImage);
			topGridItem.setExpanded(true);
			if(isInEvaluation){
				topGridItem.setCheckable(0, false);
				topGridItem.setCheckVisible(0, false);
			}
			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);
				}
				initChildGridItem(factor, gridItem);
			}
		}else {
			save_btn.setEnabled(false);
		}
		if(StringUtils.isNotEmpty(evaluationStep.getId())){
			GridUtils.refreshGridCheckedStatus(grid, 0);
		}else {
			GridUtils.checkAll(grid, 0, true);
		}
	}
	
	/**
	 * @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.setCheckVisible(0, false);
				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 refreshEvaluationStepForm(){
		stepNameText.setText(ObjectUtils.getObjString(evaluationStep.getStepName()));
		stepMemoText.setText(ObjectUtils.getObjString(evaluationStep.getStepMemo()));
		BillFilter itemizeBillFilterItemize = evaluationStep.getEvaluationParamByKey(EvaluationFactorEnum.FACTOR_ITEMIZE);
		BillFilter measureBillFilterItemize = evaluationStep.getEvaluationParamByKey(EvaluationFactorEnum.FACTOR_MEASURE);
		if (null != itemizeBillFilterItemize) {
			setFilterLabel(messageItemizeText, itemizeBillFilterItemize);
		}
		if (null != measureBillFilterItemize) {
			setFilterLabel(messageMeasureText, measureBillFilterItemize);
		}
		WidgetUtils.setRadioChecked(roleTypeComposite, ObjectUtils.getObjString(evaluationStep.getRoleType()), "roleType");
		if(StringUtils.isNotEmpty(evaluationStep.getId()) && ObjectUtils.equals(evaluationStep.getRoleType(), EvaluationStepEnum.CUSTOM)){
			stepExpertUserIdList = expertService.getEvaluationStepExpertList(tenderProjectEntity.getId(), evaluationStep);
//			if(null == stepExpertUserIdList){
//				stepExpertUserIdList = new ArrayList<Expert>();
//			}else {
//				stepExpertUserIdList.clear();
//			}
//			if(null != expertList){
//				for(Expert expert : expertList){
//					stepExpertUserIdList.add(expert);
//				}
//			}
		}
		selectRoleTypeAction(evaluationStep.getRoleType());
	}
	
	
	/**
	 * @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.setStepName(stepNameText.getText().trim());
			evaluationStep.setStepType(EvaluationStepEnum.BILL);//价格评审
			evaluationStep.setStepMemo(stepMemoText.getText().trim());
			evaluationStep.setTenderId(tenderProjectEntity.getId());
			evaluationStep.setFilterType(EvaluationStepEnum.FILTER_NO);
			evaluationStep.setEvalBidType(EvaluationFactorEnum.EVAL_BID_BUS);
			evaluationStep.setRoleType(ObjectUtils.getObjString(WidgetUtils.getRadioVal(roleTypeComposite,"roleType")));
			if (evalMethod.equals(TenderProjectEnum.SUB_LOW_PRICE)) {
				if(null != messageItemizeText && !messageItemizeText.isDisposed()){
					evaluationStep.setEvaluationParam(EvaluationFactorEnum.FACTOR_ITEMIZE, messageItemizeText.getData());
				}
				if(null != messageMeasureText && !messageMeasureText.isDisposed()){
					evaluationStep.setEvaluationParam(EvaluationFactorEnum.FACTOR_MEASURE, messageMeasureText.getData());
				}
			}
			
			if(isInEvaluation){
				evaluationStepService.updateAssignProperty(evaluationStep, new String[]{"stepName", "finalistNumber", "importType", "darkEvaluation", "stepMemo"});
				if(null != callBack){
					callBack.callBackObject(evaluationStep);
				}
				this.getShell().close();
				return;
			}
			
			List<EvaluationFactor> checkedEvaluationFactorList = GridUtils.getRealCheckedValues(grid, 0, "evaluationFactor");
			List<EvaluationFactor> checkedTopFactorList = GridUtils.getCheckedValues(grid, 0, "evaluationFactor", true);
			List<String> factorWeightList = new ArrayList<String>();
			for(EvaluationFactor factor : checkedTopFactorList){
				if(null != factor && null != factor.getFactorWeight()){
					factorWeightList.add(factor.getId());
				}
			}
			
			if(null == checkedEvaluationFactorList || checkedEvaluationFactorList.size() == 0){
				MessageDialog.openWarning(getShell(), Messages.Tips_select+stepNameText.getText().trim()+"的评审指标！");
				return;
			}
			

			// 如果选择的是自定义专家类型
			if(ObjectUtils.equals(evaluationStep.getRoleType(), EvaluationStepEnum.CUSTOM) && (null == stepExpertUserIdList || stepExpertUserIdList.size() == 0)){
				MessageDialog.openWarning(getShell(), Messages.Tips_SelcetExpert);
				return;
			}
			List<String> expertIdList = new ArrayList<String>();
			if (CollectionUtils.isNotNull(stepExpertUserIdList)){
				for(Expert expert : stepExpertUserIdList){
					expertIdList.add(expert.getUserId());
				}
			}
			evaluationStep = (EvaluationStep) evaluationStepService.saveEvaluationStepAndFactors(evaluationStep, checkedEvaluationFactorList, factorWeightList, expertIdList);
			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();
	}

	/**
	 * @Description: 选择评审成员类型
	 * @author guoyr
	 * @param roleType
	 */
	private void selectRoleTypeAction(String roleType){
//		evaluationStep.setRoleType(roleType);
		// 如果选择的是自定义专家类型
		if(ObjectUtils.equals(roleType, EvaluationStepEnum.CUSTOM)){
			StringBuilder memberName = new StringBuilder("");
			 
			if(null != stepExpertUserIdList && stepExpertUserIdList.size() > 0){
				int memberCount = 0;
				for(Expert expert : stepExpertUserIdList){
					if(null != expert && null != expert.getUser()){
						if(memberCount > 0){
							memberName.append("；");
						}
						memberName.append(ObjectUtils.getObjString(expert.getUser().getUserName()));
						memberCount ++;
					}
				}
			}
			
			
			expertNamesLabel.setText(memberName.toString());
			customComposite.layout();
			((GridData)customExpertLabel.getLayoutData()).exclude = false;
			customExpertLabel.setVisible(true);
			((GridData)customComposite.getLayoutData()).exclude = false;
			customComposite.setVisible(true);
			
//			((GridData)customComposite.getLayoutData()).heightHint = customComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
		}else {
			((GridData)customComposite.getLayoutData()).exclude = true;
			customComposite.setVisible(false);
			((GridData)customExpertLabel.getLayoutData()).exclude = true;
			customExpertLabel.setVisible(false);
//			((GridData)customComposite.getLayoutData()).heightHint = 0;
		}
		composite.layout();
	}
	
	/**
	 * @Description: 选择自定义专家
	 * @author guoyr
	 */
	private void selectStepExpert(){
		if(!isInEvaluation){
			SelectStepExpertDialog dialog = new SelectStepExpertDialog(getShell(), tenderProjectEntity.getId(), stepExpertUserIdList, new SelectExpertListener(){
				public void setExpertList(List<Expert> expertList){
					if(null != expertList){
						stepExpertUserIdList = expertList;
						selectRoleTypeAction(EvaluationStepEnum.CUSTOM);
					}
				}
			});
			dialog.open();
		}
	}
	
	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(650, 500);
	}
}
