package com.gx.obe.management.composite;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.widgets.ELink;
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.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.listener.SelectionTenderPackListener;
import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.message.Messages;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 子包件面板
 * @author guoyr
 */
public class MyTenderPackageItemComposite extends Composite {

	private Label indexLabel;
	private Label tenderNoLabel;
	private ELink tenderNameLabel;
	private Label supplierCount_label;
	private Label supplierCountValue;
	public static final String  FORMAT = "yyyy-MM-dd HH:mm";
	private TenderProjectEntity tenderProjectEntity;
	private SelectionTenderPackListener listener;
	private ELink goto_btn;
	private Label statusLabel;

	/**
	 * @param parent
	 * @param style
	 */
	public MyTenderPackageItemComposite(Composite parent, int style) {
		super(parent, style);
		setBackgroundMode(SWT.INHERIT_FORCE);
//		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginHeight = 2;
		gridLayout.marginWidth = 2;
		gridLayout.horizontalSpacing = 0;
		gridLayout.verticalSpacing = 0;
		setLayout(gridLayout);
		
		Composite composite = new Composite(this, SWT.NONE);
//		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite.setLayout(new GridLayout(6, false));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Composite composite_2 = new Composite(composite, SWT.NONE);
		composite_2.setBackgroundMode(SWT.INHERIT_FORCE);
		composite_2.setBackground(SWTResourceManager.getColor(255, 153, 51));
		composite_2.setLayout(new GridLayout(1, false));
		
		indexLabel = new Label(composite_2, SWT.NONE);
		indexLabel.setAlignment(SWT.CENTER);
		indexLabel.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.BOLD));
		indexLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		indexLabel.setText("01");
		
		tenderNoLabel = new Label(composite, SWT.NONE);
		tenderNoLabel.setFont(SWTResourceManager.getFont("微软雅黑", 11, SWT.NORMAL));
		tenderNoLabel.setForeground(SWTResourceManager.getColor(100, 100, 100));
		tenderNoLabel.setText("[ZFCG2014Z0021]");
		
		tenderNameLabel = new ELink(composite, SWT.NONE);
		tenderNameLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		tenderNameLabel.setFont(SWTResourceManager.getFont("微软雅黑", 11, SWT.NORMAL));
		tenderNameLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		tenderNameLabel.setText("2014年国家航空航天遥感影像获取项目(三期)");

		Composite subPackCountComposite = new Composite(composite, SWT.NONE);
		subPackCountComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		GridLayout gl_subPackCountComposite = new GridLayout(2, false);
		gl_subPackCountComposite.horizontalSpacing = 0;
		gl_subPackCountComposite.marginWidth = 0;
		gl_subPackCountComposite.marginHeight = 0;
		subPackCountComposite.setLayout(gl_subPackCountComposite);
		
		supplierCount_label = new Label(subPackCountComposite, SWT.NONE);
		supplierCount_label.setText("投标人数：");
		
		supplierCountValue = new Label(subPackCountComposite, SWT.NONE);
		supplierCountValue.setForeground(SWTResourceManager.getColor(72, 152, 21));
		supplierCountValue.setText("2个");

		statusLabel = new Label(composite, SWT.NONE);
		statusLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		statusLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		statusLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		statusLabel.setText("");
		
		Composite op_composite = new Composite(composite, SWT.NONE);
		op_composite.setLayout(new GridLayout(1, false));
		op_composite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		
		goto_btn = new ELink(op_composite, SWT.NONE);
		goto_btn.setActiveColor(SWTResourceManager.getColor(255, 153, 0));
		goto_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/evaluation.png"));
		goto_btn.setText(Messages.Enter);
		
//		Label label = new Label(this, SWT.NONE);
//		label.setBackground(SWTResourceManager.getColor(240, 240, 240));
//		GridData gd_label = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
//		gd_label.heightHint = 1;
//		label.setLayoutData(gd_label);
		
		init();
	}

	/**
	 * @Description: 界面数据初始化
	 * @author guoyr
	 */
	public void init(){
		addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent arg0) {
				Rectangle rectangle = getBounds();
//				Point p = Display. getDefault ().map(MyTenderPackageItemComposite.this, null , 0,0);
//				Point mousePoint = SwtUtils.getMouseLoaction();
//				if(new Rectangle(p.x + 5, p.y + 5, getBounds().width -10, getBounds().height -10).contains(mousePoint)){
//					arg0.gc.setForeground(SWTResourceManager.getColor(200, 200, 200));
//				}else {
//					arg0.gc.setForeground(SWTResourceManager.getColor(200, 200, 200));
//				}
				arg0.gc.setForeground(SWTResourceManager.getColor(200, 200, 200));
				arg0.gc.setLineWidth(3);
				arg0.gc.setLineStyle(SWT.LINE_DOT);
				arg0.gc.drawRectangle(0, 0, rectangle.width -1, rectangle.height -1);
			}
		});
		
		EnterListener enterListener = new EnterListener();
		tenderNameLabel.addMouseListener(enterListener);
		goto_btn.addMouseListener(enterListener);
	}
	
	public void addSelectionTenderPackListener(SelectionTenderPackListener selectionTenderPackListener){
		this.listener = selectionTenderPackListener;
	}
	
	class EnterListener extends MouseAdapter{
		@Override
		public void mouseUp(MouseEvent e) {
			listener.selectPack(tenderProjectEntity);
			super.mouseUp(e);
		}
	}

	/**
	 * @Description: 界面刷新 
	 * @author guoyr
	 * @param index
	 * @param parentProject
	 * @param packageMap
	 */
	public void refresh(int index, TenderProjectEntity parentProject, TenderProjectEntity packMap){
		tenderProjectEntity = packMap;
//		tenderProjectEntity.setId((String)packMap.get("TENDER_ID"));
//		tenderProjectEntity.setTenderNo((String)packMap.get("TENDER_NO"));
//		tenderProjectEntity.setTenderName((String)packMap.get("TENDER_NAME"));
//		tenderProjectEntity.setTenderType((String)packMap.get("TENDER_TYPE"));
//		tenderProjectEntity.setPhaseType((String)packMap.get("PHASE_TYPE"));
//		tenderProjectEntity.setIsPack((String)packMap.get("IS_PACK"));
//		tenderProjectEntity.setTenderMethod((String)packMap.get("TENDER_METHOD"));
//		tenderProjectEntity.setEvaluationStatus((String)packMap.get("EVALUATION_STATUS"));
		tenderProjectEntity.setParentId(parentProject.getId());
		tenderProjectEntity.setParentProjectEntity(parentProject);
		if(index < 10){
			indexLabel.setText("0"+index);
		}else {
			indexLabel.setText(""+index);
		}
		
//		if(null != tenderProjectEntity.getEvaluationStatus() && tenderProjectEntity.getEvaluationStatus().length() >0){
//			statusLabel.setText("["+tenderProjectEntity.getEvaluationStatus()+"]");
//		}else {
//			statusLabel.setText("[未开始评标]");
//			
//		}
		supplierCount_label.setText(com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProjectEntity.getTenderType()) + "数：");
		int supplierCount  = packMap.getSupplierCountIntValue();
		if(supplierCount > 0){
			supplierCountValue.setText(supplierCount+Messages.SupplierCouns);
		}else {
			supplierCountValue.setText("无");
		}
		tenderProjectEntity.setSupplierCount(supplierCount);
		tenderNoLabel.setText("["+tenderProjectEntity.getTenderNo() + "]");
		tenderNameLabel.setText(tenderProjectEntity.getTenderName());
		statusLabel.getParent().layout();
	}
	
	@Override
	protected void checkSubclass() {
	}
}
