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.ServiceCosts;
import com.gx.obe.web.service.boq.ServiceCostsService;

/** 
 * @Description: 其它项目总承包服务明细
 * @author guoyr 
 */
public class OtherItemZcbfwComposite extends Composite implements IDwgcDetail{

	{
		LogUtils.logClass(OtherItemZcbfwComposite.class);
	}
	private Grid itemGrid;
	private ServiceCostsService serviceCostsService = new ServiceCostsService();

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public OtherItemZcbfwComposite(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.NONE);
		gridColumn.setTree(true);
		gridColumn.setText("序号");
		gridColumn.setWidth(60);
		gridColumn.setMaxmumWidth(60);
		
		GridColumn nameColumn = new GridColumn(itemGrid, SWT.NONE);
		nameColumn.setText("名称");
		nameColumn.setWidth(150);
		nameColumn.setWordWrap(true);
		
		GridColumn contentColumn = new GridColumn(itemGrid, SWT.NONE);
		contentColumn.setText("内容");
		contentColumn.setWidth(150);
		contentColumn.setWordWrap(true);
		
		GridColumn gridColumn_1 = new GridColumn(itemGrid, SWT.RIGHT);
		gridColumn_1.setText("项目价值");
		gridColumn_1.setWidth(80);
		
		GridColumn gridColumn_2 = new GridColumn(itemGrid, SWT.RIGHT);
		gridColumn_2.setText("费率");
		gridColumn_2.setWidth(50);
		
		GridColumn hjColumn = new GridColumn(itemGrid, SWT.RIGHT);
		hjColumn.setText("合价");
		hjColumn.setWidth(100);
		
		GridColumn gridColumn_3 = new GridColumn(itemGrid, SWT.NONE);
		gridColumn_3.setText("备注");
		gridColumn_3.setWidth(150);
		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<ServiceCosts> serviceCostsList = serviceCostsService.getServiceCostsListByProjectId(unitConstructProject.getId());
//			GridItem zljeItem = new GridItem(itemGrid, SWT.NONE);
//			zljeItem.setImage(0, ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/sum.png"));
//			zljeItem.setText(1, "总承包服务费合计");
			
			if(null != serviceCostsList){
				for(ServiceCosts serviceCosts : serviceCostsList){
					GridItem gridItem = new GridItem(itemGrid, SWT.NONE);
					gridItem.setText(0, ObjectUtils.getObjString(serviceCosts.getOrdCode()));
					setRowContent(gridItem, serviceCosts);
					initChildServiceCosts(gridItem, serviceCosts);
				}
			}
		}
	}
	
	/**
	 * @Description:  递归循环出总承包服务费
	 * @author guoyr
	 * @param parentItem
	 * @param serviceCosts
	 */
	private void initChildServiceCosts(GridItem parentItem, ServiceCosts serviceCosts){
		List<ServiceCosts> serviceCostsList = serviceCosts.getChildServiceCostsList();
		if(null != serviceCostsList && serviceCostsList.size() > 0){
			parentItem.setExpanded(true);
			int index = 1;
			for(ServiceCosts childServiceCosts : serviceCostsList){
				GridItem gridItem = new GridItem(parentItem, SWT.NONE);
//				gridItem.setText(0, parentItem.getText(0)+"."+(index ++));
				gridItem.setText(0, (index ++)+"");
				setRowContent(gridItem, childServiceCosts);
//				initChildServiceCosts(gridItem, childServiceCosts);
			}
		}
	}
	
	/**
	 * @Description:  设置每行的内容
	 * @author guoyr
	 * @param gridItem
	 * @param serviceCosts
	 */
	private void setRowContent(GridItem gridItem, ServiceCosts serviceCosts){
		gridItem.setText(1,ObjectUtils.getObjString(serviceCosts.getServiceName()));
		gridItem.setText(2,ObjectUtils.getObjString(serviceCosts.getContent()));
		gridItem.setText(3,ObjectUtils.getObjString(serviceCosts.getProfit()));
		gridItem.setText(4,ObjectUtils.getObjString(serviceCosts.getRate()));
		gridItem.setText(5,ObjectUtils.getObjString(serviceCosts.getTotal()));
		gridItem.setText(6,ObjectUtils.getObjString(serviceCosts.getRemark()));
	}
	
	public Composite getComposite(){
		return this;
	}

	protected void checkSubclass() {
	}
}
