package nc.ui.pubapp.uif2app.actions;

import java.awt.event.ActionEvent;

import nc.bs.uif2.IActionCode;
import nc.ui.pub.bill.action.BillTableLineAction;
import nc.ui.pubapp.uif2app.actions.intf.ICardPanelDefaultActionProcessor;
import nc.ui.pubapp.uif2app.event.card.BodyRowEditType;
import nc.ui.pubapp.uif2app.event.card.CardBodyAfterRowEditEvent;
import nc.ui.uif2.NCAction;
import nc.ui.uif2.ShowStatusBarMsgUtil;
import nc.ui.uif2.actions.ActionInitializer;

public class BodyCopyLineAction extends AbstractBodyTableExtendAction implements
    ICardPanelDefaultActionProcessor {
  private static final long serialVersionUID = 1L;

  public BodyCopyLineAction() {
    this.setCode(IActionCode.COPYLINE);
    NCAction action = new NCAction() {
      private static final long serialVersionUID = 1L;

      @Override
      public void doAction(ActionEvent e) throws Exception {
        // do nothing
      }
    };
    ActionInitializer.initializeAction(action, IActionCode.COPYLINE);
    this.putActionValue(action);
  }

  @Override
  public void doAction() {
    boolean doOperateFlag =
        this.getCardPanel().doLineAction(
            super.getCardPanel().getCurrentBodyTableCode(),
            BillTableLineAction.COPYLINE);
    if (doOperateFlag) {
      this.fireEvent();
    }
  }
  
  private void fireEvent() {
    int[] rows = this.getCardPanel().getBodyPanel().getTable().getSelectedRows();
    this.getModel().fireEvent(new CardBodyAfterRowEditEvent(this.getCardPanel(),BodyRowEditType.COPYLINE, rows));
  }

  @Override
  public int getType() {
    return BillTableLineAction.COPYLINE;
  }

  @Override
  protected boolean doBeforeAction(ActionEvent e) {
	int[] rows = this.getCardPanel().getBodyPanel().getTable().getSelectedRows();
	String name = this.getCardPanel().getBodyPanel().getName();
	if(name != null && "采购发票明细".equals(name)) {
		for (int i = 0; i < rows.length; i++) {
			String vbdef20 = (String) this.getCardPanel().getBodyValueAt(rows[i], "vbdef20");
			if(vbdef20 != null) {
			ShowStatusBarMsgUtil.showStatusBarMsg("发票参照的行不允许复制!", getModel().getContext());
		      return false;
			}
		}
	}
    if (this.getCurrentSelectIndex() == -1) {
      String message = nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("pubapp_0","0pubapp-0110")/*@res "请选择要复制的单据行"*/;
      ShowStatusBarMsgUtil.showStatusBarMsg(message, getModel().getContext());
      return false;
    }
    return true;
  }

}