package com.gx.obe.evaluation.step.review.composite;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.custom.StyledText;
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.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;

import com.gx.obe.ColorConstants;
import com.gx.obe.business.entity.ExpertEvaluationStep;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.thread.SyncRunnable;
import com.gx.obe.common.widget.thread.SyncThread;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.components.core.enumeration.TenderProjectEnum;
import com.gx.obe.evaluation.step.listener.ExpertEvaluationListener;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.UuidUtils;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.expert.Expert;
import com.gx.obe.web.entity.expert.ExpertEvaluationResult;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.TenderProjectService;
import com.gx.obe.web.service.evaluation.EvaluationStepService;
import com.gx.obe.web.service.expert.ExpertEvaluationResultService;
import com.gx.obe.web.service.expert.ExpertEvaluationStepService;
import com.gx.obe.web.service.expert.ExpertService;
import com.gx.utils.ActionLogUtils;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

public class WasteBidProjectEvaluationComposite extends Composite {

	private TenderProjectEntity tenderProjectEntity;
	private EvaluationStep evaluationStep;
	private Expert CURRENT_EXPERT;
	private ExpertEvaluationListener expertEvaluationListener;
	private TenderProjectService tenderProjectService ;
	private ExpertEvaluationResultService expertEvaluationResultService;
	private Composite right_composite;
	private EButton voteYesBtn;
	private EButton voteNoBtn;
	private EvaluationStepService evaluationStepService = new EvaluationStepService();
	private Composite abandon_evaluation_table;
//	private ExpertEvaluationStepService expertEvaluationStepService ;;
	private ExpertService expertService ;
	private Composite composite;
	private StyledText reasonText;
	private Composite countComposite;
	private Label expertLabel;
	private Label wasteNoLable;
	private Label wasteYesLabel;
	private Composite composite_4;
	private Label tenderNameLabel;
	private Composite voteOperatorComposite;
	private boolean needVote = false;
	private int wasteYesVoteCount = 0;// 存储同意废标的专家个数
	private int wasteNoVoteCount = 0;// 存储不同意废标的专家个数
	private Object result;
	private List<ExpertEvaluationResult> expertEvaluationResultList = null;
	private boolean refreshVote;
	private boolean voteOperatorVisible = true;
	private int maxNum = 2;
	private Label tenderIdLabel;
	private ScrolledComposite scrolledComposite_1;
	private int expertCount = 0;
	private CLabel wasteStausBtn;
	private Composite wasteStausComposite;
	private Runnable wasteYesRunnable;
	private Runnable wasteNoRunnable;
	private Composite voteResultComposite;
	private Label voteResultLabel;
	
	private ExpertEvaluationStepService expertEvaluationStepService = new ExpertEvaluationStepService();
	
