$(document).ready(function () {
    $("#cboPosType").lookupBuilder().onChanged(function () {
        displayPosSetting();
    });
    displayPosSetting();
});
function displayPosSetting() {
    var posType = $("#cboPosType").lookupBuilder().getSelectedValue();
    $(".container-pos").each(function () {
        var list = $(this).attr("data-pos-type").split(",");
        if (list.includes(posType))
            $(this).show();
        else
            $(this).hide();
    });
}
async function getEventLookupCombo($combo, search, code) {
    var id = $combo.attr('id');
    var url = "";
    var data = null;
    switch (id) {
        case "cboBank":
            url = "TreasuryApi/GetBankAccountList";
            data = { bank: true, search: search, filter: "", sort: "Name", pageNo: 1, selectedCode: code };
            break;
        case "cboPosType": 
            url = "TreasuryApi/GetPosTypeList";
            break;
    }
    await ajax({
        url: url,
        data: data,
        success: function (result) {
            if (id === "cboBank") {
                $.each(result, function () {
                    this.Code = this.Code + "|" + this.TafsilCurrencyName + "|" + this.TafsilGroupCode + "|" + this.TafsilCode;
                });
            }
            $combo.lookupBuilder().bindData(result);
        }
    });
}
function cboBankChange() {
    var selectedItem = $("#cboBank").lookupBuilder().getSelectedValue();
    if (selectedItem.length > 0) {
        selectedItem = selectedItem.split("|");
        $("#txtCurrency").val(selectedItem[1]);
    }
}   
function save() {
    var data = getJsonValue();
    var exceptionText = checkValidation('');
    if (exceptionText === '') {
        var bank = $("#cboBank").lookupBuilder().getSelectedValue().split("|");
        data["TafsilGroupCode"] = bank[2];
        data["TafsilCode"] = bank[3];
        var code = getQuery("code");
        var posType = Number(data["PosType"]);
        if (posType >= 3) {
            data["ServerIp"] = data["UrlIp"] + ":" + data["UrlPort"];
            if (posType === 4) {
                var posPort = $("#txtPosPort").val()
                data["PosIpAddress"] = $("#txtPosUrl").val() + (posPort ? ":" + posPort : "");
            }
        }
        ajax({
            loading: true,
            url: "TreasuryApi/" + (code === "" ? "InsertBankAccount" : "UpdateBankAccount"),
            data: data,
            success: function (result) {
                showMsgModal(2, result.FinalMessage.join("<br />"));
                refreshParent('loadGrid()');
                closeTopModal();
            }
        });
    }
    else {
        showMsgModal(0, exceptionText);
    }
}
function copyPosKey() {
    var data = $("#divPosKey").text();
    navigator.clipboard.writeText(data);
    $(".copy-toast").show();
    setTimeout(function () {
        $(".copy-toast").hide();
    }, 1000);
}



