package com.gx.obe.management.composite;

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.swt.SWT;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.widgets.BigButton;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.core.widgets.ImageButton;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
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.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.dialog.WasteBidStepResultDialog;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.components.core.enumeration.EvaluationStepEnum;
import com.gx.obe.evaluation.step.dialog.SelectBidRegulationsDialog;
import com.gx.obe.evaluation.step.form.WasteBidStepDialog;
import com.gx.obe.evaluation.step.listener.BidRegulationsListener;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.SupplierEvaluationResult;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.SupplierEvaluationResultService;
import com.gx.obe.web.service.SupplierService;
import com.gx.obe.web.service.evaluation.EvaluationStepService;
import com.gx.obe.web.service.expert.ExpertEvaluationResultService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

public class SupplierEvaluactionOpinionComposite extends Composite {
	private SupplierService supplierService = new SupplierService();
	private TenderProjectEntity tenderProjectEntity;
	private Composite supplierBorderComposite;
	private Composite supplierListComposite;
	private Grid supplierGrid;
	private Text supplierEvaluactionOpinion;
	private ExpertEvaluationResultService expertEvaluationResultService;
	private SupplierEvaluationResultService supplierEvaluationResultService ;
	private SupplierEvaluationResult supplierEvaluationResult;
	private Composite evaluateGroup;
	private Text supplierEvalOpinionText;
	private Text requirementText;
	private Label requirementLength;
	private Label opinionLength;
	private ELink wasteBidItemLink;
	private ELink refreshLink;
	private StyledText editorLabel;
	private boolean applyFlag;
	boolean isUnqualified = false;
	private GridColumn gridColumn_num;
	private Composite composite;
	private BigButton submitBtn;
	private Supplier supplier;
	private ImageButton selectWasteSupplierBtn;
	private ImageButton selectAllSupplierBtn;
	private Label openionTitleLabel;
	private ELink abandonStep;
	private EvaluationStepService evaluationStepService = new EvaluationStepService();
	private Composite topComposite;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public SupplierEvaluactionOpinionComposite(Composite parent, int style) {
		super(parent, style);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent arg0) {
				disposiseAction();
			}
		});
		this.tenderProjectEntity = (TenderProjectEntity)parent.getData("tenderProjectEntity");
		GridLayout gridLayout = new GridLayout(2, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		
		topComposite = new Composite(this, SWT.NONE);
		topComposite.setLayout(new GridLayout(3, false));
		topComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
		
		selectWasteSupplierBtn = new ImageButton(topComposite, SWT.NONE);
		selectWasteSupplierBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/checked.png"));
		selectWasteSupplierBtn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				refreshSupplierAction(true);
			}
		});
		selectWasteSupplierBtn.setText(Messages.Button_ShowCanceled);
		
		selectAllSupplierBtn = new ImageButton(topComposite, SWT.NONE);
		selectAllSupplierBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/unchecked.png"));
		selectAllSupplierBtn.setText(Messages.Button_ShowAll);
		selectAllSupplierBtn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				refreshSupplierAction(false);
			}
		});
		
		abandonStep = new ELink(topComposite, SWT.NONE);
		abandonStep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		abandonStep.setText(Messages.Button_canceledBid);
		abandonStep.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/exit.png"));
		abandonStep.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				addAbandonStepModelAction();
			}
		});
		
		
		supplierBorderComposite = new Composite(this, SWT.NONE);
		GridLayout gl_s_bord = new GridLayout(1, false);
		gl_s_bord.verticalSpacing = 0;
		gl_s_bord.marginHeight = 1;
		gl_s_bord.horizontalSpacing = 0;
		gl_s_bord.marginWidth = 1;
		supplierBorderComposite.setLayout(gl_s_bord);
		GridData gd_composite_a2 = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1);
		gd_composite_a2.widthHint = 350;
		supplierBorderComposite.setLayoutData(gd_composite_a2);
		supplierBorderComposite.setBackground(ColorConstants.BORDER_LINE_COLOR);
		
		supplierListComposite = new Composite(supplierBorderComposite, SWT.NONE);
		supplierListComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_composite_4 = new GridLayout(1, false);
		gl_composite_4.verticalSpacing = 0;
		gl_composite_4.horizontalSpacing = 0;
		gl_composite_4.marginHeight = 0;
		gl_composite_4.marginWidth = 0;
		supplierListComposite.setLayout(gl_composite_4);
		supplierListComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		supplierGrid = new Grid(supplierListComposite, SWT.V_SCROLL);
		supplierGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		supplierGrid.setItemHeaderWidth(30);
		supplierGrid.setHeaderVisible(true);
		supplierGrid.setAutoWidth(true);
		
		
		gridColumn_num = new GridColumn(supplierGrid, SWT.LEFT);
		gridColumn_num.setText(Messages.Supplier);
		gridColumn_num.setWidth(200);
		
		GridColumn statusColumn_num = new GridColumn(supplierGrid, SWT.CENTER);
		statusColumn_num.setText(Messages.Status);
		statusColumn_num.setWidth(50);
		statusColumn_num.setMaxmumWidth(60);
		
		Composite container = new Composite(this, SWT.NONE);
		container.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_container = new GridLayout(1, false);
		gl_container.verticalSpacing = 0;
		gl_container.horizontalSpacing = 0;
		gl_container.marginHeight = 0;
		gl_container.marginWidth = 0;
		container.setLayout(gl_container);
		container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		evaluateGroup = new Composite(container, SWT.NONE);
		evaluateGroup.setBackground(ColorConstants.BORDER_LINE_COLOR);
		evaluateGroup.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		GridLayout gl_evaluateGroup = new GridLayout(1, false);
		gl_evaluateGroup.verticalSpacing = 1;
		gl_evaluateGroup.horizontalSpacing = 1;
		gl_evaluateGroup.marginHeight = 1;
		gl_evaluateGroup.marginWidth = 1;
		evaluateGroup.setLayout(gl_evaluateGroup);
		evaluateGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Composite composite_5 = new Composite(evaluateGroup, SWT.NONE);
		composite_5.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite_5.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		composite_5.setLayout(new GridLayout(3, false));
		
		editorLabel = new StyledText(composite_5, SWT.WRAP);
		editorLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		editorLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		editorLabel.setForeground(ColorConstants.FONT_BLACK_COLOR);
		editorLabel.setText("您正在对录入评（废）标意见");
		
		refreshLink = new ELink(composite_5, SWT.NONE);
		refreshLink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/refresh.png"));
		refreshLink.setText(Messages.Button_ApplyInput);
		new Label(composite_5, SWT.NONE);
		refreshLink.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				refreshAction();
			}
		});
		
		Composite composite_3 = new Composite(evaluateGroup, SWT.NONE);
		GridData gd_composite_3 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_composite_3.heightHint = 50;
		composite_3.setLayoutData(gd_composite_3);
		GridLayout gl_composite_3 = new GridLayout(1, false);
		gl_composite_3.verticalSpacing = 0;
		gl_composite_3.horizontalSpacing = 1;
		gl_composite_3.marginHeight = 1;
		gl_composite_3.marginWidth = 1;
		composite_3.setLayout(gl_composite_3);
		composite_3.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		Composite composite_1 = new Composite(composite_3, SWT.NONE);
		composite_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		composite_1.setLayout(new GridLayout(3, false));
		
		Label label_2 = new Label(composite_1, SWT.NONE);
		label_2.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		label_2.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		label_2.setText(Messages.Title_biddingFileRequir + "("+Messages.NoRequired+")");
		
		requirementLength = new Label(composite_1, SWT.NONE);
		requirementLength.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		requirementLength.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		requirementLength.setText("");
		
		wasteBidItemLink = new ELink(composite_1, SWT.NONE);
		wasteBidItemLink.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				selectionWasteOpinion();
			}
		});
		wasteBidItemLink.setText(MessageFormat.format(Messages.Button_selectFormat, Messages.Button_votoClause));
		wasteBidItemLink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/query.png"));
		
		ESepator req_sep = new ESepator(composite_3, SWT.NONE);
		req_sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		req_sep.setBackground(ColorConstants.BORDER_LINE_COLOR);
		req_sep.setLineStyle(SWT.LINE_DOT);
		req_sep.setLineWidth(2);
		
		requirementText = new Text(composite_3, SWT.V_SCROLL | SWT.MULTI | SWT.WRAP);
		requirementText.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		requirementText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		requirementText.setTextLimit(500);
		
		Composite composite_2 = new Composite(evaluateGroup, SWT.NONE);
		GridData gd_composite_2 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_composite_2.heightHint = 50;
		composite_2.setLayoutData(gd_composite_2);
		GridLayout gl_composite_2 = new GridLayout(1, false);
		gl_composite_2.verticalSpacing = 0;
		gl_composite_2.horizontalSpacing = 1;
		gl_composite_2.marginHeight = 1;
		gl_composite_2.marginWidth = 1;
		composite_2.setLayout(gl_composite_2);
		composite_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		Composite composite_4 = new Composite(composite_2, SWT.NONE);
		composite_4.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite_4.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		composite_4.setLayout(new GridLayout(2, false));
		
		openionTitleLabel = new Label(composite_4, SWT.NONE);
		openionTitleLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		openionTitleLabel.setForeground(ColorConstants.FONT_BLACK_COLOR);
		openionTitleLabel.setText("评（废）标意见（必填）");
		
		opinionLength = new Label(composite_4, SWT.NONE);
		opinionLength.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		opinionLength.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		opinionLength.setText("");
		
		ESepator opi_sep = new ESepator(composite_2, SWT.NONE);
		opi_sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		opi_sep.setBackground(ColorConstants.BORDER_LINE_COLOR);
		opi_sep.setLineStyle(SWT.LINE_DOT);
		opi_sep.setLineWidth(2);
		
		supplierEvalOpinionText = new Text(composite_2, SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		supplierEvalOpinionText.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		supplierEvalOpinionText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		supplierEvalOpinionText.setTextLimit(500);
		
		composite = new Composite(evaluateGroup, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		composite.setLayout(new GridLayout(1, false));
		
		submitBtn = new BigButton(composite, SWT.NONE);
		submitBtn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		submitBtn.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
		submitBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				saveSupplierEvaluationOpinion();
			}
		});
		submitBtn.setText("保存评（废）标意见");
		
		init();
	}
	/**
	 * @Description: 初始化
	 * @author guoyr 
	 */
	private void init(){
		expertEvaluationResultService = new ExpertEvaluationResultService();
		if(null == supplierEvaluationResultService){
			supplierEvaluationResultService = new SupplierEvaluationResultService();
		}
		String supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProjectEntity.getTenderType());
		supplierGrid.setItemHeight(Constants.ROW_HIGHT);
		supplierGrid.setAutoHeight(true);
		supplierGrid.setWordWrapHeader(true);
		gridColumn_num.setText(supplierName);
