var AccountTitleNo = "";
var trId = "";
var isAdd = true;

function EditAccountTitle(AccountTitleNo) {  
    isAdd = false;

    clearForm(isAdd);

    $.ajax({
        url: "/Finance/GetAccountTitle",
        type: 'GET',
        cache: false,
        data: { AccountTitleNo: AccountTitleNo },
        success: function (result) {
            if (result.AccountTitleNo != "") {
                $("#AccountTitleName").val(result.AccountTitleName);
                $("#Memo").val(result.Memo);
                //$("#AutoMemo").html(result.AutoMemo);
                $("#AccountTitleNo").val(result.AccountTitleNo);
                $("#AccountTitleType").val(result.AccountTitleType);

                if (result.AccountTitleType == "1") {
                    $("input[name='AccountTitleType'][value='1']").prop("checked", true);
                }
                else {
                    $("input[name='AccountTitleType'][value='2']").prop("checked", true);
                }
             
                if (result.IsActive) {
                    $($("input[name='IsActive']").get(0)).prop("checked", true);
                }
                else {
                    $($("input[name='IsActive']").get(1)).prop("checked", true);
                }

                showAccountTitle();
            }

        },
        error: function (e) {
        }
    });

    return false;
}

function DeleteAccountTitle(AccountTitleNo,AccountTitleName) {  

    synConfirm('确定删除科目 [' + AccountTitleName + '] 吗？', function () {
        $.ajax({
            url: "/Finance/DeleteAccountTitle",
            type: 'POST',
            cache: false,
            data: { AccountTitleNo: AccountTitleNo },
            success: function (result) {
                if (result.code == 0) {
                    location.href = '/Finance/AccountTitle';
                }
                else {
                    alert(result.message);
                }

            },
            error: function (e) {
                alert('该数据已产生业务数据，无法删除');
            }
        });

    });


    return false;
}

$(function () {

    setOperationBtns();

    TdRowOverOut("tAccountTitle");    

    $('#divAccountTitle').dialog({
        width: 500,
        autoOpen: false,
        modal: true,
        buttons: {
        }
    });

    $('#btnNew').click(function () {
        isAdd = true;
        clearForm(isAdd);
        showAccountTitle();
        $("#AccountTitleName").focus();
    });

    $('#btnCancel').click(function () {
        $('#divAccountTitle').dialog('close');
    });


    $('#btnSave').click(function () {
       
        if (isAdd) {
            if (checkRequired('divAccountTitle', 'errorMessage') == false) {
                return;
            }

            $.ajax({
                url: "/Finance/AddAccountTitle",
                type: 'POST',
                cache: false,
                data: $('form').serialize(),
                success: function (result) {
                    if (result.code == 0) {
                        closeAccountTitle();
                        location.href = "/Finance/AccountTitle";
                    }
                    else {
                        $("#errorMessage").text(result.message);
                    }
                },
                error: function (e) {
                    $("#errorMessage").text("保存失败.");
                }
            });
        }
        else {
            synConfirm("确定保存修改吗？", function () {
                $.ajax({
                    url: "/Finance/UpdateAccountTitle",
                    type: 'POST',
                    cache: false,
                    data: $('form').serialize(),
                    success: function (result) {
                        if (result.code == 0) {
                            closeAccountTitle();
                            location.href = "/Finance/AccountTitle";
                        }
                        else {
                            $("#errorMessage").text(result.message);
                        }
                    },
                    error: function (e) {
                        $("#errorMessage").text("保存失败.");
                    }
                });
            });           
        }
    });
});

function showAccountTitle() {
    $('#divAccountTitle').dialog('open');
}

function closeAccountTitle() {
    $('#divAccountTitle').dialog('close');
}

function clearForm(isAdd) {
    $("input").removeClass("redBorder");
    $("input[type=text]").val("");
    $("textarea").val("");    
    
    ClearErrorMsg();

    if (isAdd) {       
        $("#AccountTitleName").focus();
    }
    else {
        $("#AccountTitleName").focus();
    }
}

function SetColor() {
    $("#tAccountTitle").find("tr").each(function (i, val) {
        if ($(val).find("td:eq(3)").find("span").html() == "禁用") {
            $(val).css("background-color", "#bbbbbb");
        }      

    });
}