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

/**
 * @Description: 不可竞争费检查
 * @author guoyr
 */
public class UnCompeteItemListComposite extends Composite implements IBoqChecker {
	
	private Grid checkItemGrid;
	
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public UnCompeteItemListComposite(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(40);
		gridColumn_num.setTreeIndent(10);
		
		GridColumn gridColumn_2 = new GridColumn(checkItemGrid, SWT.NONE);
		gridColumn_2.setText("名称");
		gridColumn_2.setWidth(100);
//		gridColumn_2.setWordWrap(true);
		
		GridColumn gridColumn_opinion = new GridColumn(checkItemGrid, SWT.NONE);
		gridColumn_opinion.setText("检查结果");
		gridColumn_opinion.setWidth(100);
		gridColumn_opinion.setWordWrap(true);
		
		GridColumn gridColumn_3 = new GridColumn(checkItemGrid, SWT.NONE);
		gridColumn_3.setText("取费基数说明");
		gridColumn_3.setWidth(150);
		gridColumn_3.setWordWrap(true);
		
		GridColumn gridColumn_4 = new GridColumn(checkItemGrid, SWT.RIGHT);
		gridColumn_4.setText("计算基数");
		gridColumn_4.setWidth(100);
		
		GridColumn gridColumn_5 = new GridColumn(checkItemGrid, SWT.RIGHT);
		gridColumn_5.setText("报出金额");
		gridColumn_5.setWidth(90);
		
		GridColumn gridColumn_6 = new GridColumn(checkItemGrid, SWT.RIGHT);
		gridColumn_6.setText("报出费率%");
		gridColumn_6.setWidth(60);
		
		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);
	}
	
	/**
	 * @Description: 只显示示错误
	 * @author guoyr
	 * @param check
	 * @param showErrorOnly
	 */
	public void listErrorOnlyCheckDetail(IChecker check) {
		checkItemGrid.removeAll();
		List<UnCompleteCheckResult> checkResultList = check.getCheckResultErrorList();
		
		if (null != checkResultList && checkResultList.size() > 0) {
			int i = 1;
			for (UnCompleteCheckResult 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();
		List<UnCompleteCheckResult> checkResultList = check.getCheckResultDetailTreeList();
		
		if (null != checkResultList && checkResultList.size() > 0) {
			int i = 1;
			for (UnCompleteCheckResult checkResult : checkResultList) {
				GridItem constructItem = new GridItem(checkItemGrid, SWT.NONE);
				constructItem.setText(0, NumericChineseUtils.getChinese(i) + "");
				constructItem.setBackground(SWTResourceManager.getColor(70, 180, 180));
				constructItem.setColumnSpan(1, checkItemGrid.getColumnCount() - 2);
				constructItem.setText(1, checkResult.getName());
				initSubCheckResult(checkResult, constructItem, false);
				i++;
			}
		}
	}
	
	private void initSubCheckResult(UnCompleteCheckResult checkResult, GridItem parentItem, boolean getParentNo) {
		List<UnCompleteCheckResult> checkResultList = checkResult.getChildList();
		if (null != checkResultList && checkResultList.size() > 0) {
			parentItem.setExpanded(true);
			int index = 1;
			for (UnCompleteCheckResult 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, true);
			}
		}
		
	}
	
	/**
	 * @Description: 设置检查结果
	 * @author guoyr
	 * @param gridItem
	 * @param totalPrice
	 * @param sumPrice
	 * @param error
	 */
	private void setCheckResult(GridItem gridItem, UnCompleteCheckResult result) {
		gridItem.setText(1, ObjectUtils.getObjString(result.getName()));
		gridItem.setText(3, 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()));
		if (result.isReasonable()) {
//			gridItem.setText(2, "无错误");
			gridItem.setText(2, result.getOpinion());
			gridItem.setForeground(2, SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
		} else {
			gridItem.setText(2, ObjectUtils.getObjString(result.getOpinion()).replace("=", "≠"));
			gridItem.setForeground(2, SWTResourceManager.getColor(SWT.COLOR_RED));
		}
	}
	
	protected void checkSubclass() {}
}