//		if(null == tenderProjectEntity.getParentId()){
//			abandonStep.setText("项目废标");
//		}else {
//			abandonStep.setText(com.gx.obe.components.core.I18N.getString(I18NEnum.SUBPACKAGE, tenderProjectEntity.getTenderType()) + Messages.Button_canceledBid);
//		}
		supplierGrid.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				supplierSelectionAction();
			}
		});
		editorLabel.setEditable(false);
		requirementText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				requirementLength.setText("   (" +requirementText.getText().trim().length() + "/500)");
			}
		});
		requirementText.setText("");
		
		supplierEvalOpinionText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				opinionLength.setText("   (" +supplierEvalOpinionText.getText().trim().length() + "/500)");
			}
		});
//		selectWasteSupplierBtn.setText("只显示已废标（已否决）" + supplierName);
//		selectAllSupplierBtn.setText("显示全部" + supplierName);
		
		refreshSupplierAction(false);
	}
	
	private void refreshSupplierAction(boolean wasteOnly){
		if(wasteOnly){
			selectWasteSupplierBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/checked.png"));
			selectAllSupplierBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/unchecked.png"));
		}else {
			selectWasteSupplierBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/unchecked.png"));
			selectAllSupplierBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/checked.png"));
		}
		supplierGrid.removeAll();
		List<Supplier> supplierList = supplierService.getOpenBidSuccessSupplier(tenderProjectEntity.getId());
		if(null != supplierList && supplierList.size() > 0 ){
			List<Supplier> newSupplierList = new ArrayList<Supplier>();
			List<SupplierEvaluationResult> supplierEvaluationResultList = supplierEvaluationResultService.getSupplierEvaluationResultListByTenderId(tenderProjectEntity.getId());
		
			for(Supplier supplier : supplierList){
				SupplierEvaluationResult supplierEvaluationResult = null;
				if(null != supplierEvaluationResultList && supplierEvaluationResultList.size() > 0){
					for(SupplierEvaluationResult evalResult : supplierEvaluationResultList){
						if(ObjectUtils.equals(evalResult.getSupplierId(), supplier.getId())){
							supplierEvaluationResult = evalResult;
							break;
						}
					}
				}
				supplier.setSupplierEvaluationResult(supplierEvaluationResult);
				if(wasteOnly){
					if(null != supplierEvaluationResult && null != supplierEvaluationResult.getAccessment() && supplierEvaluationResult.getAccessment().equals(EvaluationStepEnum.UNQUALIFIED)){
						newSupplierList.add(supplier);
					}
				}else {
					newSupplierList.add(supplier);
				}
			}
			
			for(Supplier supplier : newSupplierList){
				GridItem item = new GridItem(supplierGrid, SWT.NONE);
				item.setText(0, supplier.getSupplierName());
				String status = "";
				if(null != supplier.getSupplierEvaluationResult()){
					if(null != supplier.getSupplierEvaluationResult().getAccessment() && supplier.getSupplierEvaluationResult().getAccessment().equals(EvaluationStepEnum.UNQUALIFIED)){
						status = Messages.RevokeStatus_yes;
						item.setForeground(1, SWTResourceManager.getColor(SWT.COLOR_RED));
					}else {
						status = Messages.RevokeStatus_no;
					}
				}
				item.setText(0, supplier.getSupplierName());
				item.setText(1, status);
				item.setData("supplier", supplier);
			}
			supplierGrid.setSelection(0);
		}
		supplierSelectionAction();
	}
	
	/**
	 * @Description: 废标步骤
	 * @author wangxiang 
	 */
	private void addAbandonStepModelAction() {
		if (abandonStep.getEnabled()) {
			EvaluationStep wasteEvaluationStep = getWasteEvaluationStep();
			if (null != wasteEvaluationStep) {
				WasteBidStepResultDialog dialog = new WasteBidStepResultDialog(getShell(), tenderProjectEntity, wasteEvaluationStep);
				dialog.setOperator(SWT.MAX);
				dialog.open();
			}else{
				final WasteBidStepDialog dialog = new WasteBidStepDialog(getShell(), tenderProjectEntity, null);
				dialog.setCallBack(new CallBack() {
					public void callBackObject(Object obj) {
						if(null != obj) {
							dialog.close();
						}
					}
				});
				dialog.open();
			}
		}
	}
	
	/**
	 * @Description: 验证是否存在废标步骤
	 * @author wangxiang
	 * @return
	 */
	private EvaluationStep getWasteEvaluationStep() {
		List<EvaluationStep> evaluationStepList = evaluationStepService.getEvaluationStepListByType(tenderProjectEntity.getId(), EvaluationStepEnum.WASTE_BID);
		if(null != evaluationStepList && evaluationStepList.size() > 0){
			return evaluationStepList.get(evaluationStepList.size() -1);
		}else{
			return null;
		}
	}
	
	/**
	 * @Description:刷新 
	 * @author guoyr 
	 */
	private void refreshAction(){
		if(refreshLink.getEnabled()){
			supplierSelectionAction();
		}
	}
	
	
	
	/**
	 * @Description: 申请编辑供应商的评审意见
	 * @author guoyr
	 * @return 
	 */
	private void applyEditSupplierEvaluationResult(Supplier supplier){
		applyFlag = supplierEvaluationResultService.applyEditSupplierEvaluationResult(tenderProjectEntity.getId(), supplier.getId(), Constants.USER.getId(), Messages.ProjectManager);
		if(!applyFlag){
			MessageDialog.openShortMessage(Messages.Tips_operateFail);
		}
		refreshLink.setEnabled(!applyFlag);// 已申请到编辑权限就再不用刷新了
		refreshLink.setVisible(!applyFlag);
		wasteBidItemLink.setEnabled(applyFlag);
		requirementText.setEditable(applyFlag);
		supplierEvalOpinionText.setEditable(applyFlag);
		submitBtn.setEnabled(applyFlag);
	}
	
	
	/**
	 * @Description: 选择供应商事件
	 * @author guoyr
	 * @param supplier 
	 */
	private void supplierSelectionAction(){
		if(null != supplierEvaluationResult && null != supplier && null != supplierEvaluationResult.getApplyUserId() && supplierEvaluationResult.getApplyUserId().equals(Constants.USER.getId())){
			supplierEvaluationResultService.releaseEditSupplierEvaluationResult(tenderProjectEntity.getId(), supplier.getId(), Constants.USER.getId());
		}
		supplier = null;
		supplierEvaluationResult = null;
		requirementText.setText("");
		supplierEvalOpinionText.setText("");
		if(supplierGrid.getSelectionCount() > 0){
			supplier = (Supplier)supplierGrid.getSelection()[0].getData("supplier");
		}
		String editor = null;
		if(null != supplier){
			// 申请编辑权限
			applyEditSupplierEvaluationResult(supplier);
			supplierEvaluationResult = supplierEvaluationResultService.getSupplierEvaluationResultByTenderIdAndSupplierId(tenderProjectEntity.getId(), supplier.getId());
			if(null != supplierEvaluationResult){
				if(null != supplierEvaluationResult.getEvaluationOpinion() && supplierEvaluationResult.getEvaluationOpinion().length() > 0){
					supplierEvalOpinionText.setText(supplierEvaluationResult.getEvaluationOpinion());
				}
				if(null != supplierEvaluationResult.getBiddingRequest() && supplierEvaluationResult.getBiddingRequest().length() > 0){
					requirementText.setText(supplierEvaluationResult.getBiddingRequest());
				}
				editor = supplierEvaluationResult.getOpinionEditor();
			}
			if(applyFlag){
//				editor = Messages.You;
			}else {
//				editor = Messages.Others;
			}
			isUnqualified = false;
			if( null != supplierEvaluationResult && null != supplierEvaluationResult.getAccessment() && supplierEvaluationResult.getAccessment().equals(EvaluationStepEnum.UNQUALIFIED)){
				isUnqualified = true;
			}
			editorLabel.setText(MessageFormat.format(Messages.Title_supplierEvaluationOpinion, editor, supplier.getSupplierName(), isUnqualified ? Messages.Title_bidCancelledOpinion : Messages.Title_evaluationOpinion));
			
//			StyleRange styleRange = new StyleRange(); 
//			styleRange.start = editorLabel.getText().indexOf(editor); 
//			styleRange.length = editor.length(); 
//			styleRange.foreground = SWTResourceManager.getColor(SWT.COLOR_BLUE);
//			styleRange.fontStyle = SWT.BOLD; 
//			editorLabel.setStyleRange(styleRange);
//			styleRange.start = editorLabel.getText().indexOf(supplier.getSupplierName()); 
//			styleRange.length = supplier.getSupplierName().length(); 
//			editorLabel.setStyleRange(styleRange);
			
			if(isUnqualified){
				openionTitleLabel.setText(Messages.Title_bidCancelledOpinion + "("+Messages.Required+")");
				submitBtn.setText(MessageFormat.format(Messages.Button_saveFormat, Messages.Title_bidCancelledOpinion));
			}else {
				openionTitleLabel.setText(Messages.Title_evaluationOpinion+ "("+Messages.Required+")");
				submitBtn.setText(MessageFormat.format(Messages.Button_saveFormat, Messages.Title_evaluationOpinion));
			}
			
		}else {
			editorLabel.setText(MessageFormat.format(Messages.Title_supplierEvaluationOpinion, com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProjectEntity.getTenderType())));
			refreshLink.setEnabled(false);// 已申请到编辑权限就再不用刷新了
			wasteBidItemLink.setEnabled(false);
			requirementText.setEditable(false);
			supplierEvalOpinionText.setEditable(false);
			submitBtn.setEnabled(false);
		}
		openionTitleLabel.getParent().layout();
	}
	
	/**
	 * @Description: 选择废标意见
	 * @author guoyr 
	 */
	private void selectionWasteOpinion(){
		if(applyFlag){
			SelectBidRegulationsDialog dialog = new SelectBidRegulationsDialog(getShell(), tenderProjectEntity, new BidRegulationsListener() {
				public void selectBidRegulations(String BidRegulationsContents) {
					requirementText.setText(requirementText.getText() + "\r\n" + BidRegulationsContents);				
				}
			});
			dialog.open();
//			MessageDialog.openShortMessage("未找到废标条款！");
			
		}
	}
	
	/**
	 * @Description: 保存操作
	 * @author guoyr 
	 */
	private void saveSupplierEvaluationOpinion(){
		if(applyFlag){
			if(supplierEvalOpinionText.getText().trim().length() == 0){
				if(isUnqualified){
					
					MessageDialog.openShortMessage(MessageFormat.format(Messages.Tips_required, Messages.Title_bidCancelledOpinion));
				}else {
					
					MessageDialog.openShortMessage(MessageFormat.format(Messages.Tips_required, Messages.Title_evaluationOpinion));
				}
				return;
			}
			if(supplierEvaluationResultService.saveSupplierEvaluationOpinion(true, tenderProjectEntity.getId(), supplier.getId(), requirementText.getText(), supplierEvalOpinionText.getText(),supplierEvaluationResult.getVetoType())){
				supplierEvaluationResult.setApplyUserId(null);
				MessageDialog.openShortMessage(Messages.Tips_saveSuccess);
			}else {
				MessageDialog.openError(getShell(), Messages.Tips_saveFail);
			}
		}
	}
	
	private void disposiseAction(){
		if(null != supplier){
			supplierEvaluationResultService.releaseEditSupplierEvaluationResult(tenderProjectEntity.getId(), supplier.getId(), Constants.USER.getId());
		}
		
	}
	@Override
	protected void checkSubclass() {
	}
}
