package com.gx.obe.management.composite;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.calendar.CalendarUtil;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
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.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.enumeration.TenderProjectEnum;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.TenderProjectService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/** 
 * @Description: 供应商评标结果
 * @author chenxw 
 */
public class SupplierEvaluationResultComposite extends Composite {

	private TenderProjectEntity tenderProjectEntity;
	private Composite composite;
	private Map<String, Text> textMap = new HashMap<String, Text>();
	private Combo combo;
	private TenderProjectService tenderProjectService ;
	private ScrolledComposite scrolledComposite;
	private Composite winnerDetailComposite;
	private Text childrenFirmText;
	private com.gx.obe.web.service.TenderProjectService tenderProjectServices;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public SupplierEvaluationResultComposite(Composite parent, int style) {
		super(parent, style);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		setBackgroundMode(SWT.INHERIT_FORCE);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		tenderProjectServices = new com.gx.obe.web.service.TenderProjectService();
		tenderProjectEntity = (TenderProjectEntity) parent.getData("tenderProjectEntity");

		Composite borderComposite = new Composite(this, SWT.NONE);
		borderComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_borderComposite = new GridLayout(1, false);
		gl_borderComposite.marginHeight = 1;
		gl_borderComposite.marginWidth = 1;
		borderComposite.setLayout(gl_borderComposite);
		borderComposite.setBackground(ColorConstants.BORDER_LINE_COLOR);
		
		scrolledComposite = new ScrolledComposite(borderComposite, SWT.NONE | SWT.H_SCROLL | SWT.V_SCROLL);
		scrolledComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		
		
		composite = new Composite(scrolledComposite, SWT.NONE);
		composite.setBackground(ColorConstants.BORDER_LINE_COLOR);
		GridLayout gl_composite = new GridLayout(2, false);
		gl_composite.verticalSpacing = 1;
		gl_composite.horizontalSpacing = 1;
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		scrolledComposite.setContent(composite);
		scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

		Composite composite_1 = new Composite(composite, SWT.NONE);
		composite_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		GridLayout gl_composite_1 = new GridLayout(2, false);
		composite_1.setLayout(gl_composite_1);

		Label label = new Label(composite_1, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setFont(SWTResourceManager.getFont("微软雅黑", 11, SWT.BOLD));
		label.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label.setText("中标候选人基本情况 第一中标候选人：");

		combo = new Combo(composite_1, SWT.READ_ONLY);
		combo.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, true, 1, 1));

		Composite childrenFirmComposite = new Composite(composite, SWT.NONE);
		childrenFirmComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		childrenFirmComposite.setLayout(new GridLayout(2, false));
		childrenFirmComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

		Label childrenFirmLabel = new Label(childrenFirmComposite, SWT.NONE);
		childrenFirmLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		childrenFirmLabel.setText("分子公司：");
		childrenFirmLabel.setFont(SWTResourceManager.getFont("微软雅黑", 11, SWT.BOLD));
		childrenFirmLabel.setForeground(ColorConstants.FONT_BLACK_COLOR);

		childrenFirmText = new Text(childrenFirmComposite, SWT.NONE);
		childrenFirmText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		childrenFirmText.setTextLimit(50);

		winnerDetailComposite = new Composite(composite, SWT.NONE);
		winnerDetailComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_winnerDetailComposite = new GridLayout(2, false);
		gl_winnerDetailComposite.verticalSpacing = 0;
		gl_winnerDetailComposite.horizontalSpacing = 0;
		gl_winnerDetailComposite.marginHeight = 0;
		gl_winnerDetailComposite.marginWidth = 0;
		winnerDetailComposite.setLayout(gl_winnerDetailComposite);
		winnerDetailComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

		Composite winnerDetailTitleComposite = new Composite(winnerDetailComposite, SWT.NONE);
		winnerDetailTitleComposite.setLayout(new GridLayout(1, false));
		winnerDetailTitleComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

