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

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

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.MessageDialog;
import org.eclipse.swt.core.widgets.EButton;
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.core.widgets.RectangleRadioButton;
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.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.swtchart.ILineSeries;

import com.gx.obe.ColorConstants;
import com.gx.obe.action.bean.Action;
import com.gx.obe.business.entity.BidPriceRound;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.listener.IRefreshListener;
import com.gx.obe.common.widget.thread.SyncThread;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.components.core.enumeration.BidPriceEnum;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.config.ConfigConstants;
import com.gx.obe.config.utils.PropertiesUtils;
import com.gx.obe.evaluation.step.composite.HisBidPriceResultComposite;
import com.gx.obe.evaluation.step.composite.MultiBidPriceResultChartComposite;
import com.gx.obe.evaluation.step.listener.ExpertEvaluationListener;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.NumericChineseUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.util.utils.UuidUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.expert.Expert;
import com.gx.obe.web.entity.price.BidPriceHisResult;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;
import com.gx.obe.web.service.BidPriceHisResultService;
import com.gx.obe.web.service.BidPriceRoundStatusService;
import com.gx.obe.web.service.ReBidPriceRoundService;
import com.gx.obe.web.service.SupplierService;
import com.gx.obe.web.service.evaluation.EvaluationStepService;
import com.gx.obe.web.service.expert.ExpertEvaluationStepService;
import com.gx.obe.web.service.expert.ExpertService;
import com.gx.utils.ActionLogUtils;
import com.report.util.FreemarkerUtil;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

public class MultiRoundsPriceComposite extends Composite {
	private Composite leftComposite;
	private Composite rightComposite;
	private TenderProjectEntity tenderProjectEntity;
	private EvaluationStep evaluationStep;
	private ExpertEvaluationStepService expertEvaluationStepService;
	private SupplierService supplierService = new SupplierService();
	private EvaluationStepService evaluationStepService = new EvaluationStepService();
	private ExpertService expertService;
	private Grid supplierGrid;
	private List<Supplier> supplierList;
	private GridColumn supplierNameColumn;
	private Label supplierNameLabel;
	private Composite multiWhellPriceComposite;
	private Supplier selectSupplier;
	private EButton addbidpricebutton;
	private int maxRound = 1;
	private List<BidPriceHisResult> bidPriceHisResultList;
	private MultiBidPriceResultChartComposite hispricecomposite;
	private TenderProjectRuleEntity tenderProjectRuleEntity;
	private Expert CURRENT_EXPERT;
	private String supplierName;
	private Composite addcomposite;
	private Text roundNameexp;
	private Composite savecomposite;
	private EButton finishButton;
	private ImageButton curRoundButton;
	private String roundPriceName;
	private ExpertEvaluationListener expertEvaluationListener;
	private Composite titleComposite;
	private Composite unfoldComposite;
	private ImageButton hideOrShowSupplier_btn;
	private ILineSeries bisprices = null;
	private ILineSeries lowLineSeries = null;
	private ILineSeries hignseries = null;
	private ELink toPreSupplier;
	private ELink toNextSupplier;
	private Composite showComposite;
	private BidPriceHisResultService bidPriceHisResultService;
	private ReBidPriceRoundService reBidPriceRoundService;

