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

import java.text.MessageFormat;
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.ColorConstants;
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.inter.IBidPriceShowTable;
import com.gx.obe.message.Messages;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.SupplierOpeningResult;
import com.gx.obe.web.entity.price.BidPrice;
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.CellRender;
import de.kupzog.ktable.renderers.FixedCellRenderer;
import de.kupzog.ktable.renderers.TextCellRenderer;

/**
 * @Description: 投标总报价管理
 * @author guoyr
 */
public class ShowBidPriceTableComposite extends Composite implements IBidPriceShowTable{
	
	private KTable bidPriceTable;
	private CommonKtableCModel commKTableModel;
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public ShowBidPriceTableComposite(Composite parent, int style) {
		super(parent, style);
		setBackground(ColorConstants.BORDER_LINE_COLOR);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = 1;
		gridLayout.marginWidth = 1;
		setLayout(gridLayout);
		
		Composite composite = new Composite(this, SWT.NONE);
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		composite.setLayout(gl_composite);
		  
		bidPriceTable = new KTable(composite, SWTX.AUTO_SCROLL | SWT.FULL_SELECTION | SWTX.FILL_WITH_LASTCOL | SWTX.EDIT_ON_KEY);
		bidPriceTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
	}
	
	/** 
	 * @Description: 刷新投标报价
	 * @author guoyr
	 * @param tenderProjectEntity
	 * @param tenderProjectRuleEntity
	 * @param supplierList 
	 */
	public void refreshBidPriceResult(final TenderProjectEntity tenderProjectEntity, final TenderProjectRuleEntity tenderProjectRuleEntity, final List<Supplier> supplierList, List<BidPrice> bidPriceList){
		commKTableModel = new CommonKtableCModel(bidPriceTable, 1, 0, supplierList.size(), 3);
		bidPriceTable.setAutoComputerSurplusWidth(false);
		commKTableModel.setContentRowHeight(Constants.ROW_HIGHT);
		commKTableModel.setColumnWidth(0, 50);
		commKTableModel.setColumnMinWidth(1, 250);
		commKTableModel.setColumnWidth(2, 200);
		
		// 设置单元格内容
		CellContentRender contentRender = new CellContentRender(){
			public Object receiveContent(int row, int col) {
				int currRow = row - commKTableModel.getFixedRowCount();
				if(row < commKTableModel.getFixedRowCount()){
					if(col == 0){
						return Messages.Num ;
					}else if(col == 1){
						return MessageFormat.format(Messages.NameFormat, com.gx.obe.components.core.I18N.getString(I18NEnum.SUPPLIERNAME, tenderProjectEntity.getTenderType()));
					}else if(col == 2){
						return "投标报价(" + tenderProjectRuleEntity.getPriceUnitUseDefault()+")";
					}
				}else {
					if(col == 0){
						return row +"";
					}else{
						Supplier supplier = supplierList.get(currRow);
						if(col == 1){
							return supplier.getSupplierName();
						}else if(col == 2 && null != supplier.getSupplierOpeningResult()){
							SupplierOpeningResult supplierOpeningResult = supplier.getSupplierOpeningResult();
							if(null == supplierOpeningResult.getBidPrice()){
								supplierOpeningResult.setBidPrice(supplierOpeningResult.getEvaluationPrice());
							}
							if(null != supplierOpeningResult.getBidPrice()){
								return MoneyUtils.getMoneyFormatByUnit(supplierOpeningResult.getBidPrice(), null != tenderProjectRuleEntity ? tenderProjectRuleEntity.getPriceUnit() : "");
							}
						}
					}
				}
				return "";
			}
		};
		commKTableModel.setCellContentRender(contentRender);
		
		// 设置单元格渲染器
		CellRender cellRender = new CellRender(){
			public Object receiveCellRender(int row, int col) {
				if(commKTableModel.isFixedCell(col, row)){
					return new FixedCellRenderer();
				}else if(col == 2){
					return new TextCellRenderer();
				}
				return null;
			}
		};
		commKTableModel.setCellRender(cellRender);
		
		// 编辑单元格对齐方式
		CellAlignmentRender cellAlignmentRender = new CellAlignmentRender(){
			public int receiveCellAlignment(int row, int col) {
				if(row < commKTableModel.getFixedRowCount()){
					return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
				}else {
					if(col == 1){
						return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_LEFT;
					}else{
						return SWTX.ALIGN_VERTICAL_CENTER | SWTX.ALIGN_HORIZONTAL_CENTER;
					}
				
				}
			}
		};
		commKTableModel.setCellAlignmentRender(cellAlignmentRender);
		
		bidPriceTable.setModel(commKTableModel);
	
	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
