package com.gx.obe.evaluation.step.composite.business;

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

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.components.core.util.MoneyUtils;
import com.gx.obe.evaluation.step.entity.BusinessEvalResult;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;
import com.swtdesigner.SWTResourceManager;

import de.kupzog.ktable.KTable;
import de.kupzog.ktable.SWTX;
import de.kupzog.ktable.model.CommonKtableCModel;
import de.kupzog.ktable.renderers.CellAlignmentRender;
import de.kupzog.ktable.renderers.CellContentRender;
import de.kupzog.ktable.renderers.RowHeightRender;

/** 
 * @Description: 商务评审结果界面
 * @author chenxw 
 */
public class BusinessEvalResultComposite extends Composite {
	
	private KTable resultTable;
	
	private TenderProjectEntity tenderProject;
	private TenderProjectRuleEntity projectRule;
	private BigDecimal basePrice;
	private List<BusinessEvalResult> businessEvalResultList;
	
	/** 
	 * @author chenxw
	 * @param parent
	 * @param style 
	 */
	public BusinessEvalResultComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gl_evaluationTableComposite = new GridLayout(1, false);
		gl_evaluationTableComposite.marginHeight = 0;
		gl_evaluationTableComposite.marginWidth = 0;
		setLayout(gl_evaluationTableComposite);
		GridData gd_evaluationTableComposite = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_evaluationTableComposite.minimumHeight = 200;
		setLayoutData(gd_evaluationTableComposite);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		
	}
	
	/**
	 * @Description: 刷新
	 * @author chenxw
	 * @param tenderProject
	 * @param projectRule
	 * @param basePrice
	 * @param businessEvalResultList 
	 */
	public void refresh(TenderProjectEntity tenderProject, TenderProjectRuleEntity projectRule, BigDecimal basePrice, List<BusinessEvalResult> businessEvalResultList) {
		this.tenderProject = tenderProject;
		this.projectRule = projectRule;
		this.basePrice = basePrice;
		this.businessEvalResultList = businessEvalResultList;
		refreshTableComposite();
	}
	
	/**
	 * @Description: 刷新表格界面
	 * @author chenxw 
	 */
	private void refreshTableComposite() {
		if (null != resultTable) {
			resultTable.dispose();
		}
		
		resultTable = new KTable(this, SWTX.AUTO_SCROLL | SWT.FULL_SELECTION | SWTX.EDIT_ON_KEY);
		resultTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
//		resultTable.setModel(new KTableCModel(resultTable, 1, 1, 1, 1));
		
		final String supplierName = com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProject.getTenderType());
		final int headRow = 1;
		final int bidNameCol = 0;
		final int bidPriceCol = 1;
		final int basePriceCol = 2;
		final int differenceCol = 3;
		final int scoreCol = 4;
		final int sortNoCol = 5;
		
		CommonKtableCModel tableModel = new CommonKtableCModel(resultTable, headRow, 1, businessEvalResultList.size(), 5);
		
		tableModel.setColumnWidth(bidNameCol, 400);
		tableModel.setColumnWidth(bidPriceCol, 40);
		tableModel.setColumnWidth(basePriceCol, 40);
		tableModel.setColumnWidth(differenceCol, 40);
		tableModel.setColumnWidth(scoreCol, 40);
		tableModel.setColumnWidth(sortNoCol, 40);
		
		tableModel.setRowHeightRender(new RowHeightRender() {
			public int receiveRowHeight(int row) {
				return Constants.ROW_HIGHT;
			}
		});
		
		tableModel.addRowSpan(headRow, basePriceCol, businessEvalResultList.size());
		
		tableModel.setCellContentRender(new CellContentRender() {
			public Object receiveContent(int row, int col) {
				if (row < headRow) {
					switch (col) {
						case bidNameCol: return supplierName.concat("名称");
						case bidPriceCol: return "评标价（" + projectRule.getEvalPriceUnit() + "）";
						case basePriceCol: return "基准价（" + projectRule.getEvalPriceUnit() + "）";
						case differenceCol: return "偏差百分比（%）";
						case scoreCol: return "得分";
						case sortNoCol: return "排名";
						default: return "";
					}
				} else {
					BusinessEvalResult businessEvalResult = businessEvalResultList.get(row - headRow);
					switch (col) {
						case bidNameCol: return ObjectUtils.getObjString(businessEvalResult.getSupplierName());
						case bidPriceCol: return MoneyUtils.getMoneyFormatByUnit(businessEvalResult.getEvaluationPrice(), projectRule.getEvalPriceUnit());
						case basePriceCol: return MoneyUtils.getMoneyFormatByUnit(basePrice, projectRule.getEvalPriceUnit());
						case differenceCol: return ObjectUtils.getObjString(BigDecimalUtils.round(businessEvalResult.getDeviationRate(), 2));
						case scoreCol: return ObjectUtils.getObjString(BigDecimalUtils.round(businessEvalResult.getScore(), projectRule.getScoreRoundIntVal()));
						case sortNoCol: return businessEvalResult.getSortNo() == Integer.MAX_VALUE ? "" : ObjectUtils.getObjString(businessEvalResult.getSortNo());
						default: return "";
					}
				}
			}
		});
		
		tableModel.setCellAlignmentRender(new CellAlignmentRender() {
			public int receiveCellAlignment(int row, int col) {
				if (row < headRow) {
					return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
				} else {
					switch (col) {
						case bidNameCol: return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_LEFT;
						case bidPriceCol: return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_RIGHT;
						case basePriceCol: return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_RIGHT;
						case differenceCol: return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
						case scoreCol: return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
						case sortNoCol: return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
						default: return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
					}
				}
			}
		});
		
		resultTable.setModel(tableModel);
		resultTable.refresh();
		layout();
	}
	
	protected void checkSubclass() {}
	
}
