package com.gx.obe.management.composite;

import java.math.BigDecimal;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.custom.ScrolledComposite;
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.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import com.gx.obe.ColorConstants;
import com.gx.obe.boq.CreditParam;
import com.gx.obe.common.widget.composite.UnderlineText;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.components.core.enumeration.EvaluationFactorEnum;
import com.gx.obe.components.core.enumeration.EvaluationStepEnum;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.CollectionUtils;
import com.gx.obe.util.utils.UuidUtils;
import com.gx.obe.web.entity.evaluation.EvaluationFactor;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.TenderProjectService;
import com.gx.obe.web.service.evaluation.EvaluationFactorService;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 商务指标
 * @author chenxw
 */
public class CreditFactorManagerComposite extends Composite {
	
	private final TenderProjectService tenderProjectService = new TenderProjectService();
	private final EvaluationFactorService evaluationFactorService = new EvaluationFactorService();
	
	private ScrolledComposite scrolledComposite;
	private Composite factorComposite;
	private UnderlineText weightText;
	private Button isEffectiveButton;
	private UnderlineText baseScoreText;
	private UnderlineText bidderCountText;
	
	private TenderProjectEntity tenderProject;
	
	private EvaluationFactor creditFactor;
	
	private CreditParam creditParam;
	
	/** 
	 * @author chenxw
	 * @param parent
	 * @param style 
	 */
	public CreditFactorManagerComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		setBackgroundMode(SWT.INHERIT_FORCE);
		
		scrolledComposite = new ScrolledComposite(this, SWT.V_SCROLL);
		scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		
		factorComposite = new Composite(scrolledComposite, SWT.NONE);
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.marginHeight = 1;
		gl_composite.marginWidth = 1;
		gl_composite.horizontalSpacing = 1;
		gl_composite.verticalSpacing = 1;
		factorComposite.setLayout(gl_composite);
		factorComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		factorComposite.setBackground(ColorConstants.BORDER_LINE_COLOR);
		
		Composite composite_1 = new Composite(factorComposite, SWT.NONE);
		composite_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		composite_1.setLayout(new GridLayout(1, false));
		
		Label label_10 = new Label(composite_1, SWT.NONE);
		label_10.setText("得分计算设置");
		
		Composite composite_3 = new Composite(factorComposite, SWT.NONE);
		composite_3.setLayout(new GridLayout(1, false));
		composite_3.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridData gd_composite_3 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_composite_3.widthHint = 200;
		composite_3.setLayoutData(gd_composite_3);
		
		Composite composite_7 = new Composite(composite_3, SWT.NONE);
		composite_7.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite_7.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		Label label_7 = new Label(composite_7, SWT.NONE);
		label_7.setText("建筑工程施工总承包项目信用评价分值权重：");
		
		Label label_8 = new Label(composite_7, SWT.NONE);
		label_8.setText("1000万元以下项目为1%-3%；");
		
		Label label_9 = new Label(composite_7, SWT.NONE);
		label_9.setText("1000万元以上至5000万元以下项目为4%-6%；");
		
		Label label_11 = new Label(composite_7, SWT.NONE);
		label_11.setText("5000万元以上项目为7%-10%；");
		
		Composite composite_8 = new Composite(composite_3, SWT.NONE);
		composite_8.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite_8.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		Label label_6 = new Label(composite_8, SWT.NONE);
		label_6.setText("市政公用工程施工总承包、专业承包、园林绿化项目信用评价分值权重为1%-3%。");
		
		Composite composite_4 = new Composite(composite_3, SWT.NONE);
		composite_4.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite_4.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		Label label = new Label(composite_4, SWT.NONE);
		label.setText("信用评价分值占总得分的权重为");
		
		weightText = new UnderlineText(composite_4, SWT.CENTER);
		weightText.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		weightText.setLayoutData(new RowData(50, SWT.DEFAULT));
//		weightText.setTextLimit(2);
		weightText.setCheckBigDecimal(false);
		
		Label label_1 = new Label(composite_4, SWT.NONE);
		label_1.setText("%。");
		
		Composite composite = new Composite(composite_3, SWT.NONE);
		composite.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		isEffectiveButton = new Button(composite, SWT.CHECK);
		
		Label label_2 = new Label(composite, SWT.NONE);
		label_2.setText("企业信用评价分值不低于");
		
		baseScoreText = new UnderlineText(composite, SWT.CENTER);
		baseScoreText.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		baseScoreText.setLayoutData(new RowData(50, SWT.DEFAULT));
		baseScoreText.setCheckBigDecimal(false);
		
		Label label_3 = new Label(composite, SWT.NONE);
		label_3.setText("分（80~90分）的投标人参与评审。");
		
		Composite composite_5 = new Composite(composite_3, SWT.NONE);
		composite_5.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		composite_5.setLayout(new RowLayout(SWT.HORIZONTAL));
		
		Label label_5 = new Label(composite_5, SWT.NONE);
		label_5.setText("如果投标企业多于");
		
		bidderCountText = new UnderlineText(composite_5, SWT.CENTER);
		bidderCountText.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		bidderCountText.setCheckIntValue(false);
		
		Label label_4 = new Label(composite_5, SWT.NONE);
		label_4.setText("家（含）时，则低于该分值的投标人不再参与评审。否则，本项设置无效，所有有效投标人均参与评审。");
		