	private BidPriceRoundStatusService bidPriceRoundStatusService;
	private Integer allowMaxRound;
	private Label tips;
	private Action projectAction;
	private String SUPPLIER_NAME;
	private Composite roundListComposite;
	private Composite roundComposite;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public MultiRoundsPriceComposite(Composite parent, Action projectAction, int style) {
		super(parent, style);
		this.projectAction = projectAction;
		GridLayout gridLayout = new GridLayout(3, false);
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		gridLayout.verticalSpacing = 0;
		setLayout(gridLayout);

		showComposite = new Composite(this, SWT.NONE);
		showComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_showComposite = new GridLayout(3, false);
		gl_showComposite.verticalSpacing = 0;
		gl_showComposite.horizontalSpacing = 0;
		gl_showComposite.marginHeight = 0;
		gl_showComposite.marginWidth = 0;
		showComposite.setLayout(gl_showComposite);

		leftComposite = new Composite(showComposite, SWT.BORDER);
		GridLayout gl_leftComposite = new GridLayout(1, false);
		gl_leftComposite.verticalSpacing = 0;
		gl_leftComposite.horizontalSpacing = 0;
		gl_leftComposite.marginHeight = 0;
		gl_leftComposite.marginWidth = 0;
		leftComposite.setLayout(gl_leftComposite);
		leftComposite.setBackgroundMode(SWT.INHERIT_FORCE);
		GridData gd_leftComposite = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1);
		gd_leftComposite.widthHint = 300;
		leftComposite.setLayoutData(gd_leftComposite);
		leftComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));

		supplierGrid = new Grid(leftComposite, SWT.V_SCROLL | SWT.H_SCROLL);
		supplierGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		supplierGrid.setHeaderVisible(true);
		supplierGrid.setAutoWidth(true);

		GridColumn indexColumn = new GridColumn(supplierGrid, SWT.CENTER);
		indexColumn.setText(MessageFormat.format(Messages.Supplier, Messages.Name));
		indexColumn.setWidth(50);
		indexColumn.setMaxmumWidth(50);
		indexColumn.setText(Messages.Num);

		supplierNameColumn = new GridColumn(supplierGrid, SWT.LEFT);
		supplierNameColumn.setText(MessageFormat.format(Messages.Supplier, Messages.Name));
		supplierNameColumn.setWidth(200);

		GridColumn roundColumn = new GridColumn(supplierGrid, SWT.CENTER);
		roundColumn.setText(Messages.Round);
		roundColumn.setWidth(50);
		roundColumn.setMaxmumWidth(80);
		roundColumn.setVisible(true);
		roundColumn.setVisible(false);

		unfoldComposite = new Composite(showComposite, SWT.NONE);
		unfoldComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1));
		unfoldComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_unfoldComposite = new GridLayout(1, false);
		gl_unfoldComposite.marginHeight = 0;
		gl_unfoldComposite.marginWidth = 0;
		unfoldComposite.setLayout(gl_unfoldComposite);

		hideOrShowSupplier_btn = new ImageButton(unfoldComposite, SWT.NONE);
		hideOrShowSupplier_btn.setBorderColor(ColorConstants.BORDER_LINE_COLOR);
		hideOrShowSupplier_btn.setBackgroundColor(SWTResourceManager.getColor(242, 242, 242));
		hideOrShowSupplier_btn.setActiveBackgroundColor(SWTResourceManager.getColor(255, 153, 0));
		hideOrShowSupplier_btn.setRightMargin(0);
		hideOrShowSupplier_btn.setTopMargin(50);
		hideOrShowSupplier_btn.setBottomMargin(50);
		hideOrShowSupplier_btn.setLeftMargin(0);
		hideOrShowSupplier_btn.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, true, 1, 1));
		hideOrShowSupplier_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				unFoldLeft();
			}
		});
		hideOrShowSupplier_btn
				.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/left_arrow.png"));

		rightComposite = new Composite(showComposite, SWT.NONE);
		rightComposite.setBackground(ColorConstants.BORDER_LINE_COLOR);
		rightComposite.setBackgroundMode(SWT.INHERIT_FORCE);
		GridLayout gl_rightComposite = new GridLayout(2, false);
		gl_rightComposite.verticalSpacing = 0;
		gl_rightComposite.horizontalSpacing = 1;
		gl_rightComposite.marginHeight = 1;
		gl_rightComposite.marginWidth = 1;
		rightComposite.setLayout(gl_rightComposite);
		rightComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		titleComposite = new Composite(rightComposite, SWT.NONE);
		titleComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_titleComposite = new GridLayout(3, false);
		titleComposite.setLayout(gl_titleComposite);
		titleComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));

		toPreSupplier = new ELink(titleComposite, SWT.NONE | EButton.CSS_BLUE);
		toPreSupplier.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		toPreSupplier.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		toPreSupplier.setText("<<" + Messages.PreviousSupplier);
//		toPreSupplier.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/white/pre.png"));
		toPreSupplier.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				previewAction();
			}
		});

		supplierNameLabel = new Label(titleComposite, SWT.CENTER);
		supplierNameLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		supplierNameLabel.setForeground(ColorConstants.FONT_BLACK_COLOR);
		supplierNameLabel.setFont(SWTResourceManager.getFont("微软雅黑", 11, SWT.BOLD));
		supplierNameLabel.setText(MessageFormat.format(Messages.Supplier, Messages.Name));

		toNextSupplier = new ELink(titleComposite, SWT.NONE | EButton.CSS_BLUE);
		toNextSupplier.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		toNextSupplier.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		toNextSupplier.setText(Messages.NextSupplier + ">>");
