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

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.ImageResolver;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.calendar.CalendarUtil;
import org.eclipse.swt.core.listener.LoadingAdatper;
import org.eclipse.swt.core.listener.PaintImageListener;
import org.eclipse.swt.core.utils.GridUtils;
import org.eclipse.swt.core.utils.WidgetUtils;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.EDialog;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
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.Shell;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
import com.gx.obe.business.entity.BidPriceRound;
import com.gx.obe.business.entity.MenuFunction;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.dialog.BidPriceItemDialog;
import com.gx.obe.common.widget.shell.ProgressMessageDialog;
import com.gx.obe.common.widget.thread.SyncThread;
import com.gx.obe.common.widget.utils.MenuFunctionUtils;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.enumeration.BidPriceEnum;
import com.gx.obe.config.ConfigConstants;
import com.gx.obe.config.utils.PropertiesUtils;
import com.gx.obe.evaluation.step.thread.ApplyReBidPriceThread;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.DateUtils;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.NumericChineseUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.price.BidPrice;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;
import com.gx.obe.web.service.BidPriceService;
import com.gx.obe.web.service.SupplierService;
import com.gx.utils.NowTimeUtils;
import com.report.util.FreemarkerUtil;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 启动新一轮报价
 * @author wangxiang
 */
public class ApplyReBidPriceDialog extends EDialog {
	private EButton save_btn;
	private ImageResolver imageResolver = new ImageResolver();
	private SupplierService supplierService = new SupplierService();
	private BidPriceService bidPriceService ;
	private TenderProjectEntity tenderProjectEntity;
	private Text stepMemoText;
	private Composite composite;
	private CLabel roundLabel;
	private Text timeText;
	private CallBack addCallBack;
	private Text roundName;
	private TenderProjectRuleEntity tenderProjectRuleEntity;
	private ELink addBidPriceItemBtn;
	private ELink delBidPriceItemBtn;
	private int newRound;
	private Grid itemListGrid;
	private List<BidPrice> bidPriceList;
	private int selectionIndex = 0;
	private BidPrice curBidPrice;
	private int intervalsTime;
	private MenuFunctionUtils menuFunctionUtils;
	private Grid supplierGrid;
	private List<Supplier> supplierList;
	private EvaluationStep evaluationStep;
	{
		LogUtils.logClass(ApplyReBidPriceDialog.class);
	}

	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public ApplyReBidPriceDialog(Shell parentShell, TenderProjectEntity tenderProjectEntity,TenderProjectRuleEntity tenderProjectRuleEntity, int newRound, MenuFunctionUtils menuFunctionUtils,EvaluationStep evaluationStep, CallBack callBack) {
		super(parentShell);
		setTitle("启动新轮次报价");
		this.tenderProjectEntity = tenderProjectEntity;
		this.tenderProjectRuleEntity = tenderProjectRuleEntity;
		this.menuFunctionUtils = menuFunctionUtils;
		this.newRound = newRound;
		this.addCallBack = callBack;
		this.evaluationStep = evaluationStep;
	}

