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

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.business.vo.RatesCheckResult;
import com.gx.obe.components.core.Constants;
import com.gx.obe.components.core.enumeration.EngineerConstants;
import com.gx.obe.evaluation.step.inter.IBoqChecker;
import com.gx.obe.evaluation.step.inter.IChecker;
import com.gx.obe.util.utils.NumberFormatUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 不可竞争费检查
 * @author guoyr
 */
public class RatesCompeteItemListComposite extends Composite implements IBoqChecker {
	
	private Grid checkItemGrid;
	private GridColumn nameColumn;
	private GridColumn ratesPriceColumn;
	private GridColumn remarkColumn;
	
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public RatesCompeteItemListComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		
		checkItemGrid = new Grid(this, SWT.H_SCROLL | SWT.V_SCROLL);
		checkItemGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		checkItemGrid.setHeaderVisible(true);
		checkItemGrid.setWordWrapHeader(true);
		
		GridColumn gridColumn_num = new GridColumn(checkItemGrid, SWT.NONE);
		gridColumn_num.setTree(true);
		gridColumn_num.setText("序号");
		gridColumn_num.setWidth(100);
		gridColumn_num.setMaxmumWidth(120);
		gridColumn_num.setTreeIndent(10);
		
		nameColumn = new GridColumn(checkItemGrid, SWT.NONE);
		nameColumn.setText("名称");
		nameColumn.setWidth(150);
		nameColumn.setWordWrap(true);
		
		GridColumn gridColumn_opinion = new GridColumn(checkItemGrid, SWT.CENTER);
		gridColumn_opinion.setText("     提示信息     ");
		gridColumn_opinion.setWidth(150);
		gridColumn_opinion.setWordWrap(true);
		
		ratesPriceColumn = new GridColumn(checkItemGrid, SWT.RIGHT);
		ratesPriceColumn.setText("规费/取费基数");
		ratesPriceColumn.setWidth(80);
		
		GridColumn gridColumn_6 = new GridColumn(checkItemGrid, SWT.RIGHT);
		gridColumn_6.setText("费率%");
		gridColumn_6.setWidth(30);
		gridColumn_6.setMaxmumWidth(100);
		
		GridColumn gridColumn_5 = new GridColumn(checkItemGrid, SWT.RIGHT);
		gridColumn_5.setText("计算金额");
		gridColumn_5.setWidth(50);
		
		remarkColumn = new GridColumn(checkItemGrid, SWT.NONE);
		remarkColumn.setText("费率说明");
		remarkColumn.setWidth(80);
		remarkColumn.setWordWrap(true);
		
//		
//		GridColumn gridColumn_7 = new GridColumn(checkItemGrid, SWT.RIGHT);
//		gridColumn_7.setText("规定费率%");
//		gridColumn_7.setWidth(60);
		