//		toNextSupplier.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/white/next.png"));
		toNextSupplier.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				nextAction();
			}
		});

		roundComposite = new Composite(rightComposite, SWT.NONE);
		roundComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_roundComposite = new GridLayout(4, false);
		gl_roundComposite.verticalSpacing = 0;
		gl_roundComposite.marginWidth = 0;
		gl_roundComposite.marginHeight = 0;
		roundComposite.setLayout(gl_roundComposite);
		roundComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));

		roundListComposite = new Composite(roundComposite, SWT.NONE);
		roundListComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		RowLayout rl_roundListComposite = new RowLayout(SWT.HORIZONTAL);
		rl_roundListComposite.marginBottom = 0;
		roundListComposite.setLayout(rl_roundListComposite);

		addbidpricebutton = new EButton(roundComposite, SWT.NONE | EButton.CSS_BLUE);
		addbidpricebutton.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		addbidpricebutton.setText(Messages.Button_add_1);
		addbidpricebutton
				.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/white/submit.png"));
		addbidpricebutton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		addbidpricebutton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				addNewRoundBidPrice();
			}
		});

		roundNameexp = new Text(roundComposite, SWT.BORDER);
		GridData gd_roundNameText = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_roundNameText.widthHint = 100;
		roundNameexp.setLayoutData(gd_roundNameText);
		roundNameexp.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		roundNameexp.setText(PropertiesUtils.getProperty("multyPriceName", "第${N}轮报价"));
		roundNameexp.setTextLimit(20);

		tips = new Label(roundComposite, SWT.RIGHT);
		tips.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		tips.setText("");

		ESepator subLine = new ESepator(roundComposite, SWT.NONE);
		subLine.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 4, 1));
		subLine.setBackground(ColorConstants.SKIN_BG);
		subLine.setLineWidth(2);

		multiWhellPriceComposite = new Composite(rightComposite, SWT.NONE);
		multiWhellPriceComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		multiWhellPriceComposite.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		multiWhellPriceComposite.setLayout(new FillLayout(SWT.HORIZONTAL));
		GridData gd_scrolledComposite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_scrolledComposite.widthHint = 120;
		multiWhellPriceComposite.setLayoutData(gd_scrolledComposite);

		hispricecomposite = new MultiBidPriceResultChartComposite(rightComposite, SWT.NONE);
		hispricecomposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridData gd_hispricecomposite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_hispricecomposite.widthHint = 100;
		hispricecomposite.setLayoutData(gd_hispricecomposite);

		savecomposite = new Composite(showComposite, SWT.NONE);
		savecomposite.setBackgroundMode(SWT.INHERIT_FORCE);
		savecomposite.setLayout(new GridLayout(1, false));
		savecomposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 3, 1));

		finishButton = new EButton(savecomposite, SWT.NONE | EButton.CSS_BLUE);
		GridData gd_finishButton = new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1);
		gd_finishButton.heightHint = 30;
		finishButton.setLayoutData(gd_finishButton);
		finishButton.setFont(SWTResourceManager.getFont("微软雅黑", 11, SWT.NORMAL));
		finishButton.setLeftMargin(10);
		finishButton.setRightMargin(10);
		finishButton.setText(Messages.Button_submitEvaluationResult);
		finishButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				submitEvaluationResultAction();
			}
		});

		init();
	}

	/**
	 * @Description: 初始化
	 * @author wangxiang
	 */
	private void init() {
		bidPriceRoundStatusService = new BidPriceRoundStatusService();
		expertEvaluationStepService = new ExpertEvaluationStepService();
		bidPriceHisResultService = new BidPriceHisResultService();
		reBidPriceRoundService = new ReBidPriceRoundService();
		expertService = new ExpertService();
		tenderProjectEntity = projectAction.getTenderProject();
		tenderProjectRuleEntity = projectAction.getTenderProjectRule();
		evaluationStep = projectAction.getEvaluationStep();
		supplierGrid.setItemHeight(Constants.ROW_HIGHT);
		supplierGrid.setAutoHeight(true);
		supplierGrid.setWordWrapHeader(true);
		supplierGrid.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				loadRoundBidPriceResult();
			}
		});
		SUPPLIER_NAME = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME,
				tenderProjectEntity.getTenderType());
		supplierNameColumn.setText(MessageFormat.format(Messages.NameFormat, SUPPLIER_NAME));
		this.CURRENT_EXPERT = expertService.getEvaluationStepExpert(tenderProjectEntity.getId(), evaluationStep);

		allowMaxRound = evaluationStep.getEvaluationParamByKey(ConfigConstants.MAX_ROUND, Integer.class, 3);
		if (allowMaxRound > 0) {
			tips.setText(MessageFormat.format(Messages.Tips_exceedMaxBidPriceRound, allowMaxRound));
		}

		finishButton.setText(MessageFormat.format(Messages.CompleteFormat, evaluationStep.getStepName()));
		if (null == evaluationStep.getStepStatus() || !evaluationStep.getStepStatus().equals(CommonEnum.IN_PROGRESS)) {
			// 修改状态为正在进行中
			ActionLogUtils.addServerLog(tenderProjectEntity.getId(), "进入" + evaluationStep.getStepName(), Constants.USER.getUserName());
			expertEvaluationStepService.updateExpertEvaluationStatus(CommonEnum.IN_PROGRESS,
					tenderProjectEntity.getId(), evaluationStep, CURRENT_EXPERT.getUserId());
		}
		refreshBidPriceRound();
		refreshSuppliers();
		refreshBtnStatus();
	}

	private ImageButton createRoundButton(final BidPriceRound bidPriceRound) {
		final ImageButton roundButton = new RectangleRadioButton(roundListComposite, SWT.NONE);
		roundButton.setText(ObjectUtils.getObjString(bidPriceRound.getRoundName()));
		roundButton.setData("bidPriceRound", bidPriceRound);
		roundButton.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		roundButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				if (null != curRoundButton && !curRoundButton.isDisposed()) {
					curRoundButton.setSelection(false);
				}
				curRoundButton = roundButton;
				curRoundButton.setSelection(true);
				loadRoundBidPriceResult();
			}
		});
		return roundButton;
	}

	private void refreshBidPriceRound() {
		for (Control control : roundListComposite.getChildren()) {
			control.dispose();
		}
		List<BidPriceRound> bidPriceRoundList = reBidPriceRoundService
				.getBidPriceRoundList(tenderProjectEntity.getId());
		if (null == bidPriceRoundList || bidPriceRoundList.size() == 0) {
			BidPriceRound lastBidPriceRound = new BidPriceRound();
			lastBidPriceRound.setRoundName(Messages.OriginalBidPrice);
			lastBidPriceRound.setRoundNo(1);
			bidPriceRoundList.add(lastBidPriceRound);
		}
		if (null != bidPriceRoundList && bidPriceRoundList.size() > 0) {
			maxRound = bidPriceRoundList.size();
//			maxRound =  reBidPriceRoundService.getMaxRound(tenderProjectEntity.getId());
			for (BidPriceRound bidPriceRound : bidPriceRoundList) {
				curRoundButton = createRoundButton(bidPriceRound);
			}

		}
		roundListComposite.layout();
		roundComposite.layout();
		rightComposite.layout();
		curRoundButton.notifyListeners(SWT.MouseUp, null);
	}

	/**
	 * refreshSupplierTable:获取当前评标步骤下的供应商列表
	 * 
	 * @author wangxiang
	 */
	private void refreshSuppliers() {
		supplierList = supplierService.getEvaluationStepSupplierList(tenderProjectEntity.getId(), evaluationStep);
		supplierGrid.removeAll();
		if (null != supplierList && supplierList.size() > 0) {
			Map<String, String> supplierMaxPriceRoundMap = bidPriceHisResultService.getSupplierMaxPriceRoundMap(tenderProjectEntity.getId(), null);
			int index = 1;
			for (Supplier supplier : supplierList) {
				String roundNum = supplierMaxPriceRoundMap.get(supplier.getId());
				Integer roundInt = Integer.valueOf(null != roundNum ? roundNum :"0");
				if (null == roundNum || 0 == roundInt) {
					roundNum = "1";
				}
				GridItem item = new GridItem(supplierGrid, SWT.NONE);
				item.setText(0, (index++) + "");
				item.setText(1, supplier.getSupplierName());
				item.setText(2, roundNum);
				item.setData("supplier", supplier);
				item.setData("isSubmit", supplier);
			}
		}
		supplierGrid.setSelection(0);
		loadRoundBidPriceResult();
	}

	/**
	 * @Description: 获得项目下指定轮次报价供应商报价完成情况
	 * @author guoyr
	 */
	private void refreshComplateStatus(final BidPriceRound curbidPriceRound) {
		if (curbidPriceRound.getRoundNo() == 1) {
			for (GridItem gridItem : supplierGrid.getItems()) {
				Supplier supplier = (Supplier) gridItem.getData("supplier");
				if (null != supplier) {
					gridItem.setImage(1,
							ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/submit.png"));
				}
			}
			return;
		}

		SyncThread syncThread = new SyncThread(new SyncThread.Runnable() {
			public void run() {
				final Map<String, String> resultMap = bidPriceRoundStatusService
						.getSupplierBidPriceRoundStatus(curbidPriceRound.getId());

				Display.getDefault().syncExec(new Runnable() {
					public void run() {
						if (!supplierGrid.isDisposed()) {
							for (GridItem gridItem : supplierGrid.getItems()) {
								Supplier supplier = (Supplier) gridItem.getData("supplier");
								if (null != supplier) {
									if (resultMap.containsKey(supplier.getId())) {
										if (null != resultMap.get(supplier.getId()) && BidPriceEnum.PRICE_STATUS_OK
												.equals(resultMap.get(supplier.getId()))) {
											gridItem.setImage(1, ResourceManager.getPluginImage(
													IconsActivator.PLUGIN_ID, "icons/btn/blue/submit.png"));
										} else {
											gridItem.setImage(1, ResourceManager.getPluginImage(
													IconsActivator.PLUGIN_ID, "icons/btn/blue/hourglass.png"));
										}
									} else {
										gridItem.setImage(1, ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID,
												"icons/btn/blue/warning.png"));
									}
								}
							}
						}
					}
				});
			}
		});
		syncThread.start();

	}

	/**
	 * @Description: 选择供应商事件
	 * @author wangxiang
	 */
	private void loadRoundBidPriceResult() {
		if (null == curRoundButton || curRoundButton.isDisposed()) return;
		for (Control control : multiWhellPriceComposite.getChildren()) {
			control.dispose();
		}
		selectSupplier = null;
		if (supplierGrid.getSelectionCount() > 0) {
			selectSupplier = (Supplier) supplierGrid.getSelection()[0].getData("supplier");
		}
		BidPriceRound curbidPriceRound = (BidPriceRound) curRoundButton.getData("bidPriceRound");
		
		if (null != selectSupplier && null != curbidPriceRound) {
			addbidpricebutton.setEnabled(true);
			supplierName = selectSupplier.getSupplierName();
			supplierNameLabel.setText(supplierName);
//			int round = curbidPriceRound.getRoundNo();
//			if(curbidPriceRound.getRoundNo() == maxRound){
//				round = 0;
//			}
			refreshComplateStatus(curbidPriceRound);
			HisBidPriceResultComposite hisBidPriceResultComposite = new HisBidPriceResultComposite(multiWhellPriceComposite, SWT.NONE);
			hisBidPriceResultComposite.refresh(tenderProjectEntity, tenderProjectRuleEntity, selectSupplier, curbidPriceRound,maxRound, new IRefreshListener() {
				@Override
				public void refresh(BidPriceRound curbidPriceRound) {
					refreshComplateStatus(curbidPriceRound);
					refreshBidPriceChart(curbidPriceRound);
				}
			});
			multiWhellPriceComposite.layout();
			refreshBtnStatus();
			refreshBidPriceChart(curbidPriceRound);
		}
	}

	/**
	 * @Description: 下一家
	 * @author wangxiang
	 */
	private void nextAction() {
		if (toNextSupplier.getEnabled()) {
			int selectIndex = supplierGrid.getSelectionIndex();
			if (selectIndex < supplierGrid.getItemCount()) {
				if (null == selectSupplier) {
					supplierGrid.setSelection(selectIndex);
				} else {
					supplierGrid.setSelection(selectIndex + 1);
				}
				loadRoundBidPriceResult();
//				cutsupComposite.layout();
				Rectangle rectangle = supplierGrid.getBounds();
				supplierGrid.scroll(0, 0, 0, 0, rectangle.width, rectangle.height, true);
				supplierGrid.getVerticalBar().setSelection(supplierGrid.getSelectionIndex());
				supplierGrid.getVerticalBar().notifyListeners(SWT.Selection, null);
				refreshBtnStatus();
			}

		}
	}

	/**
	 * @Description: 上一家
	 * @author wangxiang
	 */
	private void previewAction() {
		if (toPreSupplier.getEnabled()) {
			int selectIndex = supplierGrid.getSelectionIndex();
			if (selectIndex > 0) {
				supplierGrid.setSelection(selectIndex - 1);
				loadRoundBidPriceResult();
				Rectangle rectangle = supplierGrid.getBounds();
				supplierGrid.scroll(0, 0, 0, 0, rectangle.width, rectangle.height, true);
				supplierGrid.getVerticalBar().setSelection(supplierGrid.getSelectionIndex());
				supplierGrid.getVerticalBar().notifyListeners(SWT.Selection, null);
				refreshBtnStatus();
			}
		}
	}

	/**
	 * @Description: 上一家 、下一家 菜单控制
	 * @author mazc
	 */
	private void refreshBtnStatus() {
		int selectIndex = supplierGrid.getSelectionIndex();
		toPreSupplier.setEnabled(true);
		toNextSupplier.setEnabled(true);
		if (selectIndex <= 0) {
			toPreSupplier.setEnabled(false);
		}
		if (selectIndex == supplierGrid.getItemCount() - 1) {
			toNextSupplier.setEnabled(false);
		}
	}

