package nc.ui.cmp.settlement.actions;

import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.Action;

import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.cmp.settlement.validate.SettleValidate;
import nc.cmp.utils.CmpUtils;
import nc.cmp.utils.SettleUtils;
import nc.funcnode.ui.action.INCAction;
import nc.itf.cmp.ebank.ICMPPaymentService;
import nc.itf.cmp.pub.CmpSelfDefButtonNameConst;
import nc.itf.cmp.settlement.ISettlementQueryService;
import nc.ui.cmp.netpayment.PaymentProc;
import nc.ui.pub.beans.MessageDialog;
import nc.ui.pub.beans.UIDialog;
import nc.ui.uif2.ShowStatusBarMsgUtil;
import nc.vo.cmp.BusiStatus;
import nc.vo.cmp.SettleStatus;
import nc.vo.cmp.settlement.SettlementAggVO;
import nc.vo.cmp.settlement.SettlementBodyVO;
import nc.vo.cmp.settlement.SettlementHeadVO;
import nc.vo.cmp.settlement.international.util.SettleInternationalValidate;
import nc.vo.cmp.validate.CMPValidate;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDouble;

/**
 * 合并支付
 *
 * @author wuzhwa
 * @since v6.0
 *
 */
public class SettleCombinPayAction extends SettleDefaultAction {

	private static final long serialVersionUID = -4257102182028309094L;

	public SettleCombinPayAction() {

		setBtnName(CmpSelfDefButtonNameConst.getInstance().getConstBtnnameCombinpayName());
		putValue(Action.SHORT_DESCRIPTION, getBtnName());
		putValue(INCAction.CODE, "合并支付");/*-=notranslate=-*/

	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * nc.ui.cmp.settlement.actions.SettleDefaultAction#doAction(java.awt.event
	 * .ActionEvent)
	 *
	 * @see V57 void
	 * nc.ui.cmp.settlement.SettlementCardEventHandler.onBoCombinPay()
	 */
	@Override
	public void doAction(ActionEvent e) throws Exception {
		SettlementAggVO[] aggvos = null;
		try {
			Object obj = getValue();
			if (obj.getClass().isArray()) {
				aggvos = (SettlementAggVO[]) obj;
			} else {
				aggvos = new SettlementAggVO[] { (SettlementAggVO) obj };
			}
			StringBuffer error = new StringBuffer("业务单据编号:");
			for (SettlementAggVO settlementAggVO : aggvos) {
				//表头原币金额
				UFDouble headPrimal = (UFDouble) settlementAggVO.getParentVO().getAttributeValue("primal");
				//表体原币金额
				UFDouble bodyPrimal = new UFDouble(0.00);
				SettlementBodyVO [] settlementBodyVOs = (SettlementBodyVO[]) settlementAggVO.getChildrenVO();
				for (SettlementBodyVO settlementBodyVO : settlementBodyVOs) {
					bodyPrimal = bodyPrimal.add(settlementBodyVO.getPay_last());
				}
				if(headPrimal.compareTo(bodyPrimal) != 0) {
					String billcode = (String) settlementAggVO.getParentVO().getAttributeValue("billcode");
					error.append(billcode);
					error.append(",");
				}
			}
			if(error.length() != 7) {
				throw new BusinessException(error+"表体合计金额不等于表头金额!");
			}
			List<SettlementAggVO> aggLst = SettleUtils.filterSettleInfo4NetSettleFlagUnSettle(aggvos);
			if(aggLst.size()==0){
				ShowStatusBarMsgUtil.showErrorMsg(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("3607set1_0","03607set1-0078")/*@res "提示"*/,nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("3607set1_0","03607set1-0079")/*@res "没有可网银支付的表体记录"*/, getModel().getContext());
				return;
			}
			aggvos = aggLst.toArray(new SettlementAggVO[] {});
			NCLocator.getInstance().lookup(ICMPPaymentService.class).validateCombinPay(aggvos);
			this.getEdit().getClientSigner().checkIfCASign();
//			this.getEdit().getClientSigner().identifyCA();

			int i = MessageDialog.showYesNoDlg(getEdit(), nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID(
					"3607set_0", "03607set-0029")/* @res "提示" */, nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID(
					"3607set_0", "03607set-0050")/* @res "确定进行网上支付？" */);
			if (i != UIDialog.ID_YES) {
				return;
			}
			// 换用新的签名处理，在paymentproc里面,原因是签名也会弹出密码框，重复弹出了，更改实现方式。
			Integer doZhifu = new PaymentProc().doZhifu(getEdit(), aggvos);
			if(doZhifu == -1){
				return;
			}
			List<String> pkLst = CmpUtils.makeList();
			for (SettlementAggVO aggvo : aggvos) {
				pkLst.add(aggvo.getParentVO().getPrimaryKey());
			}
			aggvos = NCLocator.getInstance().lookup(ISettlementQueryService.class).querySettlementAggVOsByPks(pkLst.toArray(new String[]{}));

			if(!this.isListSelected()){
				this.getEdit().setLoadBean(aggvos[0]);
			}
			getModel().directlyUpdate(aggvos);
			this.getEdit().setValue(aggvos[0]);


			ShowStatusBarMsgUtil.showStatusBarMsg(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("3607set_0",
					"03607set-0051")/*
									 * @res "发送网银成功"
									 */, getModel().getContext());
		} catch (BusinessException be) {
			Logger.error(be.getMessage(), be);
			throw new BusinessException(be.getMessage());

		}

	}





	@Override
	protected boolean isActionEnable() {
		SettlementAggVO[] aggvos = getSelectedAggVOs();
		if (aggvos == null || aggvos.length == 0 || aggvos[0] == null) {
			return false;
		}
		for (SettlementAggVO settlementAggVO : aggvos) {
			SettlementAggVO currentAggvo = settlementAggVO;
			SettlementHeadVO head = (SettlementHeadVO) currentAggvo.getParentVO();
			// 收款类不能 支付
			if (nc.vo.cmp.settlement.SettleEnumCollection.Direction.REC.VALUE.equals(head.getDirection())) {
				return false;
			}
			if (head.getBusistatus() == null || !head.getBusistatus().equals(BusiStatus.Sign.getBillStatusKind())) {
				// 非签字态的不能网上支付
				return false;
			}
//			if (head.getSettlestatus() != null
//					&& (head.getSettlestatus().equals(SettleStatus.PAYING.getStatus())
//							|| head.getSettlestatus().equals(SettleStatus.CHANGEING.getStatus())
//							|| head.getSettlestatus().equals(SettleStatus.RECEIVING.getStatus())
//							|| head.getSettlestatus().equals(SettleStatus.SUCCESSPART.getStatus()) || head
//							.getSettlestatus().equals(SettleStatus.SUCCESSSETTLE.getStatus()))) {
//				/**
//				 * 支付中，划账中、已经结算的不能支付
//				 */
//				return false;
//			}

			// 付款结算工资发放单据不能合并支付
			if ("DS".equals(head.getPk_tradetype())) {
				return false;
			}
			if (head.getPk_ftsbill() != null) {
				// 资金组织生成的结算信息手工结算
				return false;
			}
			if (SettleUtils.isExistInnerAccount(currentAggvo)) {
				// 有内部账户的不能手工结算
				return false;
			}
			for (SettlementBodyVO body : (SettlementBodyVO[])settlementAggVO.getChildrenVO()) {
				if(body.getSettlestatus()!=null && body.getSettlestatus().equals(SettleStatus.SETTLERESET.getStatus())){
					// 含有结算红冲的信息，不能反操作
					return false;
				}
			}
		}

		return super.isActionEnable();
	}
}