		init();
	}
	
	/**
	 * @Description: 初始化界面
	 * @author guoyr
	 */
	private void init() {
		checkItemGrid.removeAll();
		checkItemGrid.setItemHeight(Constants.ROW_HIGHT);
		checkItemGrid.setAutoHeight(true);
	}
	
	private void refreshColumnName(IChecker check) {
		if (DictValueEnum.CleanTender.GUIFEI_RATE_CHECK.equals(check.getDictionaryValue().getDictValueCode())) {
			nameColumn.setText("专业名称");
			nameColumn.setWidth(200);
			ratesPriceColumn.setText("规费/取费基数");
			remarkColumn.setVisible(false);
		} else {
			nameColumn.setText("计价依据");
			nameColumn.setWidth(250);
			ratesPriceColumn.setText("安全文明施工费/取费基数");
		}
	}
	
	/**
	 * @Description: 只显示示错误
	 * @author guoyr
	 * @param check
	 * @param showErrorOnly
	 */
	public void listErrorOnlyCheckDetail(IChecker check) {
		checkItemGrid.removeAll();
		checkItemGrid.resizeColumnWidth();
		refreshColumnName(check);
		List<RatesCheckResult> checkResultList = check.getCheckResultErrorList();
		
		if (null != checkResultList && checkResultList.size() > 0) {
			int i = 1;
			for (RatesCheckResult checkResult : checkResultList) {
				GridItem constructItem = new GridItem(checkItemGrid, SWT.NONE);
				setCheckResult(constructItem, checkResult);
				constructItem.setText(0, (i++) + "");
			}
		}
		
	}
	
	/**
	 * @Description: 进行检查
	 * @author guoyr
	 */
	public void listCheckDetail(IChecker check) {
		checkItemGrid.removeAll();
		checkItemGrid.resizeColumnWidth();
		refreshColumnName(check);
		List<RatesCheckResult> checkResultList = check.getCheckResultDetailTreeList();
		
		if (null != checkResultList && checkResultList.size() > 0) {
			for (RatesCheckResult checkResult : checkResultList) {
				GridItem constructItem = new GridItem(checkItemGrid, SWT.NONE);
				constructItem.setText(0, "单项工程");
//				constructItem.setText(0, NumericChineseUtils.getChinese(i)+"");
				constructItem.setBackground(SWTResourceManager.getColor(70, 180, 180));
				constructItem.setColumnSpan(1, 4);
				constructItem.setText(1, checkResult.getName());
				constructItem.setText(6, ObjectUtils.getObjString(checkResult.getCalculateRemark()));
				initSubCheckResult(checkResult, constructItem, false);
			}
		}
	}
	
	private void initSubCheckResult(RatesCheckResult checkResult, GridItem parentItem, boolean getParentNo) {
		List<RatesCheckResult> checkResultList = checkResult.getChildList();
		if (null != checkResultList && checkResultList.size() > 0) {
			parentItem.setExpanded(true);
			int index = 1;
			for (RatesCheckResult childCheckResult : checkResultList) {
				GridItem constructItem = new GridItem(parentItem, SWT.NONE);
				
				if (getParentNo) {
					constructItem.setText(0, parentItem.getText(0) + "." + (index++));
				} else {
					constructItem.setText(0, (index++) + "");
				}
				setCheckResult(constructItem, childCheckResult);
				initSubCheckResult(childCheckResult, constructItem, false);
			}
		}
		
	}
	
	/**
	 * @Description: 设置检查结果
	 * @author guoyr
	 * @param gridItem
	 * @param totalPrice
	 * @param sumPrice
	 * @param error
	 */
	private void setCheckResult(GridItem gridItem, RatesCheckResult result) {
		gridItem.setText(1, ObjectUtils.getObjString(result.getName()));
		
		if (EngineerConstants.GCXM.equals(result.getCheckType())) {
			gridItem.setText(0, "总工程");
		} else if (EngineerConstants.DXGC.equals(result.getCheckType())) {
			gridItem.setBackground(SWTResourceManager.getColor(70, 180, 180));
			gridItem.setText(0, "单项工程");
			gridItem.setText(6, ObjectUtils.getObjString(result.getCalculateRemark()));
		} else if (EngineerConstants.DWGC.equals(result.getCheckType())) {
			gridItem.setBackground(SWTResourceManager.getColor(234, 230, 250));
			gridItem.setText(0, "单位工程");
			if (result.isReasonable()) {
				gridItem.setText(2, result.getOpinion());
				gridItem.setForeground(2, SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
			} else {
				gridItem.setText(2, ObjectUtils.getObjString(result.getOpinion()));
				gridItem.setForeground(2, SWTResourceManager.getColor(SWT.COLOR_RED));
			}
			gridItem.setText(3, NumberFormatUtils.format(result.getPrice()));
			gridItem.setText(5, NumberFormatUtils.format(result.getCalculate()));
		} else {
			gridItem.setText(3, NumberFormatUtils.format(result.getPrice()));
			gridItem.setText(4, NumberFormatUtils.format(result.getRate()));
			gridItem.setText(5, NumberFormatUtils.format(result.getCalculate()));
			gridItem.setText(6, ObjectUtils.getObjString(result.getCalculateRemark()));
		}
		
//		gridItem.setText(4, ObjectUtils.getObjString(result.getCalculate()));
//		gridItem.setText(5, ObjectUtils.getObjString(result.getPrice()));
//		gridItem.setText(6, ObjectUtils.getObjString(result.getRate()));
//		gridItem.setText(7, ObjectUtils.getObjString(result.getBaseRate()));
//		gridItem.setText(8, ObjectUtils.getObjString(result.getOpinion()));
		
	}
	
	protected void checkSubclass() {}
}
