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.nebula.widgets.grid.listener.GridSelectionListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

import com.gx.obe.components.core.enumeration.EngineerConstants;
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.MeasureTable;
import com.gx.obe.web.entity.boq.QuotaItem;
import com.gx.obe.web.service.boq.MeasureTableService;
import com.gx.obe.web.service.boq.QuotaItemService;

/**
 * @Description: 措施项目明细界面
 * @author guoyr
 */
public class MeasureZjDetailComposite extends Composite implements IDwgcDetail {
	{
		LogUtils.logClass(MeasureZjDetailComposite.class);
	}
	private Grid itemGrid;
	private Grid menuGrid;
	private SashForm sashForm;
	private MeasureTableService measureTableService = new MeasureTableService();
	private QuotaItemService quotaItemService = new QuotaItemService();
	
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public MeasureZjDetailComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		
		sashForm = new SashForm(this, SWT.VERTICAL);
		sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		itemGrid = new Grid(sashForm, SWT.H_SCROLL | SWT.V_SCROLL);
		itemGrid.setHeaderVisible(true);
		
		GridColumn qdxhColumn = new GridColumn(itemGrid, SWT.CENTER);
//		qdxhColumn.setTree(true);
//		qdxhColumn.setShowTreeTogglePic(false);
		qdxhColumn.setText("序号");
		qdxhColumn.setWidth(60);
		qdxhColumn.setMaxmumWidth(60);
//		qdxhColumn.setTreeIndent(10);
		
		GridColumn qdbhColumn = new GridColumn(itemGrid, SWT.NONE);
		qdbhColumn.setText("项目编码");
		qdbhColumn.setWidth(100);
		
		GridColumn qdNameColumn = new GridColumn(itemGrid, SWT.NONE);
		qdNameColumn.setText("项目名称");
		qdNameColumn.setWidth(200);
		qdNameColumn.setWordWrap(true);
		
		GridColumn qdhjColumn = new GridColumn(itemGrid, SWT.RIGHT);
		qdhjColumn.setText("金额(元)");
		qdhjColumn.setWidth(50);
		
		GridItem gridItem = new GridItem(itemGrid, SWT.NONE);
		gridItem.setText("New Row");
		
		menuGrid = new Grid(sashForm, SWT.H_SCROLL | SWT.V_SCROLL);
		menuGrid.setHeaderVisible(true);
		
		GridColumn gljxh = new GridColumn(menuGrid, SWT.CENTER);
		gljxh.setText("序号");
		gljxh.setWidth(60);
		gljxh.setMaxmumWidth(60);
		
		GridColumn gljCode = new GridColumn(menuGrid, SWT.NONE);
		gljCode.setText("定额编码");
		gljCode.setWidth(80);
		
		GridColumn gridColumn_6 = new GridColumn(menuGrid, SWT.NONE);
		gridColumn_6.setText("定额名称");
		gridColumn_6.setWidth(150);
		gridColumn_6.setWordWrap(true);
		
		GridColumn gridColumn_7 = new GridColumn(menuGrid, SWT.CENTER);
		gridColumn_7.setText("单位");
		gridColumn_7.setWidth(60);
		
		GridColumn gridColumn_8 = new GridColumn(menuGrid, SWT.RIGHT);
		gridColumn_8.setText("数量");
		gridColumn_8.setWidth(50);
		
		GridColumn dedjColumn_9 = new GridColumn(menuGrid, SWT.RIGHT);
		dedjColumn_9.setText("定额单价");
		dedjColumn_9.setWidth(80);
//		
//		GridColumn scdjColumn = new GridColumn(menuGrid, SWT.RIGHT);
//		scdjColumn.setText("市场单价");
//		scdjColumn.setWidth(80);
//		scdjColumn.setWordWrap(true);
		
