import AButton from '/js/libs/js-AButton.js';
import AModal from '/js/libs/js-AModal.js';
import ATable from '/js/libs/js-ATable.js';
import ATab from '/js/libs/js-ATab.js';
window.L6102 = function () {
    var asyncStyleSheets = [
        '/css/atg-ui/table.css'
    ];
    window.app.loadCSS(asyncStyleSheets);


    Waves.attach('.btn-effect', ['waves-float']);
    Waves.init({ duration: 1000, delay: 200 });
    var btnAdd = document.getElementById("btAdd");

    var tabs = new ATab(document.querySelector(".atabs"), document.querySelector(".tabcontents"));
    var tb = new ATable(document.querySelector(".c-table"), 10);
    var btUpdate = new AButton(document.getElementById("btUpdate"));
    var btn = new AButton(btnAdd);
    var m = new AModal(null);
    var mU = new AModal(null);
    var btBack = document.getElementById("btBack");
    btBack.addEventListener("click", (e) => {
        tabs.selectedTab(0);
    })
    let promise = new Promise(function (resolve, reject) {
        tb.AddHeader("ID", "120px", "", "IdTypeStorageServer");
        tb.AddHeader("Storage Type Name", "", "240px", "TypeName");
        tb.AddHeader("Action", "180px", "", "Action", function (id, obj) {
            var ac = document.createElement("div");
            ac.classList.add("d-f", "j-c-center");
            var d = document.createElement("button");
            d.classList.add("btn", "btn-warning", "mr-2");
            var ico = document.createElement("span");
            ico.classList.add("atg", "atg-pencil");
            d.appendChild(ico);
            d.addEventListener("click", ((e) => { updateClick.call(tabs, e, id, obj); }));
            ac.appendChild(d);
            d = document.createElement("button");
            d.classList.add("btn", "btn-danger");
            var ico = document.createElement("span");
            ico.classList.add("atg", "atg-x");
            d.appendChild(ico);
            ac.appendChild(d);
            return ac;
        });
        tb.SetScrollBarY(300);
        tb.IsCheckBox(true);
        tb.LoadDataUrl("/Storage/GetTypeStorage");
    });

    m.overlay.setIndex(2101);
    m.createModal("OK");
    m.on("OK", () => {
        btn.RemoveLoading(btnAdd);
        tb.RefreshPage(1, true);
    });
    m.on("Close", () => { btn.RemoveLoading(btnAdd); });
    btn.on("click_btAdd", function (e) {
        const xhr = new XMLHttpRequest();
        xhr.open("POST", "/Storage/AddType");
        xhr.setRequestHeader('Content-type', 'application/json; charset=UTF-8');
        var data = {
            "Name": document.getElementById("inpType").value
        };
        var f = function (ev) {
            if (ev.currentTarget.readyState == 4) {
                if (ev.currentTarget.status == 200) {
                    var obj = JSON.parse(ev.currentTarget.responseText);
                    if (ev.Status == 0) {
                        m.AModalOK.title("Message Error", "mess-error");
                        m.AModalOK.message(ev.Message);
                    } else {
                        m.AModalOK.title("Message", "mess-success");
                        m.AModalOK.message("Row Insert Success!");
                    }
                    m.showModal();
                }
            }

        };
        xhr.addEventListener("readystatechange", f, false);
        xhr.send(JSON.stringify(data));
        m.showModal();
    });

    tabs.lockTabs.push(1);

    mU.overlay.setIndex(2101);
    mU.createModal("OK");
    mU.on("OK", () => {
        btUpdate.RemoveLoading(document.getElementById("btUpdate"));
        tabs.selectedTab(0);
        tb.RefreshCurrentPage();
    });
    mU.on("Close", () => { btn.RemoveLoading(document.getElementById("btUpdate")); });

    btUpdate.on("click_btUpdate", (evt) => {
        var inp = document.getElementById("inpEType");
        var inpID = document.getElementById("inpEID");
        if (inpID.value != "") {
            const xhr = new XMLHttpRequest();
            xhr.open("POST", "/Storage/UpdateType");
            xhr.setRequestHeader('Content-type', 'application/json; charset=UTF-8');
            var data = {
                "ID": inpID.value,
                "Name": inp.value
            };
            var f = function (ev) {
                if (ev.currentTarget.readyState == 4) {
                    if (ev.currentTarget.status == 200) {
                        var obj = JSON.parse(ev.currentTarget.responseText);
                        if (ev.Status == 0) {
                            mU.AModalOK.title("Message Error", "mess-error");
                            mU.AModalOK.message(ev.Message);
                        } else {
                            mU.AModalOK.title("Message", "mess-success");
                            mU.AModalOK.message("Row Update Success!");
                        }
                        mU.showModal();
                    }
                }

            };
            xhr.addEventListener("readystatechange", f, false);
            xhr.send(JSON.stringify(data));
        }
    });

    //var btSearch = new AButton(document.getElementById("btSearch"));
    //btSearch.on("click_btSearch", function (evt) {
    //    var fr = new FormData();
    //    fr.append("keyword", document.getElementById("inpKey").value);
    //    tb.LoadDataFirst(fr);
    //    btSearch.RemoveLoading(evt);
    //});
}

function updateClick(e, id, obj) {
    this.enableTab(1);
    this.selectedTab(1);
    var inp = document.getElementById("inpEType");
    var inpID = document.getElementById("inpEID");
    inp.value = obj.TypeName;
    inpID.value = obj.IdTypeStorageServer;
}

window.AScript.set("6102", true);