package nc.ui.obm.ebankpaylog.actions;

import java.awt.event.ActionEvent;
import javax.swing.KeyStroke;
import nc.bs.framework.common.NCLocator;
import nc.bs.framework.exception.InitException;
import nc.desktop.ui.WorkbenchEnvironment;
import nc.funcnode.ui.FuncletInitData;
import nc.funcnode.ui.FuncletLinkListener;
import nc.funcnode.ui.FuncletWindowLauncher;
import nc.itf.obm.PayStateVO;
import nc.itf.obm.ebanklog.IEbankLogManageService;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.pubitf.obm.IObmLogManageService;
import nc.sfbase.client.ClientToolKit;
import nc.ui.ml.NCLangRes;
import nc.ui.obm.ebankpaylog.model.PayLogBillManageModel;
import nc.ui.obm.ebankpaylog.model.PayLogBillModelManager;
import nc.ui.obm.ebankpaylog.view.DfLogConfirmDlg;
import nc.ui.obm.ebankpaylog.view.PayLogListView;
import nc.ui.pub.beans.MessageDialog;
import nc.ui.pub.bill.BillListPanel;
import nc.ui.uif2.NCAction;
import nc.ui.uif2.ToftPanelAdaptor;
import nc.ui.uif2.UIState;
import nc.vo.obm.ebankpaylog.EBankPayLogAggVO;
import nc.vo.obm.ebankpaylog.EBankPayLogHVO;
import nc.vo.obm.ebankpaylog.EBankPayLogVO;
import nc.vo.obm.ebankpaylog.FuncEnum;
import nc.vo.obm.ml.MLObm;
import nc.vo.obm.ml.MLString;
import nc.vo.obm.obmvo.PmtconfirmAddVO;
import nc.vo.obm.pay.LogMgrRequestVO;
import nc.vo.obm.pmtconfirm.linkdata.PmtconfirmLinkAddData;
import nc.vo.sm.funcreg.FuncRegisterVO;

public class LogStateConfirmAction extends NCAction {
	private DfLogConfirmDlg conifrmDlg;
	private static final long serialVersionUID = 1L;
	private PayLogBillModelManager dataManager;
	private PayLogBillManageModel model;
	private PayLogListView listview;
	boolean openWindFlag = false;
	private IUAPQueryBS iuap = (IUAPQueryBS) NCLocator.getInstance().lookup(IUAPQueryBS.class.getName());
	
	public LogStateConfirmAction() {
		this.setCode("EbankLogCreatPmtConfirmAction");
		this.setBtnName(MLObm.getStr00129());
		this.putValue("ShortDescription", this.getBtnName() + "(Ctrl+M)");
		this.putValue("AcceleratorKey", KeyStroke.getKeyStroke(77, 2));
	}

	public void doAction(ActionEvent e) throws Exception {
		Object[] selectvos = this.getModel().getSelectedOperaDatas();
		if (selectvos == null) {
			throw new Exception(MLObm.getStr00130());
		} else if (selectvos.length > 1) {
			throw new Exception(MLObm.getStr00131());
		} else {
			EBankPayLogAggVO aggvo = (EBankPayLogAggVO) selectvos[0];
			EBankPayLogHVO parentVO = (EBankPayLogHVO) aggvo.getParentVO();
			EBankPayLogVO reallogVO = (EBankPayLogVO) aggvo.getChildrenVO()[0];
			if(reallogVO.getPaystate() != 0) {
				String sql = "select obmdef1 from ebank_paylog_h where pk_ebank_paylog_h = '"+parentVO.getPk_ebank_paylog_h()+"' and dr = 0";
				String vdef1 = (String) iuap.executeQuery(sql, new ColumnProcessor());
				if(vdef1 == null) {
					throw new Exception("支付状态不正确请先下载状态再确认!");
				}
			}
			if (reallogVO != null && !this.openWindFlag) {
				try {
					this.openWindFlag = true;
					this.doConfirmBillAction(parentVO, reallogVO);
					Thread.sleep(1000L);
				} catch (Exception var10) {
					throw var10;
				} finally {
					this.openWindFlag = false;
				}
			}

		}
	}

	void doConfirmBillAction(EBankPayLogHVO parentVO, EBankPayLogVO selectBodyVO) throws Exception {
		BillListPanel billlistcard = this.listview.getBillListPanel();
		if (!FuncEnum.PROVIDE_DEDUCT.toStringValue().equals(selectBodyVO.getFunc())
				&& (billlistcard.getBodyTable().getSelectedRowCount() == 0
						|| billlistcard.getBodyTable().getSelectedRowCount() != 1)) {
			throw new Exception(MLObm.getStr00132());
		} else {
			int selectRow = billlistcard.getBodyTable().getSelectedRow();
			EBankPayLogVO selectvo = null;
			if (selectRow == -1) {
				selectvo = selectBodyVO;
			} else {
				selectvo = (EBankPayLogVO) billlistcard.getBodyBillModel().getBodyValueRowVO(selectRow,
						EBankPayLogVO.class.getName());
			}

			String yurref = selectvo.getYurref();
			LogMgrRequestVO request = LogMgrRequestVO.createConfirmRequest(yurref);
			IObmLogManageService logsev = (IObmLogManageService) NCLocator.getInstance()
					.lookup(IObmLogManageService.class.getName());
			logsev.checkCanGenNewConfrimBill(request);
			PmtconfirmAddVO addvo = new PmtconfirmAddVO();
			String funcode = selectBodyVO.getFunc();
			if (!FuncEnum.PROVIDE_DEDUCT.toStringValue().equals(funcode)) {
				addvo.setYurref(selectvo.getYurref());
			} else {
				addvo.setYurref(selectBodyVO.getYurref());
			}

			addvo.setSrcnodecode("36100PZCQ");
			if (parentVO.getSrcsystem() != null) {
				addvo.setSrcsystem(parentVO.getSrcsystem());
			} else {
				addvo.setSrcsystem("OBM");
			}

			if (parentVO.getSrcbilltype() != null) {
				addvo.setSrcbilltype(parentVO.getSrcbilltype());
				addvo.setSrcbillcode(parentVO.getSrcbillcode());
			} else {
				addvo.setSrcbilltype("36C3");
				addvo.setSrcbillcode(parentVO.getHeadpackageid());
			}

			PmtconfirmLinkAddData data = new PmtconfirmLinkAddData();
			data.setUserObject(addvo);
			FuncRegisterVO funRegVo = WorkbenchEnvironment.getInstance().getFuncRegisterVO("36100CONFM");
			if (funRegVo != null) {
				ToftPanelAdaptor adaptor = (ToftPanelAdaptor) this.getModel().getContext().getEntranceUI();
				FuncletWindowLauncher.openFuncNodeInTabbedPane(adaptor, funRegVo, new FuncletInitData(0, data),
						(FuncletLinkListener) null, false);
			} else {
				MessageDialog.showErrorDlg(ClientToolKit.getApplet(),
						NCLangRes.getInstance().getStrByID("sysframev5", "UPPsysframev5-000062"), MLObm.getStr00912());
			}

		}
	}

	public void doDfAction(EBankPayLogAggVO selectAggLog) throws Exception {
		String userid = this.model.getContext().getPk_loginUser();
		this.getConifrmDlg().initData(selectAggLog);
		int var10000 = this.getConifrmDlg().showModal();
		DfLogConfirmDlg var10001 = this.conifrmDlg;
		if (var10000 == 1) {
			EBankPayLogVO[] difvos = this.conifrmDlg.getDoConfirmedSubVOS();
			if (difvos != null && difvos.length > 0) {
				PayStateVO[] statevos = null;
				if (difvos != null && difvos.length > 0) {
					statevos = new PayStateVO[difvos.length];

					for (int i = 0; i < difvos.length; ++i) {
						statevos[i] = new PayStateVO();
						String yurref = difvos[i].getYurref();
						statevos[i].setYurref(yurref);
						EBankPayLogVO var9 = difvos[i];
						var10001 = this.conifrmDlg;
						Integer istate = (Integer) var9.getAttributeValue("confrimtranflag");
						statevos[i].setPk_user(userid);
						statevos[i].setPayState(istate);
						statevos[i].setMsg(MLString.getConfirmLogStateMsg(istate));
						statevos[i].setExtendAttributeValue(PayStateVO.EXTKEY_BEFORE_TS, difvos[i].getTs());
						statevos[i].setExtendAttributeValue(PayStateVO.EXTKEY_BEFORE_STATE, difvos[i].getPaystate());
						statevos[i].setAction("zfcx");
						statevos[i].setBizDate(WorkbenchEnvironment.getInstance().getBusiDate());
					}
				}

				IEbankLogManageService logsev = (IEbankLogManageService) NCLocator.getInstance()
						.lookup(IEbankLogManageService.class.getName());
				logsev.confirmDfLogState(statevos, userid);
				this.model.updateUILogDataFromDB(new EBankPayLogAggVO[]{selectAggLog});
			}
		}

	}

	public void setConifrmDlg(DfLogConfirmDlg conifrmDlg) {
		this.conifrmDlg = conifrmDlg;
	}

	public DfLogConfirmDlg getConifrmDlg() {
		if (this.conifrmDlg == null) {
			throw new InitException(MLObm.getStr00133());
		} else {
			return this.conifrmDlg;
		}
	}

	protected boolean isActionEnable() {
		return this.model.getUiState() == UIState.NOT_EDIT && this.model.getSelectedData() != null;
	}

	public PayLogBillManageModel getModel() {
		return this.model;
	}

	public void setModel(PayLogBillManageModel model) {
		this.model = model;
		this.getModel().addAppEventListener(this);
	}

	public PayLogBillModelManager getDataManager() {
		return this.dataManager;
	}

	public void setDataManager(PayLogBillModelManager dataManager) {
		this.dataManager = dataManager;
	}

	public PayLogListView getListview() {
		return this.listview;
	}

	public void setListview(PayLogListView listview) {
		this.listview = listview;
	}
}