	/**
	 * 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();
			}
		});
		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);
		
		Composite composite_3 = new Composite(container, SWT.NONE);
		GridLayout gl_composite_3 = new GridLayout(1, false);
		gl_composite_3.marginHeight = 2;
		gl_composite_3.marginWidth = 2;
		composite_3.setLayout(gl_composite_3);
		composite_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		
		
		composite = new Composite(composite_3, SWT.NONE);
		GridLayout gl_composite = new GridLayout(2, false);
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		composite.setLayout(gl_composite);
		GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_composite.heightHint = 95;
		composite.setLayoutData(gd_composite);
		
		Label label_1 = new Label(composite, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_1.setText("报价名称：");
		
		roundName = new Text(composite, SWT.BORDER);
		roundName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		CLabel label = new CLabel(composite, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText("当前轮次：");
		
//		Composite roundComposite = new Composite(composite, SWT.NONE);
//		GridLayout gl_roundComposite = new GridLayout(3, false);
//		gl_roundComposite.marginHeight = 0;
//		gl_roundComposite.marginWidth = 0;
//		roundComposite.setLayout(gl_roundComposite);
//		roundComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
//		
//		CLabel label_11 = new CLabel(roundComposite, SWT.NONE);
//		label_11.setText("第 ");
//		
		roundLabel = new CLabel(composite, SWT.NONE);
		roundLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		roundLabel.setForeground(ColorConstants.FONT_BLUE_COLOR);
		roundLabel.setText("");
//		
//		CLabel label_13 = new CLabel(roundComposite, SWT.NONE);
//		label_13.setText(" 轮报价");
		
		CLabel label_12 = new CLabel(composite, SWT.NONE);
		label_12.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_12.setText("报价截止时间：");
		
		Composite timeComposite = new Composite(composite, SWT.NONE);
		GridLayout gl_timeComposite = new GridLayout(1, false);
		gl_timeComposite.marginHeight = 0;
		gl_timeComposite.marginWidth = 0;
		timeComposite.setLayout(gl_timeComposite);
		timeComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		timeText = new Text(timeComposite, SWT.BORDER | SWT.READ_ONLY);
		GridData gd_timeText = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_timeText.widthHint = 150;
		timeText.setLayoutData(gd_timeText);
		timeText.addPaintListener(new PaintImageListener(imageResolver.loadImage(IconsActivator.PLUGIN_ID, "icons/data.png")));
		timeText.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				CalendarUtil.getDates(timeText,null != timeText.getData("date")?(Date)timeText.getData("date"):null, "yyyy-MM-dd HH:mm:00");
				Date time = (Date) timeText.getData("date");
				if(null != time){
					int iMinute = 10;
					Date serviceTime = NowTimeUtils.getNowDate();
					Calendar c = Calendar.getInstance();  
					c.setTime(serviceTime);  
					c.add(Calendar.MINUTE,intervalsTime); //大于服务器时间10分钟（10分钟仅为目前测试使用，实际时间应该根据需求调整进行）
					serviceTime = c.getTime();		
					if(time.compareTo(serviceTime) < 0){
						MessageDialog.openWarning(getShell(), "您设置的报价时间不足"+ iMinute +"分钟，请设置合理的报价截止时间！");
						timeText.setText("");
						timeText.setData("date", null);
					}
				}
			}
		});
		CLabel label_5 = new CLabel(composite, SWT.NONE);
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_5.setText(Messages.NodeNote + "：");
		
		stepMemoText = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		GridData gd_stepMemoText = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		gd_stepMemoText.heightHint = 31;
		stepMemoText.setLayoutData(gd_stepMemoText);
		stepMemoText.setTextLimit(100);
		
		Composite detailComposite = new Composite(container, SWT.BORDER);
		GridLayout gl_detailComposite = new GridLayout(1, false);
		gl_detailComposite.verticalSpacing = 1;
		gl_detailComposite.horizontalSpacing = 1;
		gl_detailComposite.marginHeight = 0;
		gl_detailComposite.marginWidth = 0;
		detailComposite.setLayout(gl_detailComposite);
		GridData gd_detailComposite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_detailComposite.widthHint = 200;
		detailComposite.setLayoutData(gd_detailComposite);
		
		
		
		Composite composite_16 = new Composite(detailComposite, SWT.NONE);
		composite_16.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
 		GridLayout gl_composite_16 = new GridLayout(3, false);
		gl_composite_16.marginHeight = 0;
		gl_composite_16.marginWidth = 0;
		composite_16.setLayout(gl_composite_16);
		
		CLabel currencyLabel_1 = new CLabel(composite_16, SWT.NONE);
		currencyLabel_1.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
		currencyLabel_1.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		currencyLabel_1.setText("请选择待报价的供应商");
		
		ELink selectAllBtn = new ELink(composite_16, SWT.NONE);
		selectAllBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				selectSupplierAction(true);
 			}
		});
		selectAllBtn.setText(Messages.Button_selectAll);
		
		ELink reverseBtn = new ELink(composite_16, SWT.NONE);
		reverseBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				selectSupplierAction(false);
			}
		});
		reverseBtn.setText(Messages.Button_selectInvert);
 		
		ESepator sep1 = new ESepator(composite_16, SWT.NONE);
		sep1.setSize(255, 1);
		sep1.setBackground(ColorConstants.BORDER_LINE_COLOR);
		
		
		supplierGrid = new Grid(detailComposite, SWT.V_SCROLL);
		supplierGrid.setHeaderVisible(true);
		supplierGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		supplierGrid.setItemHeight(Constants.ROW_HIGHT);
		GridColumn gridColumn_11 = new GridColumn(supplierGrid, SWT.CHECK);
		gridColumn_11.setText("供应商名称");
 		
		
		Composite composite_2 = new Composite(composite_3, SWT.BORDER);
 		GridLayout gl_detailComposite2 = new GridLayout(1, false);
 		gl_detailComposite2.verticalSpacing = 1;
 		gl_detailComposite2.horizontalSpacing = 1;
 		gl_detailComposite2.marginHeight = 0;
 		gl_detailComposite2.marginWidth = 0;
		composite_2.setLayout(gl_detailComposite2);
		GridData gd_detailComposite1 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_detailComposite1.widthHint = 200;
		composite_2.setLayoutData(gd_detailComposite1);
		
		Composite composite_18 = new Composite(composite_2, SWT.NONE);
		composite_18.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite_18.setSize(310, 29);
		GridLayout gl_composite_18 = new GridLayout(3, false);
		gl_composite_18.marginHeight = 0;
		gl_composite_18.marginWidth = 0;
		composite_18.setLayout(gl_composite_18);
		
		CLabel currencyLabel = new CLabel(composite_18, SWT.NONE);
		currencyLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
		currencyLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		currencyLabel.setText(Messages.BidOpeningList);
		
		addBidPriceItemBtn = new ELink(composite_18, SWT.NONE);
		addBidPriceItemBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/add.png"));
		addBidPriceItemBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				addBidPriceItemAction();
			}
		});
		addBidPriceItemBtn.setText(Messages.Button_add);
		addBidPriceItemBtn.setData(MenuFunction.FUNCTION, "ADD_OPENING_LIST");
		
		delBidPriceItemBtn = new ELink(composite_18, SWT.NONE);
		delBidPriceItemBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/delete.png"));
		delBidPriceItemBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				delBidPriceItemAction();
			}
		});
		delBidPriceItemBtn.setText(Messages.Button_delete);
		delBidPriceItemBtn.setData(MenuFunction.FUNCTION, "DELETE_OPENING_LIST");
		
		ESepator sep = new ESepator(composite_2, SWT.NONE);
		sep.setSize(255, 1);
		sep.setBackground(ColorConstants.BORDER_LINE_COLOR);
		
		itemListGrid = new Grid(composite_2, SWT.H_SCROLL | SWT.V_SCROLL);
		GridData gd_itemListGrid = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1);
		gd_itemListGrid.heightHint = 94;
		itemListGrid.setLayoutData(gd_itemListGrid);
		itemListGrid.setSize(255, 274);
		itemListGrid.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				itemListGridSelectedAction();
			}
		});
		itemListGrid.setHeaderVisible(true);
		itemListGrid.setItemHeight(Constants.ROW_HIGHT);
		
		GridColumn gridColumn = new GridColumn(itemListGrid, SWT.CENTER);
		gridColumn.setText(Messages.Num);
		gridColumn.setWidth(30);
		gridColumn.setMaxmumWidth(60);
		
		GridColumn gridColumn_1 = new GridColumn(itemListGrid, SWT.NONE);
		gridColumn_1.setText(Messages.Name);
		gridColumn_1.setWidth(200);
		
		GridColumn gridColumn_2 = new GridColumn(itemListGrid, SWT.CENTER);
		gridColumn_2.setText(Messages.BidPriceType);
		gridColumn_2.setWidth(50);
		
		GridColumn gridColumn_3 = new GridColumn(itemListGrid, SWT.CENTER);
		gridColumn_3.setText(Messages.PriceType);
		gridColumn_3.setWidth(70);
		gridColumn_3.setVisible(false);
		
		GridColumn gridColumn_4 = new GridColumn(itemListGrid, SWT.CENTER);
		gridColumn_4.setText(Messages.IncludedInTheTotalPriceType);
		gridColumn_4.setWidth(50);
		gridColumn_4.setVisible(false);
		ESepator but_sep = new ESepator(topContainer, SWT.SEPARATOR | SWT.HORIZONTAL);
		but_sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		but_sep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		Composite composite_1 = new Composite(topContainer, SWT.NONE);
		composite_1.setBackgroundMode(SWT.INHERIT_FORCE);
		composite_1.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_bg.png"));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 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(){
		intervalsTime = Integer.valueOf(PropertiesUtils.getProperty(ConfigConstants.INTERVALS_TIME, "10"));
		bidPriceService = new BidPriceService();
//		String supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProjectEntity.getTenderType());
		
		String roundPriceTemplate = PropertiesUtils.getProperty("multyPriceName", "第${N}轮报价");
		roundLabel.setText(NumericChineseUtils.getChinese(newRound));
		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put("N", NumericChineseUtils.getChinese(newRound));
		mapData.put("n", newRound);
		String newRoundName = FreemarkerUtil.getFreemarkerString(roundPriceTemplate, mapData);
		roundName.setText(ObjectUtils.getObjString(newRoundName));
		refreshItemListGrid();
		refreshSupplier();
		if(null != menuFunctionUtils && MenuFunction.SHOW.equals(menuFunctionUtils.getAuthority((String)addBidPriceItemBtn.getData(MenuFunction.FUNCTION)))) {
			WidgetUtils.setControlVisible(addBidPriceItemBtn, true);
		}else {
			WidgetUtils.setControlVisible(addBidPriceItemBtn, false);
		}
		if(null != menuFunctionUtils && MenuFunction.SHOW.equals(menuFunctionUtils.getAuthority((String)delBidPriceItemBtn.getData(MenuFunction.FUNCTION)))) {
			WidgetUtils.setControlVisible(addBidPriceItemBtn, true);
		}else {
			WidgetUtils.setControlVisible(delBidPriceItemBtn, false);
		}
	}
	
	private void refreshSupplier() {
 		SyncThread syncThread = new SyncThread(new SyncThread.Runnable(){
			public void run() {
		 		supplierList = supplierService.getEvaluationStepSupplierList(tenderProjectEntity.getId(),evaluationStep);
				Display.getDefault().syncExec(new Runnable(){
					public void run(){
						if(!supplierGrid.isDisposed()){
							refreshSupplierList();
						}
					}
				});
			}
		});
 		syncThread.start();
	}
	
	private void refreshSupplierList() {
 		supplierGrid.removeAll();
 		int index = 1;
		for (final Supplier supplier : supplierList) {
			final GridItem gridItem = new GridItem(supplierGrid, SWT.NONE);
			gridItem.setFont(0, SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
			gridItem.setData("supplierId", supplier.getId());
			gridItem.setData("supplier", supplier);
			gridItem.setText(0, index + "、" + supplier.getSupplierName());
 			index ++;
		}
	
	}
	
	/**
	 * @Description: 删除分项明细
	 * @author wangx
	 */
	private void delBidPriceItemAction(){
		if(!delBidPriceItemBtn.getEnabled() || !delBidPriceItemBtn.getVisible()){
			return ;
		}
//		int selectIndex = bidPriceItem_grid.getSelectionIndex();
//		GridItem selectItem = bidPriceItem_grid.getSelection()[0];
//		if(bidPriceItemService.deleteBidPriceItem(((BidPriceItem)bidPriceItem_grid.getSelection()[0].getData()).getId())){
//			bidPriceItemList.remove((BidPriceItem)bidPriceItem_grid.getSelection()[0].getData());
//			selectItem.dispose();
//			if(selectIndex <= bidPriceItem_grid.getItemCount()-1){
//				bidPriceItem_grid.setSelection(selectIndex);
//			}else {
//				bidPriceItem_grid.setSelection(selectIndex -1);
//			}
//		}else {
//			MessageDialog.openInformation(getShell(), "删除失败！");
//		}
	}

