package nc.ui.obm.payroll.ebankdfgz.action;

import java.awt.Event;
import java.awt.event.ActionEvent;
import java.util.Iterator;

import javax.swing.Action;
import javax.swing.KeyStroke;

import nc.bs.framework.common.NCLocator;
import nc.bs.pub.InitDataException;
import nc.desktop.ui.WorkbenchEnvironment;
import nc.funcnode.ui.FuncletInitData;
import nc.funcnode.ui.FuncletWindowLauncher;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.sfbase.client.ClientToolKit;
import nc.ui.pub.beans.MessageDialog;
import nc.ui.pub.linkoperate.ILinkType;
import nc.ui.pubapp.uif2app.query2.model.ModelDataManager;
import nc.ui.sm.power.FuncRegisterCacheAccessor;
import nc.ui.uif2.ToftPanelAdaptor;
import nc.ui.uif2.UIState;
import nc.ui.uif2.model.AbstractAppModel;
import nc.vo.obm.ebankpaylog.PaystaeEnum;
import nc.vo.obm.obmvo.PmtconfirmAddVO;
import nc.vo.obm.payroll.DfgzBVO;
import nc.vo.obm.payroll.DfgzBillVO;
import nc.vo.obm.payroll.DfgzHVO;
import nc.vo.obm.pmtconfirm.linkdata.PmtconfirmLinkAddData;
import nc.vo.obm.util.IObmConst;
import nc.vo.payroll.ml.MLPayroll;
import nc.vo.sm.funcreg.FuncRegisterVO;
import nc.vo.tmpub.util.ArrayUtil;
import nc.ui.pubapp.uif2app.view.ShowUpableBillListView;
/**
 *  
 *  状态确认
 * @author  yinyan
 * @version 1.0 2011-10-24
 * @since   NC6.1
 */
@SuppressWarnings("restriction")
public class LogStateConfirmAction extends nc.ui.uif2.NCAction {
	 
	private DfgzLogConfirmDlg conifrmDlg;
	/** serialVersionUID*/
	private static final long serialVersionUID = 1L;
	/**
	 * UI端数据模型
	 */
	private AbstractAppModel model = null;

	private ModelDataManager dataManager;
	
	private ShowUpableBillListView ListView;
	
	private nc.ui.pubapp.uif2app.view.ShowUpableBillForm editor;
	
	private IUAPQueryBS iuap = (IUAPQueryBS) NCLocator.getInstance().lookup(IUAPQueryBS.class.getName());
	public LogStateConfirmAction() {
		super();
		setCode("EbankLogCreatPmtConfirmAction");
		setBtnName(MLPayroll.getStr00063()/*"状态确认"*/);
		putValue(Action.SHORT_DESCRIPTION,getBtnName()+"(Ctrl+M)");
		putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke((int)'M', Event.CTRL_MASK));
	}
	@Override
	public void doAction(ActionEvent e) throws Exception {
		Object selectObj = getModel().getSelectedData();
		if (selectObj == null) {
			throw new Exception(MLPayroll.getStr00064()/*"下载状态前请先选择记录"*/);
		}
		DfgzBillVO[] selectvos = null;
		if (selectObj instanceof java.util.Arrays) {
			selectvos = (DfgzBillVO[]) selectObj;
		} else {
			selectvos = new DfgzBillVO[] { (DfgzBillVO) selectObj };
		}
		if (selectvos == null||selectvos.length > 1) {
			throw new Exception(MLPayroll.getStr00065()/*"选择一条工资单记录进行确认操作"*/);
		} 
		for(DfgzBillVO selectvo : selectvos){
			if(selectvo.getParentVO().getBillpaystate() != 0) {
				String sql = "select vdef1 from ebank_dfgz_h where vbillno ='"+selectvo.getParentVO().getVbillno()+"'and logflag = 0 and dr = 0";
				String vdef1 = (String) iuap.executeQuery(sql, new ColumnProcessor());
				if(vdef1 == null) {
					throw new Exception("支付状态不正确请先下载状态再确认!");
				}	
			}
			
		}
		
		doDfAction(selectvos[0]);
	}  
	/**
	 * 触发状态确认操作按钮,	弹出确认操作框.
	 */
