$(document).ready(function () {
    $("#btnClosePanel").on('click', function () {
        IndexLibrary.ClosePanel(null);
    })
    IndexLibrary.InitializeSDK().then(function () {
        var contextData = IndexLibrary.GetPanelContext();
        LoadExistingWorkType(contextData.id, contextData.workType, contextData.workTypeDefault, contextData.workTypeColor, contextData.workTypeEffortIncluded);
    });
    //IndexLibrary.NotifyLoad();
});

function UpdateWorkType() {
    IndexLibrary.GetExtensionConfig()
        .then(async function (config) {
            var baseAPIUrl = config.apiBaseUrl;
            var workType = $("#txtWorkTypeName").val();
            var workTypeColor = $("#workTypeColor").val();
            var isDefault = $("#chkIsDefault").is(':checked')
            var isEffortIncluded = $("#chkIsEffortIncluded").is(':checked')
            var workTypeId = $("#workTypeId").val();

            IndexLibrary.UpdateWorkType(workTypeId, workType, workTypeColor, isDefault, isEffortIncluded, baseAPIUrl);
        });
}

function LoadExistingWorkType(id, workType, workTypeDefault, workTypeColor, workTypeEffortIncluded) {
    $("#txtWorkTypeName").val(workType);
    workTypeDefault == true ? $("#chkIsDefault").prop('checked', true) : $("#chkIsDefault").prop('checked', false)
    workTypeEffortIncluded == true ? $("#chkIsEffortIncluded").prop('checked', true) : $("#chkIsEffortIncluded").prop('checked', false)
    $("#workTypeColor").val(workTypeColor);
    $("#workTypeId").val(id);
}