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

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

import org.eclipse.nebula.widgets.grid.Grid;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.nebula.widgets.grid.GridItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;

import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.Constants;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.util.utils.NumericChineseUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.boq.ConstructProject;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.swtdesigner.ResourceManager;

/**
 * @Description: 工程项目汇总
 * @author guoyr
 */
public class ProjectSummaryComposite extends Composite {
	
	private Grid projectSumGrid;
	private ProjectSelectListener listener;
	
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public ProjectSummaryComposite(Composite parent, int style) {
		super(parent, style);
		setLayout(new FillLayout(SWT.HORIZONTAL));
		
		projectSumGrid = new Grid(this, SWT.H_SCROLL | SWT.V_SCROLL);
		projectSumGrid.setHeaderVisible(true);
		
		GridColumn sortNumColumn = new GridColumn(projectSumGrid, SWT.NONE);
		sortNumColumn.setShowTreeTogglePic(false);
		sortNumColumn.setTree(true);
		sortNumColumn.setText("序号");
		sortNumColumn.setTreeIndent(0);
		sortNumColumn.setWidth(20);
		
		GridColumn gridColumn = new GridColumn(projectSumGrid, SWT.NONE);
		gridColumn.setText("名称");
		gridColumn.setWidth(200);
		
		GridColumn gridColumn_1 = new GridColumn(projectSumGrid, SWT.RIGHT);
		gridColumn_1.setText("金额(元)");
		gridColumn_1.setWidth(100);
		
		GridColumn gridColumn_2 = new GridColumn(projectSumGrid, SWT.RIGHT);
		gridColumn_2.setText("安全文明施工费(元)");
		gridColumn_2.setWidth(150);
		
		GridColumn gridColumn_3 = new GridColumn(projectSumGrid, SWT.RIGHT);
		gridColumn_3.setText("规费(元)");
		gridColumn_3.setWidth(100);
		
		GridColumn gridColumn_4 = new GridColumn(projectSumGrid, SWT.RIGHT);
		gridColumn_4.setText("暂估价(元)");
		gridColumn_4.setWidth(100);
		
		GridItem gridItem = new GridItem(projectSumGrid, SWT.NONE);
		gridItem.setText("New Row");
		
		init();
	}
	
	/**
	 * @Description: 初始化界面
	 * @author guoyr
	 */
	private void init() {
		projectSumGrid.setTreeLinesVisible(false);
		projectSumGrid.setItemHeight(Constants.ROW_HIGHT);
		listener = new ProjectSelectListener() {
			public void select(TenderProjectEntity tenderProject, Supplier supplier, ConstructProject constructProject) {
				refreshProjectSumGrid(tenderProject, supplier, constructProject);
			}
			
		};
	}
	