		ESepator req_sep = new ESepator(winnerDetailComposite, SWT.NONE);
		req_sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));
		req_sep.setBackground(ColorConstants.BORDER_LINE_COLOR);
		req_sep.setLineStyle(SWT.LINE_DOT);
		req_sep.setLineWidth(2);

		Label label_1 = new Label(winnerDetailTitleComposite, SWT.NONE);
		label_1.setText("中标候选人简介");
		label_1.setFont(SWTResourceManager.getFont("微软雅黑", 11, SWT.BOLD));
		label_1.setForeground(ColorConstants.FONT_BLACK_COLOR);

		Composite composite_2 = new Composite(this, SWT.NONE);
		composite_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		GridLayout gl_composite_2 = new GridLayout(2, false);
		composite_2.setLayout(gl_composite_2);

		EButton submitBtn = new EButton(composite_2, SWT.NONE | EButton.CSS_GREEN);
		submitBtn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		GridData gd_signBtn = new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1);
		gd_signBtn.heightHint = 35;
		gd_signBtn.widthHint = 200;
		submitBtn.setLayoutData(gd_signBtn);
		submitBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				submitAction();
			}
		});
		submitBtn.setText("保存上传材料");
		init();
	}

	private void init() {
		tenderProjectService = new TenderProjectService();
		tenderProjectEntity = tenderProjectService.getById(tenderProjectEntity.getId());
		combo.add("----请选择----", 0);
		combo.add("最低价中标", 1);
		combo.add("次低价中标", 2);
		combo.add("最高价中标", 3);
		combo.add("次高价中标", 4);
		combo.add("中间价中标", 5);
		combo.select(0);

		textMap.put(TenderProjectEnum.EVALU_childrenFirm, childrenFirmText);

		createTextComposite("名称：", TenderProjectEnum.WINNER_NAME, 50, false, false, null);
		createTextComposite("企业性质：", TenderProjectEnum.WINNER_PROPERTY, 50, false, false, null);
		createDateTextComposite("成立时间：", TenderProjectEnum.WINNER_FOUND_TIME);
		createTextComposite("注册资本金（万元）：", TenderProjectEnum.WINNER_REGISTER_CAPITAL, 20, false, false, new TextValueListener() {
			public boolean isCorrect(String textValue) {
				BigDecimal capital = ObjectUtils.getObjBigDecimal(textValue);
				return null != capital;
			}
		});
		createTextComposite("主要业绩：", TenderProjectEnum.WINNER_ACHIEVEMENT, 2000, true, true, null);

		createTextComposite("评标委员会评审意见（推荐理由）", TenderProjectEnum.EVALU_recommendReason, 1);
		createTextComposite("否决投标情况", TenderProjectEnum.EVALU_vetoBidDetail, 1);
		createTextComposite("中标候选人复审情况", TenderProjectEnum.EVALU_winnerReviewDetail, 1);
		//		createTextComposite("中标候选人简介", TenderProjectEnum.EVALU_winnerDetail, 1);
		createTextComposite("价格调整说明", TenderProjectEnum.EVALU_bidPriceModifyMemo, 1);
		createTextComposite("投标人代理品牌", TenderProjectEnum.EVALU_supplierProxyContent, 1);
		createTextComposite("其他", TenderProjectEnum.EVALU_other, 1);
		
		String winBidType = tenderProjectEntity.getEvaluationParamByKey(TenderProjectEnum.EVALU_winBidType);
		if (TenderProjectEnum.TYPE_FIRST_LOW.equals(winBidType)) {
			combo.select(1);
		} else if (TenderProjectEnum.TYPE_SECOND_LOW.equals(winBidType)) {
			combo.select(2);
		} else if (TenderProjectEnum.TYPE_FIRST_HIGH.equals(winBidType)) {
			combo.select(3);
		} else if (TenderProjectEnum.TYPE_SECOND_HIGH.equals(winBidType)) {
			combo.select(4);
		} else if (TenderProjectEnum.TYPE_MIDDLE.equals(winBidType)) {
			combo.select(5);
		}
		for (String param : textMap.keySet()) {
			String textValue = tenderProjectEntity.getEvaluationParamByKey(param);
			if (StringUtils.isNotEmpty(textValue)) {
				textMap.get(param).setText(textValue);
			}
		}
		
		scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
	}

	private void submitAction() {
		String winBidType = null;
		switch (combo.getSelectionIndex()) {
		case 1:
			winBidType = TenderProjectEnum.TYPE_FIRST_LOW;
			break;
		case 2:
			winBidType = TenderProjectEnum.TYPE_SECOND_LOW;
			break;
		case 3:
			winBidType = TenderProjectEnum.TYPE_FIRST_HIGH;
			break;
		case 4:
			winBidType = TenderProjectEnum.TYPE_SECOND_HIGH;
			break;
		case 5:
			winBidType = TenderProjectEnum.TYPE_MIDDLE;
			break;
		default:
			winBidType = "";
			break;
		}
		tenderProjectEntity.setEvaluationParam(TenderProjectEnum.EVALU_winBidType, winBidType);
		for (String param : textMap.keySet()) {
			tenderProjectEntity.setEvaluationParam(param, textMap.get(param).getText().trim());
		}
		if (tenderProjectServices.updateAssignProperty(tenderProjectEntity, new String[] {"id","params" })) {
			MessageDialog.openShortMessage(Messages.Tips_saveSuccess);
		} else {
			MessageDialog.openError(getShell(), Messages.Dialog_warmTips, Messages.Tips_saveFail);
		}
	}

	public Composite createTextComposite(String titleText, String param, int horizontalSpan) {
		Composite textComposite = new Composite(composite, SWT.NONE);
		textComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridData gd_textComposite = new GridData(SWT.FILL, SWT.FILL, true, true, horizontalSpan, 1);
		gd_textComposite.widthHint = 500;
		gd_textComposite.minimumHeight = 100;
		gd_textComposite.heightHint = 100;
		textComposite.setLayoutData(gd_textComposite);
		GridLayout gl_textComposite = new GridLayout(1, false);
		gl_textComposite.verticalSpacing = 0;
		gl_textComposite.horizontalSpacing = 0;
		gl_textComposite.marginHeight = 0;
		gl_textComposite.marginWidth = 0;
		textComposite.setLayout(gl_textComposite);

		Composite composite_1 = new Composite(textComposite, SWT.NONE);
		composite_1.setLayout(new GridLayout(1, false));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		ESepator req_sep = new ESepator(textComposite, SWT.NONE);
		req_sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));
		req_sep.setBackground(ColorConstants.BORDER_LINE_COLOR);
		req_sep.setLineStyle(SWT.LINE_DOT);
		req_sep.setLineWidth(2);
		
		Label label = new Label(composite_1, SWT.NONE);
		label.setFont(SWTResourceManager.getFont("微软雅黑", 11, SWT.BOLD));
		label.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label.setText(titleText);

		Text text = new Text(textComposite, SWT.V_SCROLL | SWT.MULTI | SWT.WRAP);
		text.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		text.setTextLimit(500);

		textMap.put(param, text);
		return textComposite;
	}

	private Composite createTextComposite(String titleText, String param, int textLimit, boolean isMultLine, boolean isFill, final TextValueListener listener) {
		Composite composite = new Composite(winnerDetailComposite, SWT.NONE);
		GridLayout gl_composite = new GridLayout(3, false);
		gl_composite.marginLeft = 20;
		gl_composite.verticalSpacing = 0;
		composite.setLayout(gl_composite);
		GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true, false, isFill ? 2 : 1, 1);
		gd_composite.widthHint = 200;
		composite.setLayoutData(gd_composite);

		Label label_1 = new Label(composite, SWT.NONE);
		label_1.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		label_1.setAlignment(SWT.RIGHT);
