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

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.core.widgets.ELabel;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

import com.gx.obe.ColorConstants;
import com.gx.obe.business.vo.TaxFeesCheckResult;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.Constants;
import com.gx.obe.evaluation.step.checker.TaxFeesChecker;
import com.gx.obe.evaluation.step.inter.IBoqChecker;
import com.gx.obe.evaluation.step.inter.IChecker;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.util.utils.NumberFormatUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/** 
 * @Description: 税金费率检查
 * @author guoyr 
 */
public class TaxFeesListComposite extends Composite implements IBoqChecker{
	
	private Grid checkItemGrid;
	private GridColumn gridColumn_num;
	private Composite checkContentComposite;
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public TaxFeesListComposite(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_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);
		
		GridColumn gridColumn_2 = new GridColumn(checkItemGrid, SWT.NONE);
		gridColumn_2.setText("名称");
		gridColumn_2.setWidth(200);
		gridColumn_2.setWordWrap(true);
		
		GridColumn gridColumn_3 = new GridColumn(checkItemGrid, SWT.RIGHT);
		gridColumn_3.setText("税前工程造价");
		gridColumn_3.setWidth(60);
		
		GridColumn gridColumn_4 = new GridColumn(checkItemGrid, SWT.RIGHT);
		gridColumn_4.setText("进项税额");
		gridColumn_4.setWidth(60);
		
		GridColumn gridColumn_equipmentFee = new GridColumn(checkItemGrid, SWT.RIGHT);
		gridColumn_equipmentFee.setText("设备费");
		gridColumn_equipmentFee.setWidth(60);
		
		GridColumn gridColumn_selfSupplyFee = new GridColumn(checkItemGrid, SWT.RIGHT);
		gridColumn_selfSupplyFee.setText("甲供材料设备费");
		gridColumn_selfSupplyFee.setWidth(80);
		
		GridColumn gridColumn_5 = new GridColumn(checkItemGrid, SWT.RIGHT);
		gridColumn_5.setText("销项税额");
		gridColumn_5.setWidth(60);
		
		GridColumn gridColumn_9 = new GridColumn(checkItemGrid, SWT.CENTER);
		gridColumn_9.setText("销项税额费率");
		gridColumn_9.setWidth(60);
		gridColumn_9.setWordWrap(true);
		
		GridColumn gridColumn_6 = new GridColumn(checkItemGrid, SWT.RIGHT);
		gridColumn_6.setText("增值税应纳税额");
		gridColumn_6.setWidth(70);
		
		GridColumn gridColumn_10 = new GridColumn(checkItemGrid, SWT.CENTER);
		gridColumn_10.setText("附加税费费率");
		gridColumn_10.setWidth(60);
		
		GridColumn gridColumn_7 = new GridColumn(checkItemGrid, SWT.RIGHT);
		gridColumn_7.setText("附加税费");
		gridColumn_7.setWidth(60);
		gridColumn_7.setWordWrap(true);
		
		GridColumn gridColumn_11 = new GridColumn(checkItemGrid, SWT.RIGHT);
		gridColumn_11.setText("税金");
		gridColumn_11.setWidth(60);
		gridColumn_11.setWordWrap(true);
		
		GridColumn gridColumn_opinion = new GridColumn(checkItemGrid, SWT.CENTER);
		gridColumn_opinion.setText("提示信息");
		gridColumn_opinion.setWidth(100);
		gridColumn_opinion.setWordWrap(true);
		
		ESepator top_sep = new ESepator(this, SWT.SEPARATOR | SWT.HORIZONTAL);
		top_sep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		top_sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		checkContentComposite = new Composite(this, SWT.NONE);
		checkContentComposite.setBackground(SWTResourceManager.getColor(252, 252, 238));
		checkContentComposite.setBackgroundMode(SWT.INHERIT_FORCE);
		checkContentComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		checkContentComposite.setLayout(new GridLayout(1, false));
		
		CLabel label = new CLabel(checkContentComposite, SWT.NONE);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		label.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/open.png"));
		label.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		label.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		label.setText("说明");
		
		
		init();
	}


	/**
	 * @Description:  初始化界面
	 * @author guoyr
	 */
	private void init(){
		checkItemGrid.removeAll();
		checkItemGrid.setItemHeight(Constants.ROW_HIGHT);
		checkItemGrid.setAutoHeight(true);
		addCheckItem("其中“销项税额费率”、“附加税费”和“税金”第一行数值为投标人实际数值 、第二行数数值为系统校验计算值。");
	}
	

	/**
	 * @Description:  只显示示错误
	 * @author guoyr
	 * @param check
	 * @param showErrorOnly
	 */
	public void listErrorOnlyCheckDetail(IChecker check){
		checkItemGrid.removeAll();
		List<TaxFeesCheckResult> checkResultList = check.getCheckResultErrorList();
		
		if(null != checkResultList && checkResultList.size() > 0){
			int i = 1;
			for(TaxFeesCheckResult checkResult : checkResultList){
				GridItem constructItem = new GridItem(checkItemGrid, SWT.NONE);
				setCheckResult(constructItem, checkResult);
				constructItem.setText(0, (i++)+"");
			}
		}
		gridColumn_num.setAlignment(SWT.CENTER);
		gridColumn_num.setTree(false);
	}
	
	/**
	 * @Description:  进行检查
	 * @author guoyr
	 */
	public void listCheckDetail(IChecker check){
		checkItemGrid.removeAll();
		List<TaxFeesCheckResult> checkResultList = check.getCheckResultDetailTreeList();
		boolean hassTree = false;
		if(null != checkResultList && checkResultList.size() > 0){
			int i = 1;
			for(TaxFeesCheckResult checkResult : checkResultList){
				if(null == checkResult.getChildList() ||checkResult.getChildList().size() > 0){
					hassTree = true;
					GridItem constructItem = new GridItem(checkItemGrid, SWT.NONE);
					constructItem.setText(0, "单项工程");
					constructItem.setText(1, ObjectUtils.getObjString(checkResult.getName()));
//				constructItem.setText(0, NumericChineseUtils.getChinese(i)+"");
					constructItem.setBackground(SWTResourceManager.getColor(70, 180, 180));
//					constructItem.setColumnSpan(1, checkItemGrid.getColumnCount() -2);
					setCheckResult(constructItem, checkResult);
					initSubCheckResult(checkResult, constructItem, false);
				}else {
					GridItem gridItem = new GridItem(checkItemGrid, SWT.NONE);
					gridItem.setText(0, (i)+"");
//						gridItem.setText(0, "单位工程");
					setCheckResult(gridItem, checkResult);
				}
				i ++;
			}
		}
		if(!hassTree){
			gridColumn_num.setAlignment(SWT.CENTER);
			gridColumn_num.setWidth(60);
			gridColumn_num.setMaxmumWidth(60);
		}
		gridColumn_num.setTree(hassTree);
	}
	
	private void initSubCheckResult(TaxFeesCheckResult checkResult, GridItem parentItem, boolean getParentNo){
		List<TaxFeesCheckResult> checkResultList = checkResult.getChildList();
		if(null != checkResultList && checkResultList.size() > 0){
			parentItem.setExpanded(true);
			int index = 1;
			for(TaxFeesCheckResult result : checkResultList){
				GridItem gridItem = new GridItem(parentItem, SWT.NONE);
				gridItem.setText(0, (index++)+"");
//				gridItem.setText(0, "单位工程");
				setCheckResult(gridItem, result);
			}
		}
		
	}
	
	/**
	 * @Description:  设置检查结果
	 * @author guoyr
	 * @param gridItem
	 * @param totalPrice
	 * @param sumPrice
	 * @param error
	 */
	private void setCheckResult(GridItem gridItem, TaxFeesCheckResult result){
		gridItem.setText(1, ObjectUtils.getObjString(result.getName()));
		gridItem.setText(2, NumberFormatUtils.format(result.getPreTaxCost()));
		gridItem.setText(3, NumberFormatUtils.format(result.getInputTax()));
		gridItem.setText(4, NumberFormatUtils.format(result.getEquipmentFee()));
		gridItem.setText(5, NumberFormatUtils.format(result.getSelfSupplyFee()));
		gridItem.setText(6, NumberFormatUtils.format(result.getOutputTax()));
		BigDecimal outputTaxRate = result.getOutputTaxRate();// （销项税额费率  税前工程造价-进项税额）*9%=销项税额费
		if(null != outputTaxRate && outputTaxRate.compareTo(BigDecimal.ZERO) > 0){
			gridItem.setText(7, "9%\r\n"+NumberFormatUtils.format(result.getOutputTaxRate(), 5)+"%");
			if(BigDecimalUtils.difference(outputTaxRate, TaxFeesChecker.OUTPUTTAX_RATE).compareTo(TaxFeesChecker.ERROR_RATE) >= 0){
				gridItem.setForeground(7,SWTResourceManager.getColor(SWT.COLOR_RED));
				gridItem.setForeground(12, SWTResourceManager.getColor(SWT.COLOR_RED));
			}
		}else {
			gridItem.setText(7, "9%");
		}
		gridItem.setText(8, NumberFormatUtils.format(result.getValueAddedTax()));//增值税应纳税额
		gridItem.setText(9, NumberFormatUtils.format(result.getSurchargeTaxRate()) +"%");//附加税费费率直接读取
		
		gridItem.setText(10, NumberFormatUtils.format(result.getSurchargeTax()) +"\r\n"+NumberFormatUtils.format(result.getCalculateSurchargeTax()));//附加税费
		gridItem.setText(11, NumberFormatUtils.format(result.getTaxes()) +"\r\n"+NumberFormatUtils.format(result.getCalculateTaxes()));//
		
		if(BigDecimalUtils.difference(result.getTaxes(), result.getCalculateTaxes()).compareTo(TaxFeesChecker.ERROR_PRICE) > 0){
			gridItem.setForeground(9, SWTResourceManager.getColor(SWT.COLOR_RED));
			gridItem.setForeground(10, SWTResourceManager.getColor(SWT.COLOR_RED));
			gridItem.setForeground(11, SWTResourceManager.getColor(SWT.COLOR_RED));
			gridItem.setForeground(12, SWTResourceManager.getColor(SWT.COLOR_RED));
		}
		gridItem.setText(12, ObjectUtils.getObjString(result.getOpinion()));
		
	}
	
	/**
	 * @Description: 设置检查内容
	 * @author guoyr
	 * @param checkContent 
	 */
	private void addCheckItem(String checkContent){
		ELabel label = new ELabel(checkContentComposite, SWT.WRAP);
		label.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		label.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		label.setLeftMargin(30);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		label.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/point.png"));
		label.setText(checkContent);
	}
	protected void checkSubclass() {
	}
}