	/**
	 * @Description: 刷新项目汇总Grid
	 * @author guoyr
	 */
	private void refreshProjectSumGrid(TenderProjectEntity tenderProject, Supplier supplier, ConstructProject constructProject) {
		projectSumGrid.removeAll();
		
		if (null != constructProject) {
			GridItem gcxmbjItem = new GridItem(projectSumGrid, SWT.NONE);
			gcxmbjItem.setText(0, "一");
			gcxmbjItem.setText(1, "工程项目总价");
			gcxmbjItem.setText(2, ObjectUtils.getObjString(constructProject.getTotal()));// 工程总造价
			if (null != constructProject.getSafeFee() && constructProject.getSafeFee().compareTo(BigDecimal.ZERO) > 0) {
				gcxmbjItem.setText(3, ObjectUtils.getObjString(constructProject.getSafeFee()));// 安全文明施工费
			}
			if (null != constructProject.getLawFee() && constructProject.getLawFee().compareTo(BigDecimal.ZERO) > 0) {
				gcxmbjItem.setText(4, ObjectUtils.getObjString(constructProject.getLawFee()));// 规费
			}
			if (null != constructProject.getAppraisal() && constructProject.getAppraisal().compareTo(BigDecimal.ZERO) > 0) {
				gcxmbjItem.setText(5, ObjectUtils.getObjString(constructProject.getAppraisal()));// 暂估价
			}
			
			// 获得单项工程
			List<ConstructProject> singleConstructionList = constructProject.getChildProjectList();
			if (null != singleConstructionList) {
				GridItem dxgcSumItem = new GridItem(projectSumGrid, SWT.NONE);
				dxgcSumItem.setExpanded(true);
				dxgcSumItem.setText(0, NumericChineseUtils.getChinese(2));
				dxgcSumItem.setText(1, "单项工程费汇总");
				BigDecimal je = BigDecimal.ZERO;
				BigDecimal aqwmsgf = null;
				BigDecimal gf = null;
				BigDecimal zgj = null;
				int dxgcIndex = 1;
				for (ConstructProject singleConstruction : singleConstructionList) {
					GridItem dxgcItem = new GridItem(dxgcSumItem, SWT.NONE);
					dxgcItem.setExpanded(true);
					dxgcItem.setText(0, dxgcIndex + "");
					je = BigDecimalUtils.add(je, singleConstruction.getTotal());
					aqwmsgf = BigDecimalUtils.add(aqwmsgf, singleConstruction.getSafeFee());
					gf = BigDecimalUtils.add(gf, singleConstruction.getLawFee());
					zgj = BigDecimalUtils.add(zgj, singleConstruction.getAppraisal());
					dxgcItem.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/engineering_building.png"));
					dxgcItem.setText(1, ObjectUtils.getObjString(singleConstruction.getProjectName()));// 单项工程名称
					dxgcItem.setText(2, ObjectUtils.getObjString(singleConstruction.getTotal()));// 单项工程金额
					dxgcItem.setText(3, ObjectUtils.getObjString(singleConstruction.getSafeFee()));// 安全文明施工费
					dxgcItem.setText(4, ObjectUtils.getObjString(singleConstruction.getLawFee()));// 规费
					dxgcItem.setText(5, ObjectUtils.getObjString(singleConstruction.getAppraisal()));// 暂估价
					// 获得单位工程列表
					List<ConstructProject> unitConstructionList = singleConstruction.getChildProjectList();
					if (null != unitConstructionList) {
						int dwgcIndex = 1;
						for (ConstructProject unitConstruction : unitConstructionList) {
							GridItem dwgcItem = new GridItem(dxgcItem, SWT.NONE);
							dwgcItem.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/engineering_building_item.png"));
							dwgcItem.setText(0, (dxgcIndex + "." + dwgcIndex));
							dwgcItem.setText(1, ObjectUtils.getObjString(unitConstruction.getProjectName()));// 单位工程名称
							dwgcItem.setText(2, ObjectUtils.getObjString(unitConstruction.getTotal()));// 单位工程金额
							dwgcItem.setText(3, ObjectUtils.getObjString(unitConstruction.getSafeFee()));// 安全文明施工费
							dwgcItem.setText(4, ObjectUtils.getObjString(unitConstruction.getLawFee()));// 规费
							dwgcItem.setText(5, ObjectUtils.getObjString(unitConstruction.getAppraisal()));// 暂估价
							dwgcIndex++;
						}
					}
					dxgcIndex++;
				}
				dxgcSumItem.setText(2, ObjectUtils.getObjString(je));
				dxgcSumItem.setText(3, ObjectUtils.getObjString(aqwmsgf));
				dxgcSumItem.setText(4, ObjectUtils.getObjString(gf));
				dxgcSumItem.setText(5, ObjectUtils.getObjString(zgj));
			}
		}
		projectSumGrid.resizeColumnWidth();
	}
	
	public interface ProjectSelectListener {
		public void select(TenderProjectEntity tenderProject, Supplier supplier, ConstructProject constructProject);
		
	}
	
	public ProjectSelectListener getListener() {
		return listener;
	}
	
	public void setListener(ProjectSelectListener listener) {
		this.listener = listener;
	}
	
	protected void checkSubclass() {}
}
