package baseclass;

import p2.pao.PaoRemote;
import p2.pao.PaoSetRemote;
import p2.util.P2AppException;
import p2.util.P2Exception;
import p2.webclient.system.beans.DataBean;
import p2.webclient.system.controller.Utility;
import p2.webclient.system.controller.WebClientEvent;

import java.util.Arrays;

/**
 * 博远自定义的ByDataBean类，继承DataBean类
 *
 * @author Administrator
 */
public class DataBeanCust extends DataBean {

    @Override
    public int TOGGLEDELETEROW() throws P2Exception {
        super.TOGGLEDELETEROW();
        deleteAndClean();
        return 1;
    }

    @Override
    public int ADDROW() throws P2Exception {
        if (parent != null) {
            parent.save();
        }
        return super.ADDROW();
    }

    /**
     * 表体一次性删除方法。减少保存步骤。在TOGGLEDELETEROW()的super.TOGGLEDELETEROW()后调用。
     *
     * @throws P2Exception
     */
    public void deleteAndClean() throws P2Exception {
        if (getPaoSet().getSelection().size() > 0) {
            if (getParent() != null) {
                PaoRemote owner = getParent().getPao();
                PaoSetRemote set = getPaoSet();
                PaoRemote pao;
                if (owner.toBeAdded()) {
                    for (int i = 0; (pao = set.getPao(i)) != null; i++) {
                        if (pao.isSelected()) {
                            set.remove(i);
                            i--;
                        }
                    }
                }
                this.parent.save();
                updateRowNum();
            } else {
                this.save();
            }

        }
    }

    private void updateRowNum() throws P2Exception {
        getPaoSet().cleanup();
        if (Arrays.asList(getFields()).contains("FROWNUM") || Arrays.asList(getFields()).contains("frownum")) {
            int rows = getPaoSet().count();
            for (int i = 0; i < rows; i++) {
                getPaoSet().getPao(i).setValue("FROWNUM", i + 1, 11L);
            }
            this.save();
            this.parent.save();
        }
    }

    /**
     * 删除所有（清空）
     *
     * @return
     * @throws P2Exception
     */
    public int DELETEALL() throws P2Exception {
        getPaoSet().deleteAll();
        this.parent.save();
        byrefresh();
        return 1;
    }

    public boolean dialogIsNotOpen() throws P2Exception {
        return sessionContext.getCurrentEvent().getMessageReturn() == -1;
    }

    public void dialogOpen(String msg) throws P2Exception {
        // 弹出确认对话框
        throw new P2AppException("system", "comwarm", new String[]{msg});
    }

    public boolean dialogIsCheckOk() throws P2Exception {
        return sessionContext.getCurrentEvent().getMessageReturn() == 8;
    }

    public void byrefresh() throws P2Exception {
        this.reloadTable();
        this.refreshTable();
        this.sessionContext.queueRefreshEvent();
    }

    @Override
    public synchronized void selectAll() throws P2Exception {
        super.selectAll();
//        if (count() <= 100 || "TPRICEADJUSTDETAIL".equalsIgnoreCase(getPaoSet().getName())) {
//            super.selectAll();
//        } else {
//            int StartIndex = this.getPageStartIndex();// 开始行
//            boolean iselect = !this.isSelected(StartIndex);
//            int EndIndex = this.getPageRowCount();// 结束行
//            if (iselect) {
//                this.select(StartIndex, EndIndex);
//            } else {
//                this.unselect(StartIndex, EndIndex);
//            }
//            this.refreshTable();
//            sessionContext.queueRefreshEvent();
//        }
    }

    @Override
    public synchronized void unselectAll() throws P2Exception {
        super.unselectAll();
//        if (count() <= 100 || "TPRICEADJUSTDETAIL".equalsIgnoreCase(getPaoSet().getName())) {
//            super.unselectAll();
//        } else {
//            int StartIndex = this.getPageStartIndex();// 开始行
//            boolean iselect = !this.isSelected(StartIndex);
//            int EndIndex = this.getPageRowCount();// 结束行
//            if (iselect) {
//                this.select(StartIndex, EndIndex);
//            } else {
//                this.unselect(StartIndex, EndIndex);
//            }
//            this.refreshTable();
//            sessionContext.queueRefreshEvent();
//        }
    }

    public void closeApp(String closeApp) throws P2Exception {
        Utility.sendEvent(new WebClientEvent("closeappwin", app.getApp().toLowerCase(),
                closeApp, "", "", 0, sessionContext));
    }

    public void openApp(String openApp, long fbillid) throws P2Exception {
        closeApp(openApp);
        Utility.sendEvent(new WebClientEvent("opennewapp", app.getApp().toLowerCase(),
                openApp, "", "", fbillid, sessionContext));
    }

    /**
     * 打开进度条
     */
    public boolean runLongOp(String mathedname) throws P2Exception {
        return this.sessionContext.runLongOp(this, mathedname);
    }

    /**
     * 如果进度条执行成功
     */
    public boolean LongOpCompleted() throws P2Exception {

        return this.sessionContext.haslongOpCompleted();
    }
}
