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

import java.math.BigDecimal;
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.enumeration.EngineerConstants;
import com.gx.obe.evaluation.step.inter.IDwgcDetail;
import com.gx.obe.util.utils.BigDecimalUtils;
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.RatesTable;
import com.gx.obe.web.service.boq.RatesTableService;

/**
 * @Description: 规费计取
 * @author guoyr
 */
public class RateGFItemListComposite extends Composite implements IDwgcDetail {
	
	{
		LogUtils.logClass(RateGFItemListComposite.class);
	}
	private Grid itemGrid;
	private static RatesTableService ratesTableService = new RatesTableService();
	
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public RateGFItemListComposite(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(60);
		gridColumn.setMaxmumWidth(60);
		
		GridColumn nameColumn = new GridColumn(itemGrid, SWT.NONE);
		nameColumn.setText("专业");
		nameColumn.setWidth(200);
		
		GridColumn gridColumn_1 = new GridColumn(itemGrid, SWT.RIGHT);
		gridColumn_1.setText("取费基数");
		gridColumn_1.setWidth(80);
		
		GridColumn gridColumn_2 = new GridColumn(itemGrid, SWT.CENTER);
		gridColumn_2.setText("费率");
		gridColumn_2.setWidth(50);
		
		GridColumn gridColumn_3 = new GridColumn(itemGrid, SWT.RIGHT);
		gridColumn_3.setText("费用");
		gridColumn_3.setWidth(50);
		gridColumn_3.setWordWrap(true);
		
		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<RatesTable> allBillTableList = ratesTableService.getRatesTableList(unitConstructProject.getTenderId(), unitConstructProject.getSupplierId(), unitConstructProject.getId(), EngineerConstants.RATE_GF);
			
			if (null != allBillTableList) {
				GridItem qtxmhjItem = new GridItem(itemGrid, SWT.NONE);
				qtxmhjItem.setText(0, "");
				qtxmhjItem.setText(1, ObjectUtils.getObjString(unitConstructProject.getProjectName()) + "（人工费+机械费）");
				qtxmhjItem.setColumnSpan(1, 2);
//				qtxmhjItem.setText(3, NumberFormatUtils.format(unitConstructProject.getLawFee()));
				
//				setRowContent(qtxmhjItem, topExtraTable);
				BigDecimal totalFeesValue = BigDecimal.ZERO;
				int index = 1;
//				List<ExtraTable> extraTableList = topExtraTable.getChildExtraTableList();
				for (RatesTable ratesTable : allBillTableList) {
					GridItem gridItem = new GridItem(itemGrid, SWT.NONE);
					gridItem.setText(0, (index++) + "");
					gridItem.setText(1, ObjectUtils.getObjString(ratesTable.getName()));
					gridItem.setText(2, NumberFormatUtils.format(ratesTable.getBasePrice()));
					gridItem.setText(3, NumberFormatUtils.format(ratesTable.getRate()) + "%");
					BigDecimal feesValue = BigDecimalUtils.div(BigDecimalUtils.mul(ratesTable.getBasePrice(), ratesTable.getRate()), new BigDecimal(100));
					totalFeesValue = BigDecimalUtils.add(totalFeesValue, feesValue);
					gridItem.setText(4, NumberFormatUtils.format(feesValue));
					gridItem.setText(5, ObjectUtils.getObjString(ratesTable.getRemark()));
				}
				qtxmhjItem.setText(4, NumberFormatUtils.format(unitConstructProject.getLawFee()) + "\r\n" + NumberFormatUtils.format(totalFeesValue));
			}
		}
	}
	
	public Composite getComposite() {
		return this;
	}
	
	protected void checkSubclass() {}
}
