var contractName = getQuery("name");
var contractcode = getQuery("contractCode");
var code = getQuery("code");
var insurRate;
$(document).ready(function () {
    $("#rodTabulatedTax").on("change", function () {
        $("#txtPercentOfTotal").prop("disabled", $(this).is(":checked"))
    });
    $("#rodExemption").on("change", function () {
        $("[data-id='ExemptionPart'],[data-id='ExemptionTotal']").prop("disabled", !$(this).is(":checked"))
    });
    initial();
});
async function initial() {
    if (code)
        await getInsurRate(contractcode, code);
    if (contractName)
        $("#lblContractName").text(contractName);
    $("#divMain").show();
    closeLoading();
}
function getInsurRate(contractcode, code) {
    ajax({
        url: "SalaryApi/GetInsuranceRate",
        data: { contractTypeCode: contractcode, code: code },
        success: function (result) {
            insurRate = result;
            var startYearMonth = result.StartYearMonth.toString();
            var endYearMonth = result.EndYearMonth.toString();
            $("#txtFromYear").val(startYearMonth.substr(0, 4));
            $("#txtToYear").val(endYearMonth.substr(0, 4));
            $("#cboFromMonth").val(startYearMonth.substr(4, 2));
            $("#cboToMonth").val(endYearMonth.substr(4, 2));
            setJsonValue(result);
            var checkedTabulatedTax = result.TabulatedTax ? result.TabulatedTax : false;
            $("#rodTabulatedTax").prop("checked", checkedTabulatedTax);
            $("#txtPercentOfTotal").prop("disabled", !checkedTabulatedTax);
        }
    });
}
function save() {
    var data = getJsonValue("");
    var url = "SalaryApi/" + (code ? "UpdateInsuranceRate" : "InsertInsuranceRate");
    data["StartYearMonth"] = $("#txtFromYear").val() + $("#cboFromMonth").val();
    data["EndYearMonth"] = $("#txtToYear").val() + $("#cboToMonth").val();
    if (code) {
        data["Code"] = code;
        data["RegDate"] = insurRate.RegDate;
    }
    if (contractcode || (insurRate && insurRate.ContractTypeCode)) {
        data["ContractTypeCode"] = contractcode ? contractcode : insurRate.ContractTypeCode;
        if (!contractcode) {
            data["PremiumTaxExemption"] = insurRate.PremiumTaxExemption;
            data["TabulatedTax"] = insurRate.TabulatedTax;
            data["PercentOfTotal"] = insurRate.PercentOfTotal;
        }
    }
    var error = checkValidation("");
    if (error === "") {
        ajax({
            url: url,
            data: data,
            loading:true,
            success: function (result) {
                refreshParent('refresh()');
                showMsgModal(2, result.FinalMessage);
                closeTopModal();
            }
        });
    }
    else {
        showMsgModal(0, error, error);
    }
}