//	private void doDfAction(DfgzBillVO selectAggLog) throws Exception {
//		String userid = model.getContext().getPk_loginUser();
//		// initialize dialog data
//		getConifrmDlg().initData(selectAggLog);
//		if (conifrmDlg.showModal() == conifrmDlg.ID_OK) {
//			DfgzBVO[] dlgvos = conifrmDlg.getDoConfirmedSubVOS();
//			HashMap<String, DefaultVO> chgchildvos = new HashMap<String, DefaultVO>();
//			Integer confirmFlag = null;
//			String confirmNotes = null;
//			if ((dlgvos != null) && (dlgvos.length > 0)) {
//				for (int j = 0; j < dlgvos.length; j++) {
//					confirmFlag = (Integer) dlgvos[j]
//							.getAttributeValue(conifrmDlg.KEY_CONFIRMTRANFLAG);
//					confirmNotes = MLString.getConfirmLogStateMsg(confirmFlag);
//					if (confirmFlag != null) {
//						DefaultVO updatevo = new DefaultVO();
//						updatevo.setAttributeValue(DefaultVO.CONFIRMSTATE,
//								confirmFlag);
//						updatevo.setAttributeValue(DfgzBVO.CONFIRMINFO,
//								confirmNotes);
//						updatevo.setAttributeValue(DefaultVO.PK_ID,
//								dlgvos[j].getAttributeValue(DfgzBVO.UID2));
//						chgchildvos.put(updatevo.getString(DefaultVO.PK_ID),
//								updatevo);
//					}
//				}
//				//IDfgzLogService logsev = (IDfgzLogService) NCLocator
//				//		.getInstance().lookup(IDfgzLogService.class.getName());
//				IBusilogService logsev = (IBusilogService) NCLocator
//				.getInstance().lookup(IBusilogService.class.getName());
//				logsev.confirmDfgzPayLogState(selectAggLog.getParentVO(),
//						chgchildvos, userid);
//				DfgzBillModel dmodel=(DfgzBillModel)this.getModel();
//				dmodel.updateUILogDataFromDB(selectAggLog);
//			}
//		}
//	}
	
	private void doDfAction(DfgzBillVO selectAggLog) throws Exception {
		DfgzHVO billvo=(DfgzHVO) selectAggLog.getParent();
        //打开窗口
		PmtconfirmAddVO addvo = new PmtconfirmAddVO();
		addvo.setPk_org(billvo.getPk_org());
		addvo.setPk_group(billvo.getPk_group());
		addvo.setSrcsystem(IObmConst.SYS_OBM);// 默认
		addvo.setSrcbillcode(billvo.getVbillno());
		addvo.setSrcbilltype("36C9");//
		addvo.setSrcnodecode("3610PAYR");
		addvo.setSrcpkid(billvo.getPrimaryKey());
		addvo.setYurref(billvo.getYurref());

		PmtconfirmLinkAddData data = new PmtconfirmLinkAddData();
		data.setUserObject(addvo);
		FuncRegisterVO funRegVo = WorkbenchEnvironment.getInstance().getFuncRegisterVO(IObmConst.NODECODE_PRIVATEPAYCONFIRM);
		if (funRegVo != null) {
			ToftPanelAdaptor adaptor = (ToftPanelAdaptor) getModel().getContext().getEntranceUI();
			FuncletWindowLauncher.openFuncNodeInTabbedPane(adaptor, funRegVo,
					new FuncletInitData(ILinkType.LINK_TYPE_ADD, data), null,false);
		} else {
			MessageDialog.showErrorDlg(ClientToolKit.getApplet(), nc.ui.ml.NCLangRes.getInstance().getStrByID("sysframev5", "UPPsysframev5-000062")/* @res "错误" */, nc.ui.ml.NCLangRes.getInstance().getStrByID("sysframev5", "UPPsysframev5-000095")/* @res "没有打开此节点的权限 . 节点号=" */
                    + IObmConst.NODECODE_PRIVATEPAYCONFIRM);
		}
	}
	
	/**
	 * 
	 * @param conifrmDlg
	 * @since NC6.0
	 */
	public void setConifrmDlg(DfgzLogConfirmDlg conifrmDlg) {
		this.conifrmDlg = conifrmDlg;
	}
	/**
	 * 
	 * 取得状态确认操作对话框.
	 * @return DfdkLogConfirmDlg 由spring框架初始化生成
	 * @throws InitDataException
	 * @since NC6.0
	 */
	public DfgzLogConfirmDlg getConifrmDlg(){
		if(conifrmDlg==null){
			conifrmDlg=new DfgzLogConfirmDlg(editor,MLPayroll.getStr00066()/*"工资单确认"*/);
		}
		return conifrmDlg;
	}
	@Override
	protected boolean isActionEnable() {
		DfgzBillVO selectedData = (DfgzBillVO)model.getSelectedData();
		if(selectedData != null){
			DfgzBVO[] vos = (DfgzBVO[]) selectedData.getAllChildrenVO();
			if(!ArrayUtil.isNull(vos)){
				for(DfgzBVO vo : vos){
					//没有支付状态即未支付的单据不可确认支付状态
					if(vo.getPaystate()==null)return false;
					//有状态不明的单据,则可以确认单据状态
					if(PaystaeEnum.PAY_UNKNOWN.toIntValue()== vo.getPaystate()){
						return true;
					}
				}
			}
			return false;//全部确认完毕的单据不能再次确认
		}else{
			return false;
		}
	}

	public AbstractAppModel getModel() {
		return model;
	}
	public void setModel(AbstractAppModel model) {
		this.model = model;
		getModel().addAppEventListener(this);
	}
	public ModelDataManager getDataManager() {
		return dataManager;
	}

	public void setDataManager(ModelDataManager dataManager) {
		this.dataManager = dataManager;
	}
	public nc.ui.pubapp.uif2app.view.ShowUpableBillForm getEditor() {
		return editor;
	}
	public void setEditor(nc.ui.pubapp.uif2app.view.ShowUpableBillForm editor) {
		this.editor = editor;
	}
	public ShowUpableBillListView getListView() {
		return ListView;
	}
	public void setListView(ShowUpableBillListView listView) {
		ListView = listView;
	}
	
	
}
