var code = getQuery("code");
var lstCode = getQuery("lstCode");
$(document).ready(function () {
    $("#cboCorrespondence").lookupBuilder().onChanged(async function () {
        var correspondenceCode = $("#cboCorrespondence").lookupBuilder().getSelectedValue();
        getCustomer(correspondenceCode);
    });
    initial();
});
async function initial() {
    if (code) {
        await getPersonnel(code);
        $("#cboCorrespondence").lookupBuilder().setEnabled(false)
    }
    $("#divMain").show();
    closeLoading();
}
async function getPersonnel(code) {
    await ajax({
        url: "SalaryApi/GetPersonnel",
        data: { code: code },
        success: function (result) {
            setJsonValue(result);
        }
    });
}
function getCustomer(code) {
    if (code) {
        ajax({
            loading: true,
            url: "GeneralApi/GetCustomer",
            data: { code: code },
            success: function (result) {
                setJsonValue(result, "#divCorrespondenceInfo");
            }
        });
    } else {
        $("#txtMobileNo").text("");
        $("#txtTel").text("");
        $("#txtNationalCode").text("");
        $("#txtEmail").text("");
        $("#txtPostalCode").text("");
        $("#txtAddress").text("");
        $("#txtBirthDate").text("");
        $("#txtProvinceName").text("");
        $("#txtCityName").text("");
    }
}
async function getEventLookupCombo($combo, search, code) {
    var id = $combo.attr('id');
    var url = "";
    var data = null;
    switch (id) {
        case "cboCorrespondence":
            url = "GeneralApi/GetCustomerList";
            data = { type:2,filter: lstCode ? "WHERE Code NOT IN (" + lstCode+")" : "", search: search, pageNo: 1, selectedCode: code};
            break;
        case "cboBank":
            url = "TreasuryApi/GetBankList";
            data = { search: search, pageNo: 1, selectedCode: code};
            break;
    }
    if (!url)
        return;
    var result;
    await ajax({
        url: url,
        data: data,
        success: function (resValue) {
            result = resValue;
        }
    });
    $combo.lookupBuilder().bindData(result);
}
function save() {
    var error = checkValidation("");
    if (error === "") {
        var data = getJsonValue("");
        if (code)
            edit(data);
        else
            insert(data);
    }
    else
        showMsgModal(0, error, error);
}
function insert(data) {
    ajax({
        url: "SalaryApi/InsertPersonnel",
        data: data,
        success: function (result) {
            refreshParent('refresh()');
            showMsgModal(2, result.FinalMessage);
            closeTopModal();
        }
    });
}
function edit(data) {
    ajax({
        url: "SalaryApi/UpdatePersonnel",
        data: data,
        success: function (result) {
            refreshParent('refresh()');
            showMsgModal(2, result.FinalMessage);
            closeTopModal();
        }
    });
}