	{
		LogUtils.logClass(WasteBidProjectEvaluationComposite.class);
	}

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public WasteBidProjectEvaluationComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(2, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		tenderProjectService = new TenderProjectService();
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				goBackAction();
			}
		});
		this.tenderProjectEntity = (TenderProjectEntity) parent.getData("tenderProject");
		this.evaluationStep = (EvaluationStep) parent.getData("evaluationStep");
		this.CURRENT_EXPERT = (Expert) parent.getData(Constants.CUR_EXPERT);
		result = parent.getData("result");

		right_composite = new Composite(this, SWT.NONE);
		right_composite.setBackground(SWTResourceManager.getColor(247, 247, 247));
		right_composite.setBackgroundMode(SWT.INHERIT_FORCE);
		GridLayout gl_right_composite = new GridLayout(1, true);
		gl_right_composite.marginHeight = 0;
		gl_right_composite.marginWidth = 0;
		right_composite.setLayout(gl_right_composite);
		right_composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		scrolledComposite_1 = new ScrolledComposite(right_composite, SWT.H_SCROLL | SWT.V_SCROLL);
		scrolledComposite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		scrolledComposite_1.setExpandHorizontal(true);
		scrolledComposite_1.setExpandVertical(true);

		abandon_evaluation_table = new Composite(scrolledComposite_1, SWT.NONE);
		GridLayout gl_abandon_evaluation_table = new GridLayout(1, false);
		gl_abandon_evaluation_table.marginHeight = 50;
		gl_abandon_evaluation_table.verticalSpacing = 0;
		gl_abandon_evaluation_table.horizontalSpacing = 0;
		gl_abandon_evaluation_table.marginWidth = 0;
		abandon_evaluation_table.setLayout(gl_abandon_evaluation_table);
		
		wasteStausComposite = new Composite(abandon_evaluation_table, SWT.NONE);
		wasteStausComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		wasteStausComposite.setLayout(new FormLayout());
		GridData gd_composite_1 = new GridData(SWT.CENTER, SWT.FILL, true, false, 1, 1);
		gd_composite_1.widthHint = 800;
		wasteStausComposite.setLayoutData(gd_composite_1);
		
		wasteStausBtn = new CLabel(wasteStausComposite, SWT.NONE);
		GridLayout gl_composite_2 = new GridLayout(1, false);
		gl_composite_2.verticalSpacing = 0;
		gl_composite_2.horizontalSpacing = 0;
		gl_composite_2.marginHeight = 0;
		gl_composite_2.marginWidth = 0;
		wasteStausBtn.setLayout(gl_composite_2);
		FormData fd_composite_2 = new FormData();
		fd_composite_2.top = new FormAttachment(0, -3);
		fd_composite_2.left = new FormAttachment(0, -3);
		wasteStausBtn.setLayoutData(fd_composite_2);
		wasteStausBtn.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		wasteStausBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/waste_no.png"));

		composite_4 = new Composite(wasteStausComposite, SWT.NONE);
		FormData fd_composite_4 = new FormData();
		fd_composite_4.top = new FormAttachment(0);
		fd_composite_4.bottom = new FormAttachment(100);
		fd_composite_4.left = new FormAttachment(0);
		fd_composite_4.right = new FormAttachment(100);
		composite_4.setLayoutData(fd_composite_4);
		composite_4.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_composite_4 = new GridLayout(1, false);
		gl_composite_4.marginWidth = 50;
		gl_composite_4.marginHeight = 20;
		composite_4.setLayout(gl_composite_4);

		tenderNameLabel = new Label(composite_4, SWT.NONE | SWT.WRAP);
		tenderNameLabel.setFont(SWTResourceManager.getFont("微软雅黑", 14, SWT.NORMAL));
		tenderNameLabel.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, true, 1, 1));
		tenderNameLabel.setForeground(ColorConstants.FONT_BLACK_COLOR);
		tenderNameLabel.setText("项目名称");

		tenderIdLabel = new Label(composite_4, SWT.NONE);
		tenderIdLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true, 1, 1));
		tenderIdLabel.setText("项目编号：");
		tenderIdLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		tenderIdLabel.setForeground(ColorConstants.NOTE_TIPS_COLOR);

		composite = new Composite(abandon_evaluation_table, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 50;
		composite.setLayout(gl_composite);
		GridData gd_composite = new GridData(SWT.CENTER, SWT.FILL, true, true, 1, 1);
		gd_composite.widthHint = 800;
		gd_composite.minimumHeight = 100;
		composite.setLayoutData(gd_composite);
		
		ESepator sep = new ESepator(composite, SWT.NONE);
		sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		sep.setLineWidth(3);
		sep.setBackground(SWTResourceManager.getColor(240, 240, 240));

		Label label = new Label(composite, SWT.NONE);
		label.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		label.setText(Messages.WasteReason + ":");

		reasonText = new StyledText(composite, SWT.WRAP | SWT.MULTI);
		reasonText.setLineSpacing(10);
		reasonText.setIndent(30);
		reasonText.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		reasonText.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		reasonText.setForeground(ColorConstants.FONT_BLACK_COLOR);
		GridData gd_reasonText = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_reasonText.heightHint = 100;
		reasonText.setEditable(false);
		reasonText.setLayoutData(gd_reasonText);

		
		countComposite = new Composite(abandon_evaluation_table, SWT.NONE);
		countComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_countComposite = new GridLayout(1, false);
		gl_countComposite.marginWidth = 50;
		countComposite.setLayout(gl_countComposite);
		GridData gd_countComposite = new GridData(SWT.CENTER, SWT.FILL, true, true, 1, 1);
		gd_countComposite.widthHint = 800;
		countComposite.setLayoutData(gd_countComposite);
		
		ESepator downsep = new ESepator(countComposite, SWT.NONE);
		downsep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		downsep.setLineWidth(3);
		downsep.setLineStyle(SWT.LINE_DOT);
		downsep.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		downsep.setBackground(SWTResourceManager.getColor(240, 240, 240));

		Composite expertComposite = new Composite(countComposite, SWT.NONE);
		GridLayout gl_expertComposite = new GridLayout(6, false);
		expertComposite.setLayout(gl_expertComposite);
		GridData gd_expertComposite = new GridData(SWT.CENTER, SWT.FILL, false, true, 1, 1);
		expertComposite.setLayoutData(gd_expertComposite);
				
		Label label_1 = new Label(expertComposite, SWT.NONE);
		label_1.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_1.setText("专家总数:");

		expertLabel = new Label(expertComposite, SWT.NONE);
		expertLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		expertLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
		expertLabel.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		GridData gd_espertCountText = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_espertCountText.widthHint = 100;
		expertLabel.setLayoutData(gd_espertCountText);

		Label label_3 = new Label(expertComposite, SWT.NONE);
		label_3.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_3.setText("废标投票:");

		wasteYesLabel = new Label(expertComposite, SWT.NONE);
		wasteYesLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		wasteYesLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		wasteYesLabel.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		GridData gd_text_3 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_text_3.widthHint = 100;
		wasteYesLabel.setLayoutData(gd_text_3);
		
		Label label_2 = new Label(expertComposite, SWT.NONE);
		label_2.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_2.setText("不废标投票:");

		wasteNoLable = new Label(expertComposite, SWT.NONE);
		wasteNoLable.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		wasteNoLable.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
		wasteNoLable.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
		GridData gd_text_2 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_text_2.widthHint = 100;
		wasteNoLable.setLayoutData(gd_text_2);


//		label_5 = new Label(countComposite, SWT.NONE);
//		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
//		label_5.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));
//		label_5.setText("废 标 状 态:");

//		lblResultlable = new CLabel(countComposite, SWT.NONE);
//		GridData gd_lblResultlable = new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1);
//		gd_lblResultlable.heightHint = 80;
//		gd_lblResultlable.widthHint = 100;
//		lblResultlable.setLayoutData(gd_lblResultlable);
//		lblResultlable.setFont(SWTResourceManager.getFont("微软雅黑", 14, SWT.NORMAL));
//		lblResultlable.setText("");
		scrolledComposite_1.setContent(abandon_evaluation_table);
		scrolledComposite_1.setMinSize(abandon_evaluation_table.computeSize(SWT.DEFAULT, SWT.DEFAULT));

		voteOperatorComposite = new Composite(right_composite, SWT.NONE);
		voteOperatorComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		voteOperatorComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		voteOperatorComposite.setLayout(new GridLayout(3, false));

		voteYesBtn = new EButton(voteOperatorComposite, SWT.NONE | EButton.CSS_GREEN);
		voteYesBtn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		GridData gd_save_result_btn = new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1);
		gd_save_result_btn.widthHint = 90;
		gd_save_result_btn.heightHint = 35;
		voteYesBtn.setLayoutData(gd_save_result_btn);
		voteYesBtn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				voteConfirmAction(com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED);
			}
		});
		voteYesBtn.setText("废标");

		voteNoBtn = new EButton(voteOperatorComposite, SWT.NONE | EButton.CSS_GREEN);
		voteNoBtn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		GridData gd_signBtn = new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1);
		gd_signBtn.heightHint = 35;
		gd_signBtn.widthHint = 90;
		voteNoBtn.setLayoutData(gd_signBtn);
		voteNoBtn.setText("不废标");
		voteNoBtn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				voteConfirmAction(com.gx.obe.components.core.enumeration.EvaluationStepEnum.QUALIFIED);
			}
		});
		
		voteResultComposite = new Composite(right_composite, SWT.NONE);
		voteResultComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		voteResultComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		voteResultComposite.setLayout(new GridLayout(3, false));
		
		voteResultLabel = new Label(voteResultComposite, SWT.NONE);
		voteResultLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
		voteResultLabel.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.BOLD));
		voteResultLabel.setForeground(ColorConstants.FONT_BLACK_COLOR);

		init();

	}

	/**
	 * @Description: 刷新本人投标结果
	 * @author guoyr
	 * @param accessment 
	 */
	private void refreshVoteResult(String accessment){
		if(null != accessment && accessment.length() > 0){
			
			if (com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED.equals(accessment)) {
				voteResultLabel.setText("您已投票：废标");
			} else if (com.gx.obe.components.core.enumeration.EvaluationStepEnum.QUALIFIED.equals(accessment)) {
				voteResultLabel.setText("您已投票：不废标");
			}
			((GridData) voteResultComposite.getLayoutData()).exclude = false;	
			voteResultComposite.setVisible(true);
		}
	}
	/**
	 * @Description: 初始化
	 * @author wangxiang
	 */
	private void init() {
		expertService = new ExpertService();
		expertEvaluationResultService = new ExpertEvaluationResultService();
		((GridData) voteResultComposite.getLayoutData()).exclude = true;	
		voteResultComposite.setVisible(false);
		
		needVote = evaluationStep.getEvaluationParamByKey(com.gx.obe.components.core.enumeration.EvaluationStepEnum.PARAM_WASTE_NEED_VOTE, Boolean.class);
		voteOperatorVisible = false;
		if(needVote){
			voteOperatorVisible = true;
			// 修改状态为正在进行中
			if(null != CURRENT_EXPERT){
				ExpertEvaluationStep expertEvaluationStep = expertEvaluationStepService.getExpertEvaluationStep(tenderProjectEntity.getId(), evaluationStep.getId(), CURRENT_EXPERT.getUserId());
				if (null != expertEvaluationStep) {
					if (CommonEnum.COMPLETE.equals(expertEvaluationStep.getStepStatus())){
						voteOperatorVisible = false;
						List<ExpertEvaluationResult> expertEvaluationResultList = expertEvaluationResultService.getExpertStepAllSupplierEvaluationResultList(tenderProjectEntity.getId(), evaluationStep.getId(), Constants.USER.getId());
						if(null != expertEvaluationResultList && expertEvaluationResultList.size() > 0){
							refreshVoteResult(expertEvaluationResultList.get(0).getAccessment());
						}
					}
				}else {
					ActionLogUtils.addServerLog(tenderProjectEntity.getId(), "进入" + evaluationStep.getStepName(), Constants.USER.getUserName());
					expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.IN_PROGRESS, tenderProjectEntity.getId(), evaluationStep, CURRENT_EXPERT.getUserId());
				}
			}
		}
		
		if(!voteOperatorVisible || null != result){
			((GridData) voteOperatorComposite.getLayoutData()).exclude = true;	
			voteOperatorComposite.setVisible(false);
		}
		
		if(needVote){
			PaintListener paintListener = new PaintListener() {
				int num_width = 30;
				int num_height = 35;
				int num_sep_width = 0;
				double widthHeightRate = 0.5;
				String[] countTime = new String[maxNum];
				final Image blueEmptyImage = ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/time/blue_e.png");
				final Image redEmptyImage = ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/time/red_e.png");
				
				public void paintControl(PaintEvent arg0) {
					Label control = (Label)arg0.getSource();
					String curCountStr = (String)control.getData("count");
					boolean isRedImage = null != control.getData("red");
					if(null == curCountStr){
						curCountStr = "0";
					}
					maxNum = curCountStr.length() > 0 ? curCountStr.length() : 1;
					if (null != countTime && countTime.length != curCountStr.length()) {
						countTime = new String[maxNum];
					}
					for (int i = 0; i < maxNum; i++) {
						if (i < curCountStr.length()) {
							countTime[maxNum - 1 - i] = curCountStr.substring(curCountStr.length() - i - 1, curCountStr.length() - i);
						} else {
							countTime[maxNum - 1 - i] = "";
						}
					}
					Image emptyImage = isRedImage ? redEmptyImage : blueEmptyImage;
					if (null != emptyImage) {
						widthHeightRate = (double) emptyImage.getBounds().width / emptyImage.getBounds().height;
						num_width = emptyImage.getBounds().width;
					}
					Rectangle bounds = control.getBounds();
					
					num_width = Math.min(bounds.width / maxNum, num_width);
					num_height = (int) ((num_width) / widthHeightRate);
					if (num_height > bounds.height) {
						num_height = bounds.height;
						num_width = (int) (bounds.height * widthHeightRate);
					}
					int x = (bounds.width - (maxNum - 1) * num_sep_width - maxNum * num_width) / 2;
					int y = Math.max((bounds.height - num_height) / 2, 0) + 1;
					for (int i = 0; i < countTime.length; i++) {
						String num = countTime[i];
						Image image = emptyImage;
						if (num.length() > 0) {
							image = ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/time/"+(isRedImage ? "red_" : "blue_") + num + ".png");
							arg0.gc.setAlpha(-1);
						} else {
							// arg0.gc.setAlpha(80);
						}
						if (null != image) {
							Rectangle rectangle = image.getBounds();
							arg0.gc.drawImage(image, 0, 0, rectangle.width, rectangle.height, x, y, num_width, num_height);
						}
						x += num_width;
						if (i > 0) {
							x += num_sep_width;
						}
					}
					arg0.gc.dispose();
				}
			};
			
			expertLabel.addPaintListener(paintListener);
			wasteNoLable.addPaintListener(paintListener);
			wasteYesLabel.setData("red", "red");
			wasteYesLabel.addPaintListener(paintListener);
			
			wasteYesRunnable = new Runnable(){
				public void run(){
					if(!wasteYesLabel.isDisposed() && wasteYesVoteCount > 0){
						wasteYesLabel.setData("count", wasteYesVoteCount +"");
						wasteYesLabel.redraw();
					}
				}
			};
			wasteNoRunnable = new Runnable(){
				public void run(){
					if(!wasteNoLable.isDisposed() && wasteNoVoteCount > 0){
						wasteNoLable.setData("count", wasteNoVoteCount +"");
						wasteNoLable.redraw();
					}
				}
			};
		}else {
			((GridData) countComposite.getLayoutData()).exclude = true;	
			countComposite.setVisible(false);
		}
		
		refreshTenderInfo();
	}
	
	
	/**
	 * @Description: 刷新项目信息
	 * @author wangxiang
	 */
	private void refreshTenderInfo() {
		
		new SyncThread(new SyncRunnable(tenderNameLabel){

			@Override
			public void threadRun() {
				tenderProjectEntity = tenderProjectService.getById(tenderProjectEntity.getId());
			}
			@Override
			public void asyncExec() {
				tenderNameLabel.setText(tenderProjectEntity.getTenderName());
				tenderIdLabel.setText("项目编号：" + tenderProjectEntity.getTenderNo());
				reasonText.setText(ObjectUtils.getObjString(tenderProjectEntity.getEffectiveMemo()));
				if (ObjectUtils.equals(TenderProjectEnum.EFFECTIVE_NO, tenderProjectEntity.getEffectiveType())) {
					wasteStausBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/waste_yes.png"));
				}else {
					wasteStausBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/waste_no.png"));
				}
				
				composite_4.layout();
				if (needVote) {
					refreshExpertVote();
				} else {
					((GridData)countComposite.getLayoutData()).exclude = true;
					countComposite.setVisible(false);
				}
				scrolledComposite_1.setMinSize(abandon_evaluation_table.computeSize(SWT.DEFAULT, SWT.DEFAULT));
			
			}
		}).start();
	}


	/**
	 * @Description: 刷新表决专家
	 * @author wangxiang
	 */
	private void refreshExpertVote() {
		
		expertCount = expertService.getEvaluationStepExpertCount(tenderProjectEntity.getId(),evaluationStep);
		wasteStausBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/wasting.png"));
		expertLabel.setData("count", expertCount+"");
		expertLabel.redraw();
//		final QueryWrapper queryWrapper = new QueryWrapper(ExpertEvaluationResult.class);
//		queryWrapper.setQueryParm("accessment");
//		queryWrapper.setWhereParam("tenderId", tenderProjectEntity.getId());
//		queryWrapper.setWhereParam("stepId", evaluationStep.getId());
		wasteYesVoteCount = 0;
		wasteNoVoteCount = 0;
		
		new Thread(new Runnable() {
			public void run() {
				refreshVote = true;
				while (refreshVote) {
					try {
//						expertEvaluationResultList = expertEvaluationResultService.queryList(queryWrapper, ExpertEvaluationResult.class);
						expertEvaluationResultList = expertEvaluationResultService.getStepAllExpertEvaluationResultList(tenderProjectEntity.getId(), evaluationStep.getId());
						if (null != expertEvaluationResultList) {
							int curWasteYesVoteCount = 0;
							int curWasteNoVoteCount = 0;
							for (ExpertEvaluationResult expertEvaluationResult : expertEvaluationResultList) {
								if (com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED.equals(expertEvaluationResult.getAccessment())) {
									curWasteYesVoteCount++;
								} else if (com.gx.obe.components.core.enumeration.EvaluationStepEnum.QUALIFIED.equals(expertEvaluationResult.getAccessment())) {
									curWasteNoVoteCount++;
								}
							}
							refreshVoteCount(expertCount, curWasteYesVoteCount, curWasteNoVoteCount);
							if(expertCount > 0 && curWasteYesVoteCount + curWasteNoVoteCount >= expertCount){
								refreshVote = false;
								evaluationStep.setStepStatus(CommonEnum.COMPLETE);
								evaluationStepService.updateAssignProperty(evaluationStep, new String[] { "stepStatus" });
								break;
							}
						}
						Thread.sleep(1000);
					} catch (Exception e) {
						refreshVote = false;
					}

				}
			}
		}).start();

	}

	/**
	 * @Description: 提交表决意见
	 * @author wangxiang
	 * @param accessment
	 */
	private void voteConfirmAction(String accessment) {
		ExpertEvaluationResult expertEvaluationResult = new ExpertEvaluationResult();
		expertEvaluationResult.setId(UuidUtils.getUUID());
		expertEvaluationResult.setStepId(evaluationStep.getId());
		expertEvaluationResult.setTenderId(tenderProjectEntity.getId());
		expertEvaluationResult.setUserId(CURRENT_EXPERT.getUserId());
		expertEvaluationResult.setAccessment(accessment);
		expertEvaluationResultService.insert(expertEvaluationResult);
		expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.COMPLETE, tenderProjectEntity.getId(), evaluationStep, CURRENT_EXPERT.getUserId());
		voteOperatorComposite.setVisible(false);
		((GridData) voteOperatorComposite.getLayoutData()).exclude = true;
		
		refreshVoteResult(accessment);
		right_composite.layout();
	}
	
	private void refreshVoteCount(int curExpertCount, int curWasteYesVoteCount, int curWasteNoVoteCount){
		boolean hasResult = false;
		if(curWasteYesVoteCount > expertCount / 2){
			tenderProjectEntity.setEffectiveType(TenderProjectEnum.EFFECTIVE_NO);
			hasResult = true;
			Display.getDefault().syncExec(new Runnable(){
				public void run(){
					if(!wasteStausBtn.isDisposed()){
						wasteStausBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/waste_yes.png"));
					}
				}
			});
		}else if(curWasteNoVoteCount > expertCount / 2){
			tenderProjectEntity.setEffectiveType(TenderProjectEnum.EFFECTIVE_YES);	
			hasResult = true;
			Display.getDefault().syncExec(new Runnable(){
				public void run(){
					if(!wasteStausBtn.isDisposed()){
						wasteStausBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/waste_no.png"));
					}
				}
			});
		}
		if(curWasteYesVoteCount != wasteYesVoteCount){
			wasteYesVoteCount = curWasteYesVoteCount;
			Display.getDefault().syncExec(wasteYesRunnable);
		}
		if(curWasteNoVoteCount != wasteNoVoteCount){
			wasteNoVoteCount = curWasteNoVoteCount;
			Display.getDefault().syncExec(wasteNoRunnable);
		}
		
		if(hasResult){
			tenderProjectService.updateAssignProperty(tenderProjectEntity, new String[] {"id", "effectiveType"});
		}
	}
	
	/**
	 * @Description: 返回
	 * @author wangxiang
	 */
	private void goBackAction() {
		refreshVote = false;
		if (null != expertEvaluationListener) {
			expertEvaluationListener.complete();
		}
	}

	public void addExpertEvaluationListener(ExpertEvaluationListener expertEvaluationListener) {
		this.expertEvaluationListener = expertEvaluationListener;
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