		GridColumn hjColumn = new GridColumn(menuGrid, SWT.RIGHT);
		hjColumn.setText("合价");
		hjColumn.setWidth(100);
		hjColumn.setWordWrap(true);
		
		GridColumn rgfColumn = new GridColumn(menuGrid, SWT.RIGHT);
		rgfColumn.setText("人工费");
		rgfColumn.setWidth(50);
		
		GridColumn clfColumn = new GridColumn(menuGrid, SWT.RIGHT);
		clfColumn.setText("材料费");
		clfColumn.setWidth(50);
		
		GridColumn jxfColumn = new GridColumn(menuGrid, SWT.RIGHT);
		jxfColumn.setText("机械费");
		jxfColumn.setWidth(50);
		
		GridColumn zgjColumn = new GridColumn(menuGrid, SWT.RIGHT);
		zgjColumn.setText("暂估价");
		zgjColumn.setWidth(50);
		
		GridColumn glfColumn = new GridColumn(menuGrid, SWT.RIGHT);
		glfColumn.setText("管理费");
		glfColumn.setWidth(50);
		
		GridColumn profitColumn = new GridColumn(menuGrid, SWT.RIGHT);
		profitColumn.setText("利润");
		profitColumn.setWidth(50);
		
		GridItem gridItem_1 = new GridItem(menuGrid, SWT.NONE);
		gridItem_1.setText("New Row");
		sashForm.setWeights(new int[] { 6, 4 });
		
