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

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.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

import com.gx.obe.evaluation.step.inter.IDwgcDetail;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.boq.ConstructProject;
import com.gx.obe.web.entity.boq.ExtraTable;
import com.gx.obe.web.service.boq.ExtraTableService;

/** 
 * @Description: 其他项目汇总结果
 * @author guoyr 
 */
public class OtherItemCollectComposite extends Composite implements IDwgcDetail{

	{
		LogUtils.logClass(OtherItemCollectComposite.class);
	}
	private Grid itemGrid;
	private ExtraTableService extraTableService = new ExtraTableService();

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public OtherItemCollectComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		
		itemGrid = new Grid(this, SWT.H_SCROLL | SWT.V_SCROLL);
		itemGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		itemGrid.setHeaderVisible(true);
		
		GridColumn gridColumn = new GridColumn(itemGrid, SWT.LEFT);
//		gridColumn.setShowTreeTogglePic(false);
		gridColumn.setTree(true);
		gridColumn.setText("序号");
		gridColumn.setWidth(60);
		gridColumn.setMaxmumWidth(60);
		
		GridColumn nameColumn = new GridColumn(itemGrid, SWT.NONE);
		nameColumn.setText("名称");
		nameColumn.setWidth(200);
		
		GridColumn gridColumn_1 = new GridColumn(itemGrid, SWT.CENTER);
		gridColumn_1.setText("单位");
		gridColumn_1.setWidth(80);
		
		GridColumn gridColumn_2 = new GridColumn(itemGrid, SWT.RIGHT);
		gridColumn_2.setText("合价");
		gridColumn_2.setWidth(100);
		
		GridColumn gridColumn_3 = new GridColumn(itemGrid, SWT.NONE);
		gridColumn_3.setText("备注");
		gridColumn_3.setWidth(200);
		gridColumn_3.setWordWrap(true);
		
		GridItem gridItem = new GridItem(itemGrid, SWT.NONE);
		gridItem.setText("New Row");
		
		init();
	}
	
	/**
	 * @Description:  初始化界面
	 * @author guoyr
	 */
	private void init(){
		itemGrid.setItemHeight(25);
		itemGrid.setAutoHeight(true);
	}
	
	/**
	 * @Description:  刷新分布分项明细
	 * @author guoyr
	 * @param xmlResolver
	 * @param dxgcCode
	 * @param dwgcCode
	 */
	public void refreshDetail(ConstructProject unitConstructProject){
		itemGrid.removeAll();
		if(null != unitConstructProject){
			// 在单位工程下获得其他项目
			List<ExtraTable> topExtraTable = extraTableService.getExtraTableListByProjectId(unitConstructProject.getId());
			
			if(null != topExtraTable){
//				GridItem qtxmhjItem = new GridItem(itemGrid, SWT.NONE);
//				qtxmhjItem.setImage(0, ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/sum.png"));
//				qtxmhjItem.setText(1, "其他项目合计");
//				setRowContent(qtxmhjItem, topExtraTable);
				
				//				List<ExtraTable> extraTableList = topExtraTable.getChildExtraTableList();
				for(ExtraTable extraTable : topExtraTable){
					GridItem gridItem = new GridItem(itemGrid, SWT.NONE);
//					gridItem.setText(0, NumericChineseUtils.getChinese(otherItemIndex++));
					gridItem.setText(0, ObjectUtils.getObjString(extraTable.getExtraCode()));
					setRowContent(gridItem, extraTable);
					initChildExtraTable(gridItem, extraTable, false);
				}
			}
		}
	}

	/**
	 * @Description:  递归循环出其他项目合计下的所有内容
	 * @author guoyr
	 * @param parentList
	 * @param extraTable
	 */
	private void initChildExtraTable(GridItem parentItem, ExtraTable extraTable, boolean useParentNo){
		List<ExtraTable> extraTableList = extraTable.getChildExtraTableList();
		if(null != extraTableList && extraTableList.size() > 0){
			parentItem.setExpanded(true);
			int index = 1;
			for(ExtraTable childBillTable : extraTableList){
				GridItem gridItem = new GridItem(parentItem, SWT.NONE);
				if(useParentNo){
					gridItem.setText(0, parentItem.getText(0)+"."+(index ++));
				}else {
					gridItem.setText(0, (index ++)+"");
				}
				setRowContent(gridItem, childBillTable);
				initChildExtraTable(gridItem, childBillTable, true);
			}
		}
	}
	
	
	/**
	 * @Description:  设置每行的内容
	 * @author guoyr
	 * @param gridItem
	 * @param extraTable
	 */
	private void setRowContent(GridItem gridItem, ExtraTable extraTable){
		gridItem.setText(1,ObjectUtils.getObjString(extraTable.getExtraName()));
		gridItem.setText(2,ObjectUtils.getObjString(extraTable.getUnit()));
		gridItem.setText(3,ObjectUtils.getObjString(extraTable.getTotal()));
		gridItem.setText(4,ObjectUtils.getObjString(extraTable.getRemark()));
	}

	public Composite getComposite(){
		return this;
	}
	protected void checkSubclass() {
	}
}
