package com.gx.obe.management.composite;

import java.io.File;
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.listener.RoundBorderLineListener;
import org.eclipse.swt.core.utils.FileDialogUtils;
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.ELink;
import org.eclipse.swt.core.widgets.EMenu;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.core.widgets.ImageButton;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.StackLayout;
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.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
import com.gx.obe.action.bean.Action;
import com.gx.obe.business.http.UploadListener;
import com.gx.obe.common.dialog.GxqklDialog;
import com.gx.obe.common.file.enumeration.FileConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.button.ImageCheckBox;
import com.gx.obe.common.widget.composite.TitleComposite;
import com.gx.obe.common.widget.dialog.ProgressDialog;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.components.core.enumeration.TenderProjectEnum;
import com.gx.obe.management.thread.UploadEvaluationResultThread;
import com.gx.obe.management.webservice.QueryTxSourceThread;
import com.gx.obe.management.webservice.UploadEvaluationResultAndFile;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.FileUtils;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.RuntimeUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.util.utils.UuidUtils;
import com.gx.obe.web.entity.Chain;
import com.gx.obe.web.entity.auth.Menu;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.ChainService;
import com.gx.obe.web.service.TenderProjectService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

public class UploadEvaluationResultBlockchainComposite extends Composite {
	protected Object result;
	private Text messageText;
	private TenderProjectEntity tenderProjectEntity;
//	private Composite templateFileComposite;
	private String filePath;
//	private String buttonStatus = "";
	private ELink selectAllAutoBtn;
	private ELink disSelectAllAutoBtn;
	private ELink delAllBtn;
	private ELink delBtn;
	private ELink addNewBtn;
	private EButton upload_result_btn;
	private ProgressBar progressBar;
	private ELink saveAsEvalResultGLink;//另存为
	private String reportFilePath;//报表文件
//	private String reportDocPath;//报告文件
	private File zipFile;//压缩文件
	private UploadListener callBack;
	private Grid myReportGrid;
	private Grid attachmentGrid;
	private CLabel topRightLabel;
//	private Menu menu;
	private ImageButton autoReportBtn;
	protected String AUTO_REPORT = "AUTO_REPORT";
	protected String HAND_REPORT = "HAND_REPORT";
	private ImageButton curReportBtn;
	private ImageButton handReportBtn;
	private Composite load_composite;
	private StackLayout topStackLayout = new StackLayout();
	private Composite autoComposite;
	private Composite handComposite;
	private Composite toolComposite1;
	private Composite messageComposite;
	private Composite blockchainComposite;
	private Button button;
	private ImageCheckBox uploadBlockchainBtn;
	private Composite blockchainComplateComposite;
	private Composite bottomComposite;
	private ELink uploadBlockchainSuccessLink;
	private TenderProjectService tenderProjectService = new TenderProjectService();
	private Menu menu;
	private Action projectAction;
	private Chain chain;
	private ProgressDialog synchShellInfo;
	private QueryTxSourceThread queryTxSourceThread;
	private ChainService chainService;
	{
		  LogUtils.logClass(UploadEvaluationResultBlockchainComposite.class); 
		}
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public UploadEvaluationResultBlockchainComposite(Composite parent, Action projectAction,int style) {
		super(parent, style);
		setBackground(SWTResourceManager.getColor(255, 255, 255));
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
			}
		});
		this.projectAction = projectAction;
		this.tenderProjectEntity = (TenderProjectEntity)parent.getData("tenderProjectEntity");
		
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		
		Composite container = new Composite(this, SWT.NONE);
		container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_container = new GridLayout(1, false);
		gl_container.marginHeight = 0;
		gl_container.marginWidth = 0;
		container.setLayout(gl_container);
		
		toolComposite1 = new Composite(container, SWT.NONE);
		toolComposite1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		GridLayout gl_composite_22 = new GridLayout(4, false);
		gl_composite_22.marginWidth = 0;
		gl_composite_22.marginHeight = 0;
		gl_composite_22.verticalSpacing = 0;
		toolComposite1.setLayout(gl_composite_22);

		autoReportBtn = new ImageButton(toolComposite1, SWT.NONE);
		autoReportBtn.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));
		autoReportBtn.setLeftMargin(20);
		autoReportBtn.setRightMargin(20);
		autoReportBtn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				loadReportAction(AUTO_REPORT);
			}
		});
		autoReportBtn.setText(Messages.MyReport);
		curReportBtn = autoReportBtn;
		setanswerBtnCss(autoReportBtn);

		handReportBtn = new ImageButton(toolComposite1, SWT.NONE);
		handReportBtn.setLeftMargin(20);
		handReportBtn.setRightMargin(20);
		handReportBtn.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, true, false, 1, 1));
		handReportBtn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				loadReportAction(HAND_REPORT);
			}

		});
		handReportBtn.setText(Messages.Button_addAttachment);
		setanswerBtnCss(handReportBtn);
		topRightLabel = new CLabel(toolComposite1, SWT.NONE);
		topRightLabel.setLeftMargin(10);
		ESepator sep_1 = new ESepator(toolComposite1, SWT.NONE);
		sep_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		sep_1.setBackground(ColorConstants.SKIN_BG);
		sep_1.setLineWidth(2);

		load_composite = new Composite(container, SWT.NONE);
		topStackLayout.marginHeight = 1;
		topStackLayout.marginWidth = 1;
		load_composite.setLayout(topStackLayout);
		load_composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		load_composite.addPaintListener(new RoundBorderLineListener(ColorConstants.BORDER_LINE_COLOR));
		
		autoComposite = new Composite(load_composite, SWT.NONE);
		GridLayout gl_composite_2 = new GridLayout(1, false);
		gl_composite_2.verticalSpacing = 0;
		gl_composite_2.marginHeight = 0;
		gl_composite_2.marginWidth = 0;
		autoComposite.setLayout(gl_composite_2);
		
		TitleComposite composite_6 = new TitleComposite(autoComposite, SWT.NONE);
		composite_6.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		composite_6.setTitle("只有选中的评标报告、报表才会上传！");
		
		selectAllAutoBtn = composite_6.addELink();
		selectAllAutoBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/select_all.png"));
		selectAllAutoBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				selectAllMyReportAction(true);
			}
		});
		selectAllAutoBtn.setText(Messages.Button_selectAll);
		
		disSelectAllAutoBtn = composite_6.addELink();
		disSelectAllAutoBtn.setText(Messages.Button_deselected);
		disSelectAllAutoBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/disselect_all.png"));
		disSelectAllAutoBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				selectAllMyReportAction(false);
			}
		});
		
		ESepator sep1 = new ESepator(autoComposite, SWT.SEPARATOR| SWT.HORIZONTAL);
		sep1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		sep1.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		myReportGrid = new Grid(autoComposite, SWT.V_SCROLL);
		myReportGrid.setTreeLinesVisible(false);
		myReportGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		myReportGrid.setHeaderVisible(true);
		
		GridColumn gridColumn_1 = new GridColumn(myReportGrid, SWT.CENTER);
		gridColumn_1.setText(Messages.Num);
		gridColumn_1.setWidth(50);
		gridColumn_1.setMaxmumWidth(100);
		
		GridColumn gridColumn = new GridColumn(myReportGrid, SWT.CHECK);
		gridColumn.setText(Messages.ReportName);
		gridColumn.setWidth(500);
		
		
		handComposite = new Composite(load_composite, SWT.NONE);
		GridLayout gl_composite_12 = new GridLayout(1, false);
		gl_composite_12.verticalSpacing = 0;
		gl_composite_12.marginHeight = 0;
		gl_composite_12.marginWidth = 0;
		handComposite.setLayout(gl_composite_12);
		
		TitleComposite composite_5 = new TitleComposite(handComposite, SWT.NONE);
		composite_5.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite_5.setTitle("可以手动添加评标过程中所用到的任何附件！");
		
		delAllBtn = composite_5.addELink();
		delAllBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/delete.png"));
		delAllBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				delAllAttachmentAction();
			}
		});
		delAllBtn.setText(Messages.Button_deleteAll);
		
		delBtn = composite_5.addELink();
		delBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/delete.png"));
		delBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				delAttachmentAction();
			}
		});
		delBtn.setText(Messages.Button_delete);
		
		addNewBtn = composite_5.addELink();
		addNewBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/add.png"));
		addNewBtn.setText(Messages.Button_addAttachment);
		addNewBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				addAttachmentAction();
			}
		});
		
		ESepator sep4 = new ESepator(handComposite, SWT.SEPARATOR | SWT.HORIZONTAL);
		sep4.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		sep4.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		attachmentGrid = new Grid(handComposite, SWT.V_SCROLL);
		attachmentGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		attachmentGrid.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				refreshAttachemntTableBtn();
			}
		});
		attachmentGrid.setHeaderVisible(true);
		
		GridColumn gridColumn_3 = new GridColumn(attachmentGrid, SWT.CENTER);
		gridColumn_3.setText(Messages.Num);
		gridColumn_3.setWidth(50);
		gridColumn_3.setMaxmumWidth(100);
		
		GridColumn gridColumn_2 = new GridColumn(attachmentGrid, SWT.CHECK);
		gridColumn_2.setText(Messages.AttachmentName);
		gridColumn_2.setWidth(500);
		
		messageComposite = new Composite(container, SWT.NONE);
		GridLayout gl_composite_1 = new GridLayout(1, false);
		gl_composite_1.marginWidth = 1;
		gl_composite_1.marginHeight = 1;
		messageComposite.setLayout(gl_composite_1);
		GridData gd_composite_1 = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
		gd_composite_1.heightHint = 100;
		messageComposite.setLayoutData(gd_composite_1);
		messageComposite.addPaintListener(new RoundBorderLineListener(ColorConstants.BORDER_LINE_COLOR));
		
		messageText = new Text(messageComposite, SWT.READ_ONLY | SWT.V_SCROLL);
		messageText.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		messageText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		messageText.setText("");
		
		progressBar = new ProgressBar(messageComposite, SWT.SMOOTH | SWT.INDETERMINATE);
		GridData gd_progressBar = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_progressBar.heightHint = 2;
		progressBar.setLayoutData(gd_progressBar);
		
		bottomComposite = new Composite(container, SWT.NONE);
		bottomComposite.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false, 1, 1));
		GridLayout gl_composite = new GridLayout(4, false);
		bottomComposite.setLayout(gl_composite);
		
		blockchainComplateComposite = new Composite(bottomComposite, SWT.NONE);
		blockchainComplateComposite.setLayout(new GridLayout(2, false));
		blockchainComplateComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		
		CLabel uploadBlockchainSuccessLabel = new CLabel(blockchainComplateComposite, SWT.CHECK);
		uploadBlockchainSuccessLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		uploadBlockchainSuccessLabel.setText("已上传至中招公信链：");
		uploadBlockchainSuccessLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		uploadBlockchainSuccessLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/lan.png"));
		
		uploadBlockchainSuccessLink = new ELink(blockchainComplateComposite, SWT.NONE);
		uploadBlockchainSuccessLink.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		uploadBlockchainSuccessLink.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		uploadBlockchainSuccessLink.setText(UuidUtils.getUUID());
		uploadBlockchainSuccessLink.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				openInfoAction();
			}
		});
		
		blockchainComposite = new Composite(bottomComposite, SWT.NONE);
		blockchainComposite.setLayout(new GridLayout(2, false));
		blockchainComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		
		uploadBlockchainBtn = new ImageCheckBox(blockchainComposite, SWT.CHECK);
		uploadBlockchainBtn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		uploadBlockchainBtn.setText("同步上传中招公信链");
		uploadBlockchainBtn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		uploadBlockchainBtn.setImageSize(20);
		
		ELink blockchainHelpLink = new ELink(blockchainComposite, SWT.NONE);
		blockchainHelpLink.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		blockchainHelpLink.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		blockchainHelpLink.setText("");
		blockchainHelpLink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/warning.png"));
		blockchainHelpLink.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				MessageDialog.openInformation(getShell(), "中招公信链", "使用区块链存证，证信息真实可信！");
			}
		});
		
		
		upload_result_btn = new EButton(bottomComposite, SWT.NONE);
		upload_result_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/white/upload.png"));
		upload_result_btn.addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseUp(MouseEvent e) {
				uploadAction();
			}
		});
		upload_result_btn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		upload_result_btn.setBackground(ColorConstants.SKIN_BG);
		upload_result_btn.setForeground(ColorConstants.SKIN_FOREGROUND);
		upload_result_btn.setActiveBorderColor(ColorConstants.SKIN_ACTIVE_BG);
		upload_result_btn.setActiveBgColor(ColorConstants.SKIN_ACTIVE_BG);
		upload_result_btn.setActiveForeground(ColorConstants.SKIN_ACTIVE_FOREGROUND);
		upload_result_btn.setArcWidth(6);
		GridData gd_confirmBtn = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_confirmBtn.widthHint = 200;
		gd_confirmBtn.heightHint = 35;
		upload_result_btn.setLayoutData(gd_confirmBtn);
		upload_result_btn.setText(Messages.Button_uploadEvaluationResult);
		
		saveAsEvalResultGLink = new ELink(bottomComposite, SWT.NONE);
		saveAsEvalResultGLink.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		saveAsEvalResultGLink.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		saveAsEvalResultGLink.setText("");
		saveAsEvalResultGLink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/arrow_down.png"));
		saveAsEvalResultGLink.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				moreOpAction();
			}
		});
		
		
		init();
	}
	
	private void openInfoAction() {
		if(uploadBlockchainSuccessLink.isEnabled() && null != chain){
			synchShellInfo = new ProgressDialog(getShell(), "正在查询区块头信息...");
			queryTxSourceThread = new QueryTxSourceThread(tenderProjectEntity,chain,new CallBack() {
				public void callBack(final boolean bool, final Object obj) {
					Display.getDefault().syncExec(new Runnable() {
						public void run() {
							if(null != synchShellInfo){
								synchShellInfo.close();
							}
							if (bool) {
								GxqklDialog dialog = new GxqklDialog(getShell(), tenderProjectEntity, chain, obj);
								dialog.open();
							} else {
								MessageDialog.openWarning(getShell(), "区块头信息查询失败！");
							}
						}
					});
				}
			});
			queryTxSourceThread.start();
			synchShellInfo.open();
		}
	}
	/**
	 * @Description: 初始化
	 * @author wangx 
	 */
	private void init(){
		chainService = new ChainService();
		tenderProjectEntity = tenderProjectService.getById(tenderProjectEntity.getId());
		menu = projectAction.getMenu();
		reportFilePath = FileConstants.getReportFilePath(tenderProjectEntity.getId());
		filePath = FileConstants.TEMP_PATH +tenderProjectEntity.getId()+File.separator + "upload" + File.separator;
		myReportGrid.setItemHeight(25);
		attachmentGrid.setItemHeight(25);
		loadReportAction(AUTO_REPORT);
		refeshMyReportTable();
		refreshAttachmentTable();
		
		refreshUploadStatus();
		
		topRightLabel.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, false, 1, 1));
		progressBar.setVisible(false);
		if(null == tenderProjectEntity.getTransactionServerId() || "".equals(tenderProjectEntity.getTransactionServerId())){
			upload_result_btn.setEnabled(false);
		}
		//如果没有评标完成并且没有废标
		if(!ObjectUtils.equals(tenderProjectEntity.getComplateStatus(), CommonEnum.COMPLETE) && !ObjectUtils.equals(TenderProjectEnum.EFFECTIVE_NO, tenderProjectEntity.getEffectiveType())){
			upload_result_btn.setEnabled(false);
		}
		callBack = new UploadListener(){
			public void info(String info) {
				showMessage(info);
			}
			public void finish(final boolean uploadFlag) {
				Display.getDefault().syncExec(new Runnable() {
					public void run() {
						if(null != upload_result_btn && !upload_result_btn.isDisposed()){
							selectAllAutoBtn.setEnabled(true);
							disSelectAllAutoBtn.setEnabled(true);
							delAllBtn.setEnabled(true);
							addNewBtn.setEnabled(true);
							delBtn.setEnabled(true);
							upload_result_btn.setEnabled(true);
							saveAsEvalResultGLink.setEnabled(true);
							progressBar.setVisible(false);
							refreshUploadStatus();
							refreshAttachemntTableBtn();
							bottomComposite.setEnabled(true);
						}					
					}
				});
			}
		};
		WidgetUtils.setControlVisible(messageComposite, false);
		uploadBlockchainBtn.setSelection(true);
		
		// TODO
		upload_result_btn.setEnabled(true);
		
	}
	
	private void refreshUploadStatus(){
		if(CommonEnum.COMPLETE.equals(tenderProjectEntity.getUploadEvaluationResultStatus())){
			topRightLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
			topRightLabel.setText("已上传评标结果！");
			upload_result_btn.setText("重新上传评标结果");
		}else {
			WidgetUtils.setControlVisible(blockchainComplateComposite, false);
			upload_result_btn.setText(Messages.Button_uploadEvaluationResult);
			if(CommonEnum.FAILE.equals(tenderProjectEntity.getUploadEvaluationResultStatus())){
				topRightLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
				topRightLabel.setText("评标结果上传失败！");
			}else {
				topRightLabel.setText("");
			}
		}
		
		chain = chainService.getChainByTenderIdAndType(tenderProjectEntity.getId(),"evaluationResultRegister");
		
		if(null != tenderProjectEntity.getEvaluationResultBlockchainAddress() && tenderProjectEntity.getEvaluationResultBlockchainAddress().length() > 0){
			WidgetUtils.setControlVisible(blockchainComplateComposite, true);
			WidgetUtils.setControlVisible(blockchainComposite, false);
			uploadBlockchainSuccessLink.setText(tenderProjectEntity.getEvaluationResultBlockchainAddress());
		}else {
			WidgetUtils.setControlVisible(blockchainComplateComposite, false);
			WidgetUtils.setControlVisible(blockchainComposite, true);
		}
		bottomComposite.layout();
		bottomComposite.getParent().layout();
	}
	/**
	 * @Description: 加载上传报表
	 * @author wangxiang
	 * @param type
	 */
	private void loadReportAction(String type) {
		load_composite.setData("tenderProjectEntity", tenderProjectEntity);
		if (null != curReportBtn && !curReportBtn.isDisposed()) {
			curReportBtn.setSelection(false);
		}
		if (type.equals(AUTO_REPORT)) {
			topStackLayout.topControl = autoComposite;
			load_composite.layout();
			curReportBtn = autoReportBtn;
		} else if (type.equals(HAND_REPORT)) {
			topStackLayout.topControl = handComposite;
			load_composite.layout();
			curReportBtn = handReportBtn;
		}
		curReportBtn.setSelection(true);
		load_composite.layout();
	}
	
	/**
	 * @Description: 设置指标分类的按钮
	 * @author wangxiang
	 * @param reportBtn
	 */
	private void setanswerBtnCss(ImageButton reportBtn) {
		reportBtn.setRightMargin(10);
		reportBtn.setLeftMargin(10);
		reportBtn.setTopMargin(5);
		reportBtn.setBottomMargin(5);
		reportBtn.setRightMargin(20);
		reportBtn.setLeftMargin(20);
		reportBtn.setTopMargin(5);
		reportBtn.setBottomMargin(5);
		reportBtn.setRightMargin(10);
		reportBtn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));

		reportBtn.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		reportBtn.setBackgroundColor(SWTResourceManager.getColor(245, 245, 245));

		reportBtn.setActiveForeground(ColorConstants.SKIN_ACTIVE_FOREGROUND);
		reportBtn.setActiveBackgroundColor(ColorConstants.SKIN_ACTIVE_BG);

		reportBtn.setSelectionForeground(ColorConstants.SKIN_SELECTOIN_FOREGROUND);
		reportBtn.setSelectionBgColor(ColorConstants.SKIN_BG);

	}
	
	/**
	 * @Description: 我的报表
	 * @author guoyr
	 * @return 
	 */
	private List<File> getMyReportFileList(){
		List<File> reportFileList = new ArrayList<File>();
		//评审报表文件
		if(StringUtils.isNotEmpty(reportFilePath)){
			File[] myFiles = new File(reportFilePath).listFiles();
			if(null != myFiles){
				for(File file : myFiles){
					if(file.isFile()){
						reportFileList.add(file);
					}
				}
			}
		}
		return reportFileList;
	}
	
	/**
	 * @Description: 刷新我的报表
	 * @author guoyr
	 */
	private void refeshMyReportTable() {
		List<File> autoFileList = getMyReportFileList();
		if(null != autoFileList && autoFileList.size() > 0){
			int fileCount = 1;
			for(File file : autoFileList){
				if(file.getName().contains("~") && file.getName().contains("$")){
					continue;
				}
				GridItem gridItem = new GridItem(myReportGrid, SWT.NONE);
				gridItem.setText(0, fileCount+"");
				gridItem.setText(1, file.getName());
				gridItem.setChecked(1, true);
				gridItem.setData("file", file);
				fileCount ++;
			}
		}
	}

	/**
	 * @Description: 我的附件
	 * @author guoyr
	 */
	private void refreshAttachmentTable() {
		attachmentGrid.removeAll();
		List<File> fileList = FileUtils.listFilesByIsFile(filePath);
		if(null != fileList && fileList.size() > 0){
			int fileCount = 1;
			for (File uploadFile : fileList) {
				if(uploadFile.getName().contains("~") && uploadFile.getName().contains("$")){
					continue;
				}
				if (uploadFile.isFile()) {
					GridItem gridItem = new GridItem(attachmentGrid, SWT.NONE);
					gridItem.setText(0, fileCount+"");
					gridItem.setText(1, uploadFile.getName());
					gridItem.setChecked(1, true);
					gridItem.setCheckVisible(1, false);
					gridItem.setData("file", uploadFile);
					fileCount ++;
				}
			}
		}
		refreshAttachemntTableBtn();
	}
	
	/**
	 * @Description: 设置模板选中
	 * @author guoyr
	 * @param slectAll
	 */
	private void selectAllMyReportAction(boolean slectAll){
		if(selectAllAutoBtn.getEnabled()){
			GridUtils.checkAll(myReportGrid, 1, slectAll);
		}
	}
	
	/**
	 * @Description: 删除所有的上传附件
	 * @author guoyr
	 */
	private void delAllAttachmentAction(){
		if(delAllBtn.getEnabled()){
			FileUtils.delAllFile(filePath);
			attachmentGrid.removeAll();
			delAllBtn.setEnabled(false);
			delBtn.setEnabled(false);
		}
	}
	
	/**
	 * @Description: 选中上传附件事件
	 * @author guoyr
	 */
	private void refreshAttachemntTableBtn(){
		if(attachmentGrid.getItemCount() > 0){
			delAllBtn.setEnabled(true);
			if(attachmentGrid.getSelectionCount() > 0){
				delBtn.setEnabled(true);
			}else {
				delBtn.setEnabled(false);
			}
		}else {
			delAllBtn.setEnabled(false);
			delBtn.setEnabled(false);
		}
		
	}

	/**
	 * @Description: 删除所有的上传附件
	 * @author guoyr
	 */
	private void delAttachmentAction(){
		if(delBtn.getEnabled()){
			int selectIndex = 0;
			if(attachmentGrid.getSelectionCount() > 0){
				GridItem item = attachmentGrid.getSelection()[0];
				selectIndex = attachmentGrid.getSelectionIndex();
				File file = (File)item.getData("file");
				if(null != file && file.exists()){
					file.delete();
				}
				item.dispose();
			}
			if(attachmentGrid.getItemCount() > 0){
				if(selectIndex<=attachmentGrid.getItemCount()){
					attachmentGrid.setSelection(selectIndex -1);
				}
			}
			int fileCount = 1;
			for(GridItem item : attachmentGrid.getItems()){
				item.setText(0, (fileCount++)+"");
			}
		}
		refreshAttachemntTableBtn();
	}
	
	/**
	 * @Description: 添加上传附件
	 * @author guoyr
	 */
	private void addAttachmentAction(){
		if(addNewBtn.getEnabled()){
			String selectFiles = FileDialogUtils.getFileNames(SWT.MULTI, Messages.Button_addAttachment, new String[]{"*.*"});
			FileUtils.copyOrCreateFiles(selectFiles, filePath);
			refreshAttachmentTable();
			refreshAttachemntTableBtn();
		}
	}
	
	/**
	 * @Description: 确认上传
	 * @author guoyr
	 */
	private void uploadAction(){
		if(!upload_result_btn.getEnabled())return ;
		if(null != tenderProjectEntity.getComplateStatus() && tenderProjectEntity.getComplateStatus().equals(CommonEnum.COMPLETE)){
			if(!MessageDialog.openConfirm(getShell(),  "确认要上传评标结果吗？")){
				return;
			}
		}else {
			if(ObjectUtils.equals(tenderProjectEntity.getEffectiveType(), TenderProjectEnum.EFFECTIVE_NO)){
				if(!MessageDialog.openConfirm(new Shell(),  "当前项目已废标，确认要上传评标结果吗？")){
					return;
				}
			}else {
//				MessageDialog.openWarning(getShell(), "当前项目未完成评标，请完成评审或废标后再上传.");
				if(!MessageDialog.openConfirm(getShell(),  "当前项目未完成评标，确认要上传评标结果吗？")){
					return;
				}
			}
		}
			selectAllAutoBtn.setEnabled(false);
			disSelectAllAutoBtn.setEnabled(false);
			delAllBtn.setEnabled(false);
			addNewBtn.setEnabled(false);
			delBtn.setEnabled(false);
			upload_result_btn.setEnabled(false);
			saveAsEvalResultGLink.setEnabled(false);
			messageText.setText("");
			final List<File> reportFileList = GridUtils.getRealCheckedValues(myReportGrid, 1, "file");
			final List<File> uploadFileList = GridUtils.getRealCheckedValues(attachmentGrid, 1, "file");
			progressBar.setVisible(true);
			WidgetUtils.setControlVisible(messageComposite, true);
			messageComposite.getParent().layout();
			
			callBackMsg(MessageFormat.format(Messages.Tips_prepareTo, Messages.Button_uploadEvaluationResult));
			bottomComposite.setEnabled(false);
			new UploadEvaluationResultThread(menu,tenderProjectEntity, reportFileList, uploadFileList, callBack).start();
	}
	
	/**
	 * @Description: 导出评标结果
	 */
	private void saveAsUploadEvalResult(){
		if(!saveAsEvalResultGLink.getEnabled()){
			return ;
		}
		
		final List<File> templateFileList = GridUtils.getRealCheckedValues(myReportGrid, 1, "file");
		final List<File> uploadFileList = GridUtils.getRealCheckedValues(attachmentGrid, 1, "file");
		final String saveFilePath = FileDialogUtils.getFileSavePath("["+tenderProjectEntity.getTenderNo()+"]" + tenderProjectEntity.getTenderName()+"评标结果", Messages.Button_saveAs, new String[]{"*.zip"}, new String[]{"zip文件(*.zip)"});
		if(StringUtils.isNotEmpty(saveFilePath)){
			((GridData)messageComposite.getLayoutData()).exclude = false;
			messageComposite.setVisible(true);
			messageComposite.getParent().layout();
			progressBar.setVisible(true);
			showMessage(MessageFormat.format(Messages.Tips_loadingSth, Messages.BidEvaluationResult));
			class UploadFileThread implements Runnable{//保存时间长，采用进度条方式解决。
				public void run() {
					UploadEvaluationResultAndFile uploadEvaluateResult = new UploadEvaluationResultAndFile();
					zipFile = uploadEvaluateResult.getZipFile(tenderProjectEntity, templateFileList, uploadFileList, callBack);
					if(null != zipFile && zipFile.exists()){
						if(null != saveFilePath && !"".equals(saveFilePath)){
							if(FileUtils.copy(zipFile.getAbsolutePath(), saveFilePath)){
								showMessage("√" + MessageFormat.format(Messages.Tips_exportSuccessFormat, Messages.BidEvaluationResult));
								showMessage("√保存位置"+saveFilePath);
								Display.getDefault().syncExec(new Runnable() {
									public void run() {
										if(MessageDialog.openConfirm(getShell(), Messages.Button_openDirectory, Messages.Tips_exportAndOpenDirectory, new String[]{Messages.Button_open, Messages.Button_close})){
											progressBar.setVisible(false);
											RuntimeUtils.openDirectorAndSelect(saveFilePath);
										}
									}
								});
							}else{
								showMessage("×导出评标结果失败！");
							}
						}
					}
					
				}
			}
			new Thread(new UploadFileThread()).start();
		}
	}
	
	/**
	 * @Description: 显示消息
	 * @author guoyr
	 * @param msg
	 */
	private void showMessage(final String msg){
		Display.getDefault().syncExec(new Runnable(){
			public void run() {
				if(null != messageText && !messageText.isDisposed()){
					messageText.setText(messageText.getText() +"\r\n"+ msg);
					messageText.update();
					messageText.setSelection(messageText.getText().length());
				}
				
			}});
	}

	private void callBackMsg(String msg){
		if(null != callBack){
			callBack.info("  "+msg);
		}
	}
	
	/**
	 * @Description: 更多操作
	 * @author guoyr
	 */
	private void moreOpAction() {
		final EMenu gmenu = new EMenu(getShell());
		gmenu.addMenuItem(MessageFormat.format(Messages.Button_exportFormat, Messages.BidEvaluationResult), ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/export.png"), saveAsEvalResultGLink.getEnabled(), new EMenu.ClickListener() {
			public void click() {
				saveAsUploadEvalResult();
			}
		});
		gmenu.open();
		
	}
	
	@Override
	protected void checkSubclass() {
	}
}
