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.components.core.Constants;
import com.gx.obe.evaluation.step.inter.IDwgcDetail;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.NumberFormatUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.boq.ConstructProject;
import com.gx.obe.web.entity.boq.ProjectSummary;
import com.gx.obe.web.service.boq.ProjectSummaryService;

/**
 * @Description: 其他项目汇总结果
 * @author guoyr
 */
public class CostTableComposite extends Composite implements IDwgcDetail {
	
	private Grid itemGrid;
	private ProjectSummaryService projectSummaryService = new ProjectSummaryService();
	{
		LogUtils.logClass(CostTableComposite.class);
	}
	
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public CostTableComposite(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.CENTER);
//      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(150);
		nameColumn.setWordWrap(true);
		
		GridColumn gridColumn_1 = new GridColumn(itemGrid, SWT.LEFT);
		gridColumn_1.setText("计算基数");
		gridColumn_1.setWidth(150);
		gridColumn_1.setWordWrap(true);
		
		GridColumn gridColumn_3 = new GridColumn(itemGrid, SWT.RIGHT);
		gridColumn_3.setText("费率（%）");
		gridColumn_3.setWidth(20);
		
		GridColumn gridColumn_2 = new GridColumn(itemGrid, SWT.RIGHT);
		gridColumn_2.setText("金额（元）");
		gridColumn_2.setWidth(50);
		
		GridItem gridItem = new GridItem(itemGrid, SWT.NONE);
		gridItem.setText("New Row");
		
		init();
	}
	
	/**
	 * @Description: 初始化界面
	 * @author guoyr
	 */
	private void init() {
		itemGrid.setItemHeight(Constants.ROW_HIGHT);
		itemGrid.setAutoHeight(true);
	}
	
	/**
	 * @Description: 刷新计日工
	 * @author guoyr
	 * @param xmlResolver
	 * @param dxgcCode
	 * @param dwgcCode
	 */
	public void refreshDetail(ConstructProject unitConstructProject) {
		itemGrid.removeAll();
		if (null != unitConstructProject) {
			
			List<ProjectSummary> topProjectSummaryList = projectSummaryService.getProjectSummaryAndChildList(unitConstructProject.getId());
//          GridItem topItem = new GridItem(itemGrid, SWT.NONE);
//          topItem.setImage(0, ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/sum.png"));
//          topItem.setText(1, "单位工程造价合计");
//          
			if (null != topProjectSummaryList) {
				for (ProjectSummary projectSummary : topProjectSummaryList) {
					GridItem gridItem = new GridItem(itemGrid, SWT.NONE);
					gridItem.setText(0, ObjectUtils.getObjString(projectSummary.getSummaryCode()));
//                  gridItem.setText(0, NumericChineseUtils.getChinese(zljezcIndex++));
					setRowContent(gridItem, projectSummary);
					initChildDayWorks(gridItem, projectSummary, false);
				}
				
			}
		}
		itemGrid.resizeColumnWidth();
	}
	
	/**
	 * @Description: 递归循环出计日工
	 * @author guoyr
	 * @param parentItem
	 * @param dayWorks
	 */
	private void initChildDayWorks(GridItem parentItem, ProjectSummary dayWorks, boolean useParentNo) {
		List<ProjectSummary> appraisalPriceList = dayWorks.getChildList();
		if (null != appraisalPriceList && appraisalPriceList.size() > 0) {
			parentItem.setExpanded(true);
			int index = 1;
			for (ProjectSummary childDayWorks : appraisalPriceList) {
				GridItem gridItem = new GridItem(parentItem, SWT.NONE);
				if (useParentNo) {
					
					gridItem.setText(0, parentItem.getText(0) + "." + (index++));
				} else {
					gridItem.setText(0, (index++) + "");
				}
				setRowContent(gridItem, childDayWorks);
				initChildDayWorks(gridItem, childDayWorks, true);
			}
		}
	}
	
	/**
	 * @Description: 设置每行的内容
	 * @author guoyr
	 * @param gridItem
	 * @param appraisalPrice
	 */
	private void setRowContent(GridItem gridItem, ProjectSummary appraisalPrice) {
		gridItem.setText(1, ObjectUtils.getObjString(appraisalPrice.getSummaryName()));
		gridItem.setText(2, ObjectUtils.getObjString(appraisalPrice.getFormula()));
		gridItem.setText(3, NumberFormatUtils.format(appraisalPrice.getRate()));
		gridItem.setText(4, NumberFormatUtils.format(appraisalPrice.getTotal()));
	}
	
	public Composite getComposite() {
		return this;
	}
	
	protected void checkSubclass() {}
}