		init();
	}
	
	/**
	 * @Description: 初始化界面
	 * @author guoyr
	 */
	private void init() {
		itemGrid.setAutoHeight(true);
//		itemGrid.setShowEvenBackground(false);
		itemGrid.setItemHeight(25);
		menuGrid.setAutoHeight(true);
		menuGrid.setItemHeight(25);
//		menuGrid.setItemHeight(Constants.ROW_HIGHT);
		itemGrid.addListener(SWT.Selection, new GridSelectionListener() {
			public void leftMouseButtonDown(GridItem item) {
				listQuotaItemDetailList();
			}
		});
	}
	
	/**
	 * @Description: 刷新分布分项明细
	 * @author guoyr
	 * @param xmlResolver
	 * @param dxgcCode
	 * @param dwgcCode
	 */
	public void refreshDetail(ConstructProject unitConstructProject) {
		itemGrid.removeAll();
		unitConstructProject.getId();
		// 在单位工程下获得措施
		List<MeasureTable> measureTableList = measureTableService.getMeasureTableListByProjectId(unitConstructProject.getId(), EngineerConstants.CSXM_ZJ);
		if (null != measureTableList) {
			int index = 1;
			for (MeasureTable measureTable : measureTableList) {
				GridItem fbgcItem = new GridItem(itemGrid, SWT.NONE);
				fbgcItem.setText(0, (index++) + "");
				setRowContent(fbgcItem, measureTable);
//				initChildMeasureTable(fbgcItem, measureTable);
			}
		}
//		itemGrid.resizeColumnWidth();
		sashForm.setMaximizedControl(itemGrid);
		listQuotaItemDetailList();
	}
	
	/**
	 * @Description: 设置每行的内容
	 * @author guoyr
	 * @param gridItem
	 * @param measureTable
	 */
	private void setRowContent(GridItem gridItem, MeasureTable measureTable) {
		gridItem.setData("billId", measureTable.getId());
		// 分布工程背景
//		if(ObjectUtils.equals(measureTable.getKind(), EngineerConstants.FBGC)){
////			gridItem.setBackground(SWTResourceManager.getColor(70, 180, 180));
//		}else if(ObjectUtils.equals(measureTable.getKind(), EngineerConstants.QD)){
//			// 设置清单背景
////			gridItem.setBackground(SWTResourceManager.getColor(234, 233, 247));
//		}else if(ObjectUtils.equals(measureTable.getKind(), EngineerConstants.FY)){
//			// 作为费用，所以单独设置颜色
////			gridItem.setBackground(SWTResourceManager.getColor(240, 240, 240));
//		}else if(ObjectUtils.equals(measureTable.getKind(), EngineerConstants.DE)){
//			gridItem.setData("normId", measureTable.getId());
//			gridItem.setBackground(SWTResourceManager.getColor(250, 250, 250));
//		}
		gridItem.setText(1, ObjectUtils.getObjString(measureTable.getMeasureCode()));// 项目编码
		gridItem.setText(2, ObjectUtils.getObjString(measureTable.getMeasureName()));// 项目名称
		gridItem.setText(3, ObjectUtils.getObjString(measureTable.getTotal()));// 合价
//		gridItem.setText(4,ObjectUtils.getObjString(measureTable.getLabor()));// 人工费
//		gridItem.setText(5,ObjectUtils.getObjString(measureTable.getMaterialFee()));// 材料费
//		gridItem.setText(6,ObjectUtils.getObjString(measureTable.getMachineFee()));// 机械费
//		gridItem.setText(7,ObjectUtils.getObjString(measureTable.getAppraisal()));// 暂估价
//		gridItem.setText(8,ObjectUtils.getObjString(measureTable.getManagerFee()));// 管理费
//		gridItem.setText(9,ObjectUtils.getObjString(measureTable.getProfitFee()));// 利润
	}
	
	/**
	 * @Description: 当选中的是定额时，列出定额下面的工料机明细
	 * @author guoyr
	 */
	private void listQuotaItemDetailList() {
		menuGrid.removeAll();
		if (itemGrid.getSelectionCount() > 0) {
			String billId = (String) itemGrid.getSelection()[0].getData("billId");
			if (null != billId) {
				sashForm.setMaximizedControl(null);
				// 在定额下面获得工料机明细
				List<QuotaItem> quotaItemList = quotaItemService.getBillQuotaItemList(billId);
				if (null != quotaItemList && quotaItemList.size() > 0) {
					int index = 1;
					for (QuotaItem ratLinkRes : quotaItemList) {
						GridItem gridItem = new GridItem(menuGrid, SWT.NONE);
						gridItem.setText(0, (index++) + "");//
						// BH和THQBH相同
//					deItem.setText(1,ObjectUtils.getObjString(gljElement.attributeValue("BH")));// 编码
						gridItem.setText(1, ObjectUtils.getObjString(ratLinkRes.getQuotaCode()));// 工料机编码
						gridItem.setText(2, ObjectUtils.getObjString(ratLinkRes.getQuotaName()));// 名称
						gridItem.setText(3, ObjectUtils.getObjString(ratLinkRes.getUnit()));// 单位
						gridItem.setText(4, NumberFormatUtils.format(ratLinkRes.getQuantity()));// 数量
						gridItem.setText(5, NumberFormatUtils.format(ratLinkRes.getPrice()));// 
						gridItem.setText(6, NumberFormatUtils.format(ratLinkRes.getTotal()));// 
						gridItem.setText(7, ObjectUtils.getObjString(ratLinkRes.getLabor()));// 人工费
						gridItem.setText(8, ObjectUtils.getObjString(ratLinkRes.getMaterialFee()));// 材料费
						gridItem.setText(9, ObjectUtils.getObjString(ratLinkRes.getMachineFee()));// 机械费
						gridItem.setText(10, ObjectUtils.getObjString(ratLinkRes.getAppraisal()));// 暂估价
						gridItem.setText(11, ObjectUtils.getObjString(ratLinkRes.getManagerFee()));// 管理费
						gridItem.setText(12, ObjectUtils.getObjString(ratLinkRes.getProfitFee()));// 利润
						
					}
				}
			}
			sashForm.setMaximizedControl(null);
		} else {
			sashForm.setMaximizedControl(itemGrid);
		}
	}
	
	public Composite getComposite() {
		return this;
	}
	
	protected void checkSubclass() {}
}
