import AbsTable from '/js/libs/js-AbsTable.js'
import APaging from '/js/libs/js-APaging.js'
import AOverlay from '/js/libs/js-AOverlay.js'

export default class ATable extends AbsTable {
    constructor(e, pageSize = 5) {
        super(e);
        this.hForm = new FormData();
        this.absPaging = new APaging(e);
        this.aOverlay = new AOverlay(e);
        this.aOverlay.createOverlay();
        this.absPaging.pageSize = pageSize;
        this.absPaging.activePage = 1;
        this.absPaging.maxRow = -1;
        this.absPaging.on("PagingChange", ((e) => {
            this.SendData(false);
        }).bind(this));

    }
    LoadDataUrl(url, first = true) {
        this.url = url;
        this.aOverlay.showOverlay();
        this.SendData(first);
    }
    SendData(first) {
        var xhr = new XMLHttpRequest();
        xhr.open("POST", this.url);
        var f = function (evt) {
            if (evt.currentTarget.readyState == 4 && evt.currentTarget.status == 200) {
                if (evt.currentTarget.responseText) {
                    var o = JSON.parse(evt.currentTarget.responseText);
                    if (o.mrows != null) {
                        this.absPaging.SetPaging(o.mrows);
                    }
                    this.LoadData(o.data);
                    this.aOverlay.removeOverlay();
                }
            }
        }.bind(this);
        xhr.addEventListener("readystatechange", f, false);
        var frm = new FormData();
        AddFormData(frm, "isFirstQuery", first);
        AddFormData(frm, "pageSize", this.absPaging.pageSize);
        AddFormData(frm, "pageNumber", this.absPaging.activePage);
        AddFormData(frm, "maxRow", this.absPaging.maxRow);
        xhr.send(frm);
    }
    RefreshPage(page, firstQ = false) {
        this.absPaging.activePage = page;
        this.SendData(firstQ);
    }
    RefreshCurrentPage(firstQ = false) {
        this.SendData(firstQ);
    }
}