//		gl_title.widthHint = 100;
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, true, 1, 1));
		label_1.setText(titleText);

		final Text text = new Text(composite, (isMultLine ? (SWT.MULTI | SWT.WRAP | SWT.V_SCROLL): SWT.NONE ));
		GridData gd_text = new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1);
		if(isMultLine){
			gd_text.heightHint = 36;
		}
		text.setLayoutData(gd_text);
		text.setTextLimit(textLimit);
		if (null != listener) {
			text.addFocusListener(new FocusAdapter() {
				public void focusLost(FocusEvent e) {
					if (!listener.isCorrect(text.getText().trim())) {
						text.setText("");
					}
				}
			});
		}
		text.addPaintListener(new PaintListener(){
			public void paintControl(PaintEvent e) {
				e.gc.setForeground(ColorConstants.BORDER_LINE_COLOR);
				e.gc.drawLine(0, text.getBounds().height -1, text.getBounds().width, text.getBounds().height - 1);
			}
			
		});
		textMap.put(param, text);
		return composite;
	}

	private Composite createDateTextComposite(String titleText, String param) {
		Composite composite = new Composite(winnerDetailComposite, SWT.NONE);
		GridLayout gl_composite = new GridLayout(3, false);
		gl_composite.marginLeft = 20;
		composite.setLayout(gl_composite);
		GridData gd_composite = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_composite.widthHint = 200;
		composite.setLayoutData(gd_composite);

		Label label_1 = new Label(composite, SWT.NONE);
		label_1.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		label_1.setAlignment(SWT.RIGHT);
//		gl_title.widthHint = 100;
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, true, 1, 1));
		label_1.setText(titleText);

		final Text text = new Text(composite, SWT.NONE);
		GridData gd_text = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1);
		gd_text.widthHint = 200;
		text.setLayoutData(gd_text);
		text.setEditable(false);
		text.addPaintListener(new PaintListener(){
			public void paintControl(PaintEvent e) {
				e.gc.setForeground(ColorConstants.BORDER_LINE_COLOR);
				e.gc.drawLine(0, text.getBounds().height -1, text.getBounds().width, text.getBounds().height - 1);
			}
			
		});
		CLabel timeLabel = new CLabel(composite, SWT.NONE);
		timeLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/data.png"));
		timeLabel.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				
				CalendarUtil.getDates(text, "yyyy-MM-dd 00:00:00");

//				CalendarDialog timeDialog = new CalendarDialog(getShell(), DateUtils.toDate(text.getText().trim(), "yyyy-MM-dd 00:00:00"), null, null);
//				timeDialog.setCallBack(new CallBack() {
//					public void callBackDate(Date date) {
//						if (null != date) {
//							text.setText(DateUtils.getFormat(date, DateUtils.DATE_FORMAT_STR));
//						}
//					}
//				});
//				timeDialog.open();
			}
		});
		
		textMap.put(param, text);
		return composite;
	}
	
	interface TextValueListener {
		boolean isCorrect(String textValue);
	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
