package nc.ui.wa.ruleex.action;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

import nc.bs.uif2.IActionCode;
import nc.hr.utils.ResHelper;
import nc.ui.hr.uif2.action.HrAction;
import nc.ui.pub.query.tools.ImageIconAccessor;
import nc.ui.uif2.AppEvent;
import nc.ui.uif2.UIState;
import nc.ui.uif2.actions.ActionInitializer;
import nc.ui.uif2.editor.IEditor;
import nc.ui.uif2.model.RowOperationInfo;
import nc.ui.wa.ruleex.model.WaRuleModel;
import nc.vo.bd.pub.NODE_TYPE;
import nc.vo.hrwa.waruleex.AggWageformExVO;
import nc.vo.hrwa.waruleex.WageformExVO;
import nc.vo.hrwa.waruleex.WageformdetExVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.VOStatus;
import nccloud.commons.lang.ArrayUtils;

/**
 * WaRuleCopyTableAction<BR>
 * <BR>
 *
 * @author: xuhw
 * @date: 2009-11-10
 * @since: eHR V6.0
 * @走查人:
 * @走查日期:
 * @修改人:
 * @修改日期:
 */
public class WaRuleCopyTableAction extends HrAction
{
	/**
	 * 薪资规则表单
	 */
	private IEditor editor;

	/**
	 * 复制表
	 */
	private final String COPY = "toolbar/icon/copy.gif";

	/**
	 * 初始化按钮
	 */
	public WaRuleCopyTableAction()
	{
		super();
		ActionInitializer.initializeAction(this, IActionCode.COPY);
		putValue(AbstractAction.SMALL_ICON, ImageIconAccessor.getIcon(COPY));
	}

	@Override
	public void doAction(ActionEvent e) throws Exception
	{
		AggWageformExVO temp = new AggWageformExVO();
		AggWageformExVO selectedAggVO = (AggWageformExVO) getModel().getSelectedData();
		if (selectedAggVO == null)
		{
//			MessageDialog.showHintDlg(getModel().getContext().getEntranceUI(), null, ResHelper.getString("60130payrule","060130payrule0000")/*@res "请先选中欲复制的薪资规则！"*/);
//			return;
			throw new BusinessException(ResHelper.getString("60130payrule","060130payrule0000")/*@res "请先选中欲复制的薪资规则！"*/);
		}
		if (selectedAggVO != null && editor != null)
		{
			// 设置当前态为复制表态
			((WaRuleModel) getModel()).setCopyStatus(true);
			getModel().setUiState(UIState.ADD);
			// 将复制后的聚合vo放置在editor上
			
			WageformExVO wageVo = (WageformExVO) selectedAggVO.getParentVO().clone();
			wageVo.setCreator(null);
			wageVo.setCreationtime(null);
			wageVo.setModifier(null);
			wageVo.setModifiedtime(null);
			//复制时清空编码和名称
			wageVo.setCode(null);
			wageVo.setName(null);
			wageVo.setName2(null);
			wageVo.setName3(null);
			wageVo.setName4(null);
			wageVo.setName5(null);
			wageVo.setName6(null);
				
			wageVo.setStatus(VOStatus.NEW);
			temp.setParentVO(wageVo);
			
			WageformdetExVO[] selectedChildVOs = (WageformdetExVO[])selectedAggVO.getChildrenVO();
			WageformdetExVO[] childVOs = null;
			if (!ArrayUtils.isEmpty(selectedChildVOs)) {
				childVOs = new WageformdetExVO[selectedChildVOs.length];
				for (int i = 0; i < childVOs.length; i++) {
					childVOs[i] = (WageformdetExVO)selectedChildVOs[i].clone();
					childVOs[i].setPk_wa_wageformex(null);
					childVOs[i].setPk_wa_wageformdetex(null);
					childVOs[i].setStatus(VOStatus.NEW);
				}
			}
			temp.setChildrenVO(childVOs);
			editor.setValue(temp);
		}
		
		getModel().fireEvent(new AppEvent(WaRuleModel.EVENT_COPY, this, new RowOperationInfo()));
	}

	@Override
	protected boolean isActionEnable()
	{
		NODE_TYPE node_type = getModel().getContext().getNodeType();
		if (node_type == NODE_TYPE.ORG_NODE && getModel().getContext().getPk_org() == null || (getModel().getSelectedData() == null))
		{
			return false;
		}

		return true;
	}

	public IEditor getEditor()
	{
		return editor;
	}

	public void setEditor(IEditor editor)
	{
		this.editor = editor;
	}
}