	/**
	 * @Description: 添加分项明细
	 * @author wangx
	 */
	private void addBidPriceItemAction(){
		if(!addBidPriceItemBtn.getEnabled() || !addBidPriceItemBtn.getVisible()){
			return ;
		}
		BidPriceItemDialog dialog = new BidPriceItemDialog(getShell(), bidPriceList,tenderProjectEntity.getId());
		dialog.setTitle(Messages.Button_add);
		dialog.open();
		refreshItemListGrid();
	}
	
	
	/**
	 * @Description:刷新itemListGrid
	 * @author chenxw
	 */
	private void refreshItemListGrid() {
		bidPriceList = null;
		itemListGrid.removeAll();
		SyncThread syncThread = new SyncThread(new SyncThread.Runnable(){
			public void run() {
				bidPriceList = bidPriceService.getTenderBidPriceTableList(tenderProjectEntity.getId());
				Display.getDefault().syncExec(new Runnable(){
					public void run(){
						if(!itemListGrid.isDisposed()){
							refreshBidPriceList();
						}
					}
				});
			}
		});
//		syncThread.showMessage(Messages.Tips_loading);
		syncThread.start();
	}
	
	/**
	 * @Description:初始化itemListGrid数据
	 * @author chenxw
	 */
	private void refreshBidPriceList() {
		int index = 1;
		if(null != bidPriceList){
			for (BidPrice bidPrice : bidPriceList) {
				GridItem gridItem = new GridItem(itemListGrid, SWT.NONE);
				gridItem.setData("bidPrice", bidPrice);
				gridItem.setText(0, index++ + "");
				gridItem.setText(1, ObjectUtils.getObjString(bidPrice.getBidPriceName()));
				if (BidPriceEnum.IS_PRICE_ITEM.equals(bidPrice.getBidPriceType())) {
					gridItem.setText(2, Messages.PriceItem);
					gridItem.setImage(2, ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/money-yen.png"));
				}else {
					gridItem.setImage(2, null);
					gridItem.setText(2, Messages.NormalItem);
					
				}
				if(null != curBidPrice && bidPrice.getId().equals(curBidPrice.getId())){
					itemListGrid.setSelection(new GridItem[]{gridItem});
				}
			}
			if(itemListGrid.getSelectionCount() == 0){
				if(selectionIndex < itemListGrid.getItemCount()){
					itemListGrid.setSelection(selectionIndex);
				}
			}
			if(itemListGrid.getSelectionCount() == 0){
				itemListGrid.setSelection(0);
			}
			
		}
		selectionIndex = itemListGrid.getSelectionIndex();
		if (selectionIndex > 0) {
			Rectangle rectangle = itemListGrid.getBounds();
			itemListGrid.scroll(0, 0, 0, 0, rectangle.width, rectangle.height, true);
			itemListGrid.getVerticalBar().setSelection(selectionIndex);
			itemListGrid.getVerticalBar().notifyListeners(SWT.Selection, null);
		}
		itemListGrid.resizeColumnWidth();
		itemListGridSelectedAction();
		itemListGrid.getParent().layout();
	}

	private void itemListGridSelectedAction(){
		
	}
	

	/**
	 * @Description: 确定
	 * @author guoyr
	 */
	private void saveNodeAction(){
		if(save_btn.getEnabled()){
			if(null == timeText.getText().trim() || "".equals(timeText.getText().trim())){
				MessageDialog.openWarning(getShell(), "请设置合理的报价截止时间！");
				return;
			}
			if(null == roundName.getText().trim() || "".equals(roundName.getText().trim())){
				MessageDialog.openWarning(getShell(), "请输入报价名称！");
				return;
			}
			if(supplierGrid.isDisposed()) {
				return;
			}
			List<Supplier> supplierList = GridUtils.getCheckedValues(supplierGrid, 0, "supplier");

 			if(supplierList.size() <= 0) {
				MessageDialog.openWarning(getShell(), "请选择要报价的供应商");
				return;	
			}
			final BidPriceRound bidPriceRound = new BidPriceRound();
			bidPriceRound.setRoundMemo(ObjectUtils.getObjString(stepMemoText.getText().trim()));
			bidPriceRound.setRoundName(roundName.getText().trim());
			bidPriceRound.setRoundNo(newRound);
			bidPriceRound.setTenderId(tenderProjectEntity.getId());
			bidPriceRound.setRoundStatus(com.gx.obe.components.core.enumeration.EvaluationStepEnum.INCROSS);
			bidPriceRound.setRoundEndTime(DateUtils.toDate(timeText.getText().trim()));
			
			if(null != supplierList && supplierList.size() > 0){
				final Shell thisShell = getShell();
				final ProgressMessageDialog messageProgress = new ProgressMessageDialog(thisShell, true, -1, new LoadingAdatper() {
					public void finish(boolean bool) {
						if(bool) {
							Display.getDefault().syncExec(new Runnable() {
								public void run() {
									if(!thisShell.isDisposed()) {
										thisShell.close();
									}
								}
							});
						}
						if(null != addCallBack){
							addCallBack.callBack(bool,newRound);
						}
					}
				});
				ApplyReBidPriceThread applySecondBidPriceThread = new ApplyReBidPriceThread(tenderProjectEntity,supplierList,this.supplierList,bidPriceRound,tenderProjectRuleEntity,messageProgress);
				applySecondBidPriceThread.start();
				messageProgress.setTitle(bidPriceRound.getRoundName());
				messageProgress.open();
			}else{
				MessageDialog.openWarning(getShell(), "未选择待报价的供应商！");
			}
			
		}
	}
	
	
	private void selectSupplierAction(boolean isSelect) {
		if (isSelect) {
			GridUtils.checkAll(supplierGrid, 0, true);
		} else {
			GridUtils.checkAllBack(supplierGrid, 0);
		}
 	}
	
	
	/**
	 * @Description: 取消
	 * @author guoyr
	 */
	private void cancelAction(){
		this.getShell().close();
	}

	public CallBack getCallBack() {
		return addCallBack;
	}

	public void setCallBack(CallBack callBack) {
		this.addCallBack = callBack;
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(650, 400);
	}
}