//	/**
//	 * @Description: 切换为结果界面
//	 * @author wangxiang 
//	 */
//	private void switchResultAction() {
//		this.dispose();
//		parent.layout();
//		
//		MultiRoundsPriceResultComposite  multiRoundsPriceResultComposite = new MultiRoundsPriceResultComposite(parent,SWT.None);
//		multiRoundsPriceResultComposite.refresh(tenderProjectEntity, tenderProjectRuleEntity, evaluationStep);
//		parent.layout();
//	}

	/**
	 * @Description: 供应商展开收缩
	 * @author wangxiang
	 */
	private void unFoldLeft() {
		if (supplierGrid.isVisible()) {
			((GridData) leftComposite.getLayoutData()).exclude = true;
			leftComposite.setVisible(false);
			hideOrShowSupplier_btn
					.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/right_arrow.png"));
		} else {
			((GridData) leftComposite.getLayoutData()).exclude = false;
			leftComposite.setVisible(true);
			hideOrShowSupplier_btn
					.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/left_arrow.png"));
		}
		showComposite.layout();
	}

	/**
	 * @Description: 刷新报价的图标
	 * @author wangxiang
	 */
	private void refreshBidPriceChart(BidPriceRound curbidPriceRound) {
		hispricecomposite.refresh(tenderProjectEntity, selectSupplier, true, true, curbidPriceRound);
		hispricecomposite.setMaxAngleCount(4);
	}

	/**
	 * @Description: 验证是否全部完成报价
	 * @author guoyr
	 * @return
	 */
	private boolean valComplateBidPrice() {
		// 这里判断最新轮次供应商有没有全部完成报价并进行提示
		int noComplateCount = 0;
		Map<String, String> resultMap = bidPriceRoundStatusService
				.getBidPriceRoundComplateStatus(tenderProjectEntity.getId());
		if (null == resultMap) {
			return false;
		}
		for (Entry<String, String> entry : resultMap.entrySet()) {
			if (null == entry.getValue() || !BidPriceEnum.PRICE_STATUS_OK.equals(entry.getValue())) {
				noComplateCount++;
			}
		}
		if (noComplateCount > 0) {
			MessageDialog.openShortMessage(
					MessageFormat.format(Messages.Tips_notComplateQuotedBidPrice, noComplateCount, SUPPLIER_NAME));
			return false;
		}
		return true;
	}

	/**
	 * @Description: 添加新一轮报价
	 * @author wangxiang
	 */
	private void addNewRoundBidPrice() {
		if (!addbidpricebutton.getEnabled()) {
			return;
		}
		addbidpricebutton.setEnabled(false);
		roundPriceName = roundNameexp.getText().trim();
		if (!PropertiesUtils.getProperty("multyPriceName", "第${N}轮报价").equals(roundPriceName)) {
			PropertiesUtils.saveProperty("multyPriceName", roundPriceName);
		}
		if (null == selectSupplier) {
			return;
		}

		if (!valComplateBidPrice()) {
			return;
		}
		// 这里判断是否超过允许报价的最大轮次
		if (allowMaxRound > 0 && maxRound + 1 > allowMaxRound) {
			MessageDialog.openInformation(getShell(),
					MessageFormat.format(Messages.Tips_exceedMaxBidPriceRound, allowMaxRound));
			return;
		}

		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put("N", NumericChineseUtils.getChinese(maxRound + 1));
		mapData.put("n", maxRound + 1);
		String newRoundName = FreemarkerUtil.getFreemarkerString(roundPriceName, mapData);
		if (StringUtils.isEmpty(newRoundName)) {
			return;
		}

		if (!MessageDialog.openConfirm(getShell(), newRoundName,
				MessageFormat.format(Messages.Tips_confirmOperate, newRoundName),
				new String[] { Messages.Button_confirm, Messages.Button_cancel })) {
			return;
		}
		final BidPriceRound bidPriceRound = new BidPriceRound();
		bidPriceRound.setId(UuidUtils.getUUID());
		bidPriceRound.setRoundName(newRoundName);
		bidPriceRound.setRoundNo(maxRound + 1);
		bidPriceRound.setTenderId(tenderProjectEntity.getId());
		bidPriceRound.setRoundStatus(com.gx.obe.components.core.enumeration.EvaluationStepEnum.INCROSS);
		bidPriceRound.setRoundEndTime(null);
//		List<BidPriceHisResult> lastBidPriceResultList = bidPriceHisResultService.getSupplierBidPriceHisResultListByRound(tenderProjectEntity.getId(), null, 0);
		SyncThread syncThread = new SyncThread(new SyncThread.Runnable() {
			public void run() {
				final boolean success = reBidPriceRoundService.insertNewLocalBidpriceRound(bidPriceRound,
						tenderProjectEntity.getId(), supplierList);
				Display.getDefault().syncExec(new Runnable() {
					public void run() {
						if (!roundListComposite.isDisposed()) {
							if (success) {
								refreshBidPriceRound();
							} else {
								MessageDialog.openWarning(getShell(), Messages.Tips_operateFail);
							}
						}
					}
				});
			}
		});
		syncThread.setMessage(Messages.Tips_requesting);
		syncThread.start();

	}

	/**
	 * @Description: 结束多轮报价
	 * @author wangxiang
	 */
	private void submitEvaluationResultAction() {
		if (null != finishButton && !finishButton.isDisposed()) {
			if (!valComplateBidPrice()) {
				return;
			}

			// 检查是否存在当前评标步骤之前未完成的步骤
			boolean isFreeProgress = ObjectUtils.equals(tenderProjectRuleEntity.getOrderReview(),
					com.gx.obe.components.core.enumeration.EvaluationStepEnum.ORDER_SELF_FREE);
			if (isFreeProgress) {
				List<EvaluationStep> evaluationStepList = evaluationStepService
						.getNotComplateStepList(tenderProjectEntity.getId(), evaluationStep.getSortNo());
				if (null != evaluationStepList && evaluationStepList.size() > 0) {
					MessageDialog.openWarning(getShell(), Messages.Dialog_warmTips, MessageFormat
							.format(Messages.Tips_saveAllComplate, evaluationStepList.get(0).getStepName()));
					return;
				}
			}
			String message = MessageFormat.format(Messages.Tips_submitEvaluationResult, evaluationStep.getStepName());
			if (!MessageDialog.openConfirm(getShell(), Messages.Button_submitEvaluationResult, message,
					new String[] { Messages.Button_confirmComplate, Messages.Button_continueEvaluation })) {
				return;
			}
			if (expertEvaluationStepService.submitMultiPriceResult(tenderProjectEntity.getId(),
					CURRENT_EXPERT.getUserId(), evaluationStep)) {
				// 服务端修改 成功后跟新当前步骤状态
				evaluationStep.setStepStatus(CommonEnum.COMPLETE);
				goBackAction();
			} else {
				MessageDialog.openInformation(getShell(), "结果保存失败！");
			}
		}
	}

	private void goBackAction() {
		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
	}
}