		scrolledComposite.setContent(factorComposite);
		scrolledComposite.setMinSize(factorComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		
		Composite composite_2 = new Composite(this, SWT.NONE);
		GridData gd_composite_2 = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_composite_2.widthHint = 400;
		composite_2.setLayoutData(gd_composite_2);
		composite_2.setLayout(new GridLayout(1, false));
		
		EButton save_btn = new EButton(composite_2, SWT.NONE);
		save_btn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				saveEvaluationFactorAction();
			}
		});
		GridData gd_save_btn = new GridData(SWT.CENTER, SWT.FILL, true, true, 1, 1);
		gd_save_btn.heightHint = 35;
		gd_save_btn.widthHint = 100;
		save_btn.setLayoutData(gd_save_btn);
		save_btn.setBackground(ColorConstants.SKIN_BG);
		save_btn.setForeground(ColorConstants.SKIN_FOREGROUND);
		save_btn.setActiveBorderColor(ColorConstants.SKIN_ACTIVE_BG);
		save_btn.setActiveBgColor(ColorConstants.SKIN_ACTIVE_BG);
		save_btn.setActiveForeground(ColorConstants.SKIN_ACTIVE_FOREGROUND);
		save_btn.setArcWidth(6);
		save_btn.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		GridData gd_savebutton_1 = new GridData(SWT.CENTER, SWT.FILL, true, true, 1, 1);
		gd_savebutton_1.heightHint = 35;
		gd_savebutton_1.widthHint = 200;
		save_btn.setLayoutData(gd_savebutton_1);
		save_btn.setText("保 存");
		
	}
	
	/**
	 * @Description: 刷新
	 * @author chenxw
	 * @param tenderProject
	 * @param evalMethod
	 */
	public void refresh(TenderProjectEntity tenderProject) {
		this.tenderProject = tenderProject;
		
		this.creditFactor = getCreditFactor();
		
		creditParam = CreditParam.setSerialize(creditFactor.getComputerParams());
		if (null == creditParam) {
			creditParam = new CreditParam();
		}
		refreshCreditFactor();
	}
	
	/**
	 * @Description: 获取信用指标
	 * @author chenxw
	 * @return
	 */
	private EvaluationFactor getCreditFactor() {
		EvaluationFactor creditFactor = null;
		List<EvaluationFactor> creditFactorList = evaluationFactorService.getAllEvaluationFactorList(tenderProject.getId(), EvaluationFactorEnum.AUDIT_CREDIT);
		if (CollectionUtils.isNotNull(creditFactorList)) {
			creditFactor = creditFactorList.get(0);
		}
		if (null == creditFactor) {
			creditFactor = new EvaluationFactor();
			creditFactor.setTenderId(tenderProject.getId());
			creditFactor.setFactorName("综合信用指标");
			creditFactor.setFactorCode("creditFactor");
			creditFactor.setFactorScore(BigDecimal.valueOf(100));
			creditFactor.setFactorMinScore(BigDecimal.ZERO);
			creditFactor.setFactorMaxScore(BigDecimal.valueOf(100));
			creditFactor.setAuditType(EvaluationFactorEnum.AUDIT_CREDIT);
			creditFactor.setEvalBidType(EvaluationFactorEnum.EVAL_BID_CREDIT);
		}
		return creditFactor;
	}
	
	/**
	 * @Description: 刷新信用指标
	 * @author chenxw
	 */
	private void refreshCreditFactor() {
		weightText.setBigDecimalValue(creditFactor.getFactorWeight());
		isEffectiveButton.setSelection(CommonEnum.YES.equals(creditParam.getIsSelBaseScore()));
		baseScoreText.setBigDecimalValue(creditParam.getBaseScore());
		bidderCountText.setIntValue(creditParam.getBidderCount());
	}
	
	/**
	 * @Description: 保存评标指标事件
	 * @author chenxw
	 */
	private void saveEvaluationFactorAction() {
		if (tenderProjectService.isInProgress(tenderProject.getId(), EvaluationStepEnum.CREDIT)) {
			MessageDialog.openInformation(getShell(), "已进入综合信用得分评审环节，不能进行此操作！");
			return;
		}
		if (!weightText.checkBigDecimalIsNull("信用评价分值占总得分的权重不能为空！")) {
			return;
		}
		creditFactor.setFactorWeight(weightText.getBigDecimalValue());
		creditFactor.setFactorFinalWeight(weightText.getBigDecimalValue());
		String isSelBaseScore = CommonEnum.NO;
		if (isEffectiveButton.getSelection()) {
			isSelBaseScore = CommonEnum.YES;
			if (!baseScoreText.checkBigDecimalIsNull("企业信用评价基准分值不能为空！")) {
				return;
			}
			if (!baseScoreText.checkBigDecimalIsInRange(BigDecimal.valueOf(80), BigDecimal.valueOf(90), "企业信用评价基准分值不得低于80分或高于90分！")) {
				return;
			}
			if (!bidderCountText.checkIntIsNull("投标企业数量不能为空！")) {
				return;
			}
		}
		creditParam.setIsSelBaseScore(isSelBaseScore);
		creditParam.setBaseScore(baseScoreText.getBigDecimalValue());
		creditParam.setBidderCount(bidderCountText.getIntValue());
		
		creditFactor.setComputerParams(creditParam.getSerialize());
		
		boolean soccuss;
		if (null == creditFactor.getId()) {
			creditFactor.setId(UuidUtils.getUUID());
			soccuss = evaluationFactorService.insert(creditFactor);
		} else {
			soccuss = evaluationFactorService.updateAssignProperty(creditFactor, new String[] { "factorWeight", "factorFinalWeight", "computerParams" });
		}
		if (soccuss) {
			MessageDialog.openShortMessage(Messages.Tips_saveSuccess);
		} else {
			MessageDialog.openError(getShell(), Messages.Tips_saveFail);
		}
	}
	
	protected void checkSubclass() {}
}
