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

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.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.ELabel;
import org.eclipse.swt.core.widgets.ELink;
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.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.DictEnum;
import com.gx.obe.business.entity.Dictionary;
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.Constants;
import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.components.core.enumeration.EvaluationStepEnum;
import com.gx.obe.message.Messages;
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.DictionaryValue;
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.service.BaseDictionaryValueService;
import com.gx.obe.web.service.DictionaryService;
import com.gx.obe.web.service.evaluation.EvaluationStepExpertService;
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 FeatureEvaluationStepDialog extends EDialog {
	
	private Text stepNameText;
	private Composite roleTypeComposite;
	private Composite customComposite;
	private Composite filterComposite;
	private Composite passTypeComposite;
	private Grid grid;
	private EButton save_btn;
	private GridColumn gridColumn;
	private ImageResolver imageResolver = new ImageResolver();
	private EvaluationStepService evaluationStepService = new EvaluationStepService();
 	private DictionaryService dictionaryService ;
	private BaseDictionaryValueService dictionaryValueService ;
	private ExpertService expertService;
	private TenderProjectEntity tenderProjectEntity;
	private EvaluationStep evaluationStep;
	private Text stepMemoText;
	private ELabel expertNamesLabel;
	private Composite composite;
	private CallBack callBack;
	private List<Expert> stepExpertUserIdList ;
	private String supplierName;
	private ELabel filterHelpLabel;
	private ELabel passTypeHelpLabel;
	private Button button_2;
	private Button button_3;
	private Button button_4;
	private Button filterYesBtn;
	private Button filterNoBtn;
	private Button button_1;
	private Button button_5;
	private boolean isInEvaluation = false;// 是否在评审中
	private Label label_8;
	
	{
		LogUtils.logClass(FeatureEvaluationStepDialog.class);
	}

	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public FeatureEvaluationStepDialog(Shell parentShell, TenderProjectEntity tenderProjectEntity, EvaluationStep evaluationStep) {
		super(parentShell);
		setTitle(Messages.Step_step);
		this.tenderProjectEntity = tenderProjectEntity;
		this.evaluationStep = evaluationStep;
		supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProjectEntity.getTenderType());
	}

	/**
	 * 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(2, 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, 2, 1));
		label_6.setText(Messages.Tips_featureCheckStep);
		label_6.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, EvaluationStepEnum.getEvaluationStepIco(EvaluationStepEnum.FEATURE_CHECK)));
		label_6.setSurroundColor(SWTResourceManager.getColor(60, 138, 226));
		
		composite = new Composite(container, SWT.NONE);
		GridLayout gl_composite = new GridLayout(2, false);
		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);
		
		CLabel label = new CLabel(composite, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText(Messages.NodeName + "：");
		
		stepNameText = new Text(composite, SWT.BORDER);
		GridData gd_stepNameText = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_stepNameText.widthHint = 150;
		stepNameText.setLayoutData(gd_stepNameText);
		stepNameText.addVerifyListener(new VerifyRequiredListener(stepNameText, MessageFormat.format(Messages.Tips_required, Messages.StepName)));
		stepNameText.setTextLimit(20);
		stepNameText.setText(Messages.Step_feature);
		
		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 + "：");
		
		Composite composite_6 = new Composite(composite, SWT.NONE);
		composite_6.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_composite_6 = new GridLayout(1, false);
		gl_composite_6.marginHeight = 0;
		gl_composite_6.verticalSpacing = 1;
		gl_composite_6.marginWidth = 0;
		composite_6.setLayout(gl_composite_6);
		
		roleTypeComposite = new Composite(composite_6, SWT.NONE);
		RowLayout rl_roleTypeComposite = new RowLayout(SWT.HORIZONTAL);
		rl_roleTypeComposite.marginLeft = 0;
		roleTypeComposite.setLayout(rl_roleTypeComposite);
		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) {
				evaluationStep.setRoleType(EvaluationStepEnum.EXPERT);
				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) {
				evaluationStep.setRoleType(EvaluationStepEnum.EXPERT_LEADER);
				selectRoleTypeAction(EvaluationStepEnum.EXPERT_LEADER);
			}
		});
		button_3.setText(Messages.ExpertLeader);
		button_3.setData("roleType", EvaluationStepEnum.EXPERT_LEADER);
		
		button_4 = new Button(roleTypeComposite, SWT.RADIO);
		button_4.setData("roleType", EvaluationStepEnum.CUSTOM);
		button_4.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				evaluationStep.setRoleType(EvaluationStepEnum.CUSTOM);
				selectRoleTypeAction(EvaluationStepEnum.CUSTOM);
			}
		});
		button_4.setText(Messages.SpecifyExpert);
		
		customComposite = new Composite(composite_6, SWT.NONE);
		GridData gd_customComposite = new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1);
		gd_customComposite.heightHint = 36;
		customComposite.setLayoutData(gd_customComposite);
		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 ELabel(customComposite, SWT.WRAP);
		GridData gd_expertNamesLabel = new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1);
		gd_expertNamesLabel.heightHint = 14;
		gd_expertNamesLabel.widthHint = 150;
		expertNamesLabel.setLayoutData(gd_expertNamesLabel);
		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));
		
		Label label_4 = new Label(customComposite, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_4.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		CLabel label_2 = new CLabel(composite, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_2.setText(Messages.ShortlistedRules + "：");
		
		Composite composite_5 = new Composite(composite, SWT.NONE);
		composite_5.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_composite_5 = new GridLayout(1, false);
		gl_composite_5.marginHeight = 0;
		gl_composite_5.marginWidth = 0;
		composite_5.setLayout(gl_composite_5);
		
		filterComposite = new Composite(composite_5, SWT.NONE);
		filterComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		GridLayout gl_filterComposite = new GridLayout(3, false);
		gl_filterComposite.marginHeight = 0;
		gl_filterComposite.marginWidth = 0;
		filterComposite.setLayout(gl_filterComposite);
		
		filterYesBtn = new Button(filterComposite, SWT.RADIO);
		filterYesBtn.setData("filterType",EvaluationStepEnum.FILTER_YES);
		filterYesBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				filterAction();
			}
		});
		filterYesBtn.setSelection(true);
		filterYesBtn.setText(Messages.FilterFinalists);
		
		filterNoBtn = new Button(filterComposite, SWT.RADIO);
		filterNoBtn.setData("filterType",EvaluationStepEnum.FILTER_NO);
		filterNoBtn.setText(Messages.AllFinalists);
		filterNoBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				filterAction();
			}
		});
		
		filterHelpLabel = new ELabel(filterComposite, SWT.SELECTED);
		filterHelpLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/help.png"));
		filterHelpLabel.setText("");
		
		passTypeComposite = new Composite(composite_5, SWT.NONE);
		GridData gd_passTypeComposite = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_passTypeComposite.heightHint = 22;
		passTypeComposite.setLayoutData(gd_passTypeComposite);
		GridLayout gl_passTypeComposite = new GridLayout(3, false);
		gl_passTypeComposite.marginHeight = 0;
		gl_passTypeComposite.marginWidth = 0;
		passTypeComposite.setLayout(gl_passTypeComposite);
		
		button_1 = new Button(passTypeComposite, SWT.RADIO);
		button_1.setData("passType",EvaluationStepEnum.PASS_ALL);
		button_1.setSelection(true);
		button_1.setText(Messages.OneTicketVeto);
		
		button_5 = new Button(passTypeComposite, SWT.RADIO);
		button_5.setData("passType",EvaluationStepEnum.PASS_MORE);
		button_5.setText(Messages.MinorityObeyMajority);
		
		passTypeHelpLabel = new ELabel(passTypeComposite, SWT.SELECTED);
		passTypeHelpLabel.setText("");
		passTypeHelpLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/help.png"));
		
		CLabel label_5 = new CLabel(composite, SWT.NONE);
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, true, 1, 1));
		label_5.setText(Messages.Note + "：");
		
		stepMemoText = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		stepMemoText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		stepMemoText.setTextLimit(100);
		
		Composite composite_4 = new Composite(container, SWT.NONE);
		composite_4.setBackground(ColorConstants.BORDER_LINE_COLOR);
		composite_4.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1));
		GridLayout gl_composite_4 = new GridLayout(1, false);
		gl_composite_4.marginWidth = 1;
		gl_composite_4.marginHeight = 1;
		composite_4.setLayout(gl_composite_4);
		
		Composite composite_3 = new Composite(composite_4, SWT.NONE);
		composite_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		composite_3.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_composite_3 = new GridLayout(1, false);
		gl_composite_3.marginHeight = 0;
		gl_composite_3.marginWidth = 0;
		composite_3.setLayout(gl_composite_3);
		composite_3.setBackgroundMode(SWT.INHERIT_FORCE);
		
		
		grid = new Grid(composite_3, SWT.V_SCROLL);
		GridData gd_grid = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_grid.widthHint = 245;
		grid.setLayoutData(gd_grid);
		
		gridColumn = new GridColumn(grid, SWT.CHECK);
		gridColumn.setText("New Column");
		gridColumn.setWidth(150);
		
		label_8 = new Label(topContainer, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_8.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		
		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, 3, 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) {
				saveNodeAction();
			}
		});
		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(){
		expertService = new ExpertService();
		dictionaryValueService = new BaseDictionaryValueService();
		dictionaryService = new DictionaryService();
		new EvaluationStepExpertService();
		isInEvaluation = false;
		// 评标步骤未操作
		if(null == evaluationStep){
			evaluationStep = new EvaluationStep();
			evaluationStep.setStepName(Messages.Step_feature);
			evaluationStep.setFilterType(EvaluationStepEnum.FILTER_YES);
			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)){
				save_btn.setEnabled(false);
				isInEvaluation = true;
			}else {
				isInEvaluation = evaluationStepService.isInEvaluationStep(tenderProjectEntity.getId(), evaluationStep.getId());
			}
			if(isInEvaluation){
				for(Control control : roleTypeComposite.getChildren()){
					control.setEnabled(false);
				}
			}
		}
		refreshEvaltionNodeForm();
		grid.setItemHeight(Constants.ROW_HIGHT);
		grid.setAutoHeight(true);
		gridColumn.setWordWrap(true);
		refreshFeatureParams();
		// 添加选中监听事件
		grid.addListener(SWT.Selection, new GridCheckedListener(false));
//		GridUtil.refreshGridCheckedStatus(grid, 0);
		
		filterHelpLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				StringBuilder helpSb = new StringBuilder();
				helpSb.append(Messages.ShortlistedRules + "：");
				helpSb.append(MessageFormat.format(Messages.Tips_stepFilterRule, Messages.OneTicketVeto, Messages.MinorityObeyMajority, supplierName));
				helpSb.append("\r\n");
				helpSb.append(Messages.AllFinalists + "：");
				helpSb.append(MessageFormat.format(Messages.Tips_evaluationRule_noFilter, supplierName));
				MessageDialog.openInformation(getShell(), helpSb.toString());
			}
		});
		passTypeHelpLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				StringBuilder helpSb = new StringBuilder();
				helpSb.append(Messages.OneTicketVeto + "：");
				helpSb.append(MessageFormat.format(Messages.Tips_oneTicketVeto, Messages.Unqualified, supplierName));
				helpSb.append("\r\n");
				helpSb.append(Messages.MinorityObeyMajority + "：");
				helpSb.append(MessageFormat.format(Messages.Tips_minorityObeyMajority, Messages.MinorityObeyMajority));
				MessageDialog.openInformation(getShell(), helpSb.toString());
			}
		});
	}
	
	/**
	 * @Description: 列出特征码检查项
	 * @author guoyr
	 * @param factorId
	 */
	public void refreshFeatureParams(){
		grid.removeAll();
		Dictionary dictionary = dictionaryService.getDictionaryAndValuesByCode(DictEnum.FEATURE);
		List<DictionaryValue> clearTenderItemList = dictionary.getDictValueList();
		if(null != clearTenderItemList && clearTenderItemList.size() > 0){
			List<DictionaryValue> checkedItem = dictionaryValueService.getFeatureCheckItemList(tenderProjectEntity.getId());
			for(DictionaryValue dictionaryValue : clearTenderItemList){
				GridItem gridItem = new GridItem(grid, SWT.NONE);
				gridItem.setText(0, dictionaryValue.getDictValueName());
				gridItem.setData("dictionaryValueId", dictionaryValue.getId());
				if(null != checkedItem){
					for(DictionaryValue checked : checkedItem){
						if(dictionaryValue.getId().equals(checked.getId())){
							gridItem.setChecked(0, true);
							break;
						}
					}
				}
				if(isInEvaluation){
					gridItem.setCheckable(0, false);
				}
			}
			
		}else {
			save_btn.setEnabled(false);
		}
		if(StringUtils.isEmpty(evaluationStep.getId())){
			GridUtils.checkAll(grid, 0, true);
		}
	}
	
	/**
	 * @Description: 刷新评标步骤表单
	 * @author guoyr
	 */
	private void refreshEvaltionNodeForm(){
		stepNameText.setText(ObjectUtils.getObjString(evaluationStep.getStepName()));
		stepMemoText.setText(ObjectUtils.getObjString(evaluationStep.getStepMemo()));
		WidgetUtils.setRadioChecked(roleTypeComposite, ObjectUtils.getObjString(evaluationStep.getRoleType()), "roleType");
		WidgetUtils.setRadioChecked(filterComposite, ObjectUtils.getObjString(evaluationStep.getFilterType()), "filterType");
		WidgetUtils.setRadioChecked(passTypeComposite, ObjectUtils.getObjString(evaluationStep.getPassType()), "passType");
		filterAction();
		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 saveNodeAction(){
		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;
			}
			
			List<String> dictionaryValueList = GridUtils.getCheckedValues(grid, 0, "dictionaryValueId");
			if(null == dictionaryValueList || dictionaryValueList.size() < 1){
				MessageDialog.openWarning(getShell(), MessageFormat.format(Messages.Tips_pleaseSelectLeastOne, Messages.FeatureCode));
				return;
			}
			
			evaluationStep.setStepName(stepNameText.getText().trim());
			evaluationStep.setStepType(EvaluationStepEnum.FEATURE_CHECK);
			evaluationStep.setStepMemo(stepMemoText.getText().trim());
			evaluationStep.setTenderId(tenderProjectEntity.getId());
			evaluationStep.setRoleType(ObjectUtils.getObjString(WidgetUtils.getRadioVal(roleTypeComposite,"roleType")));
			evaluationStep.setFilterType(ObjectUtils.getObjString(WidgetUtils.getRadioVal(filterComposite,"filterType")));
			evaluationStep.setPassType(ObjectUtils.getObjString(WidgetUtils.getRadioVal(passTypeComposite,"passType")));
			// 如果选择的是自定义专家类型
			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.saveFeatureEvaluationStep(evaluationStep, expertIdList, dictionaryValueList);
			if(null != evaluationStep){
				if(null != callBack){
					callBack.callBackObject(evaluationStep);
				}
			}else {
				MessageDialog.openWarning(getShell(), stepNameText.getText()+Messages.Tips_saveFail);
			}
			this.getShell().close();
		}
	}
	
	/**
	 * @Description: 取消
	 * @author guoyr
	 */
	private void cancelAction(){
		this.getShell().close();
	}

	/**
	 * @Description: 筛选事件
	 * @author guoyr
	 */
	private void filterAction(){
		String filterType = ObjectUtils.getObjString(WidgetUtils.getRadioVal(filterComposite, "filterType"));
		if(filterType.equals(EvaluationStepEnum.FILTER_YES)){
			passTypeComposite.layout();
			((GridData)passTypeComposite.getLayoutData()).heightHint = passTypeComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
		}else {
			((GridData)passTypeComposite.getLayoutData()).heightHint = 0;
		}
		passTypeComposite.getParent().layout();
		stepNameText.getParent().layout();
	}
	
	
	/**
	 * @Description: 选择评审成员类型
	 * @author guoyr
	 * @param roleType
	 */
	private void selectRoleTypeAction(String 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();
			for(Control control : customComposite.getChildren()){
				control.setVisible(true);
				
			}
			((GridData)customComposite.getLayoutData()).heightHint = customComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
		}else {
			for(Control control : customComposite.getChildren()){
				control.setVisible(false);
				
			}
			((GridData)customComposite.getLayoutData()).heightHint = 0;
		}
		composite.layout();
	}
	
	/**
	 * @Description: 选择自定义专家
	 * @author guoyr
	 */
	private void selectStepExpert(){
		SelectStepExpertDialog dialog = new SelectStepExpertDialog(getShell(), tenderProjectEntity.getId(), stepExpertUserIdList,new SelectExpertListener(){
			public void setExpertList(List<Expert> expertList){
				if(null != expertList){
					stepExpertUserIdList = expertList;
					selectRoleTypeAction(evaluationStep.getRoleType());
				}				
			}
		});
		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(700, 450);
	}
}
