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.DictValueEnum;
import com.gx.obe.components.core.enumeration.EngineerConstants;
import com.gx.obe.evaluation.step.inter.IDwgcDetail;
import com.gx.obe.evaluation.step.inter.IGljItemDetail;
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.MaterialSummary;
import com.gx.obe.web.service.boq.MaterialSummaryService;

/** 
 * @Description: 工料机明细
 * @author guoyr 
 */
public class GljItemDetailComposite extends Composite implements IGljItemDetail, IDwgcDetail{

	{
		LogUtils.logClass(GljItemDetailComposite.class);
	}
	private Grid itemGrid;
	private MaterialSummaryService materialSummaryService = new MaterialSummaryService();

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public GljItemDetailComposite(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.setText("序号");
		gridColumn.setWidth(40);
		
		GridColumn codeColumn = new GridColumn(itemGrid, SWT.CENTER);
		codeColumn.setText("编码");
		codeColumn.setWidth(100);
		
		GridColumn nameColumn = new GridColumn(itemGrid, SWT.NONE);
		nameColumn.setText("名称");
		nameColumn.setWidth(150);
		nameColumn.setMaxmumWidth(200);
		nameColumn.setWordWrap(true);
		
		GridColumn gridColumn_1 = new GridColumn(itemGrid, SWT.CENTER);
		gridColumn_1.setText("单位");
		gridColumn_1.setWidth(50);
		
		GridColumn countColumn = new GridColumn(itemGrid, SWT.RIGHT);
		countColumn.setText("数量");
		countColumn.setWidth(80);
		
		GridColumn djColumn = new GridColumn(itemGrid, SWT.RIGHT);
		djColumn.setText("定额价（元）");
		djColumn.setWidth(90);
		
		GridColumn scjColumn = new GridColumn(itemGrid, SWT.RIGHT);
		scjColumn.setText("市场价（元）");
		scjColumn.setWidth(90);
		
		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.setWordWrap(true);
		gridColumn_3.setWidth(150);
		gridColumn_3.setMaxmumWidth(200);
		gridColumn_3.setMinimumWidth(150);
		
		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 chenxw
	 * @param unitConstructProject 
	 */
	public void refreshDetail(ConstructProject unitConstructProject) {
        itemGrid.removeAll();
        if (null == unitConstructProject) {
            return;
        }
        List<MaterialSummary> materialSummaryList = materialSummaryService.getMaterialSummaryList(unitConstructProject.getId(), EngineerConstants.RCJHZ_ZC);
        int index = 1;
        for (MaterialSummary materialSummary : materialSummaryList) {
            GridItem materialItem = new GridItem(itemGrid, SWT.NONE);
            materialItem.setText(0, (index++) + "");
            setRowContent(materialItem, materialSummary);
        }
        itemGrid.resizeColumnWidth();
    }
	
	/**
	 * @Description:  显示计日工明细
	 * @author guoyr
	 * @param xmlResolver
	 * @param dxgcCode
	 * @param dwgcCode
	 */
	public void refreshDetail(ConstructProject unitConstructProject, String kind){
		itemGrid.removeAll();
		
		if(null != unitConstructProject){
			/**
			 * 工料机汇总(00：其他；01：人工；02：材料；03：机械；04：主材；05：设备)
			 */
			if(kind.endsWith(DictValueEnum.MaterialSummary.RG)){// 人工
				kind = EngineerConstants.RCJHZ_RG;
			}else if(kind.endsWith(DictValueEnum.MaterialSummary.CL)){// 材料
				kind = EngineerConstants.RCJHZ_CL;
			}else if(kind.endsWith(DictValueEnum.MaterialSummary.JX)){// 机械
				kind = EngineerConstants.RCJHZ_JX;
			}else if(kind.endsWith(DictValueEnum.MaterialSummary.SB)){// 设备
				kind = EngineerConstants.RCJHZ_SB;
			}else if(kind.endsWith(DictValueEnum.MaterialSummary.QT)){// 其他
				kind = EngineerConstants.RCJHZ_QT;
			}
			MaterialSummary topMaterialSummary = materialSummaryService.getMaterialSummary(unitConstructProject.getId(), kind);
			if(null != topMaterialSummary){
				List<MaterialSummary> materialSummaryList = topMaterialSummary.getChildMaterialList();
				int index = 1;
				for(MaterialSummary materialSummary : materialSummaryList){
					GridItem materialItem = new GridItem(itemGrid, SWT.NONE);
					materialItem.setText(0, (index++)+"");
					setRowContent(materialItem, materialSummary);
				}
			}
			itemGrid.resizeColumnWidth();
		}
	}
	
	/**
	 * @Description:  设置每行的内容
	 * @author guoyr
	 * @param gridItem
	 * @param materialSummary
	 */
	private void setRowContent(GridItem gridItem, MaterialSummary materialSummary){
		gridItem.setText(1,ObjectUtils.getObjString(materialSummary.getMaterialCode()));
		gridItem.setText(2,ObjectUtils.getObjString(materialSummary.getMaterialName()));
		gridItem.setText(3,ObjectUtils.getObjString(materialSummary.getUnit()));
		gridItem.setText(4,ObjectUtils.getObjString(materialSummary.getQuantity()));
		gridItem.setText(5,ObjectUtils.getObjString(materialSummary.getOrgPrice()));
		gridItem.setText(6,ObjectUtils.getObjString(materialSummary.getPrice()));
		gridItem.setText(7,ObjectUtils.getObjString(materialSummary.getTotal()));
		gridItem.setText(8,ObjectUtils.getObjString(materialSummary.getRemark()));
	}

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