var trId = "";
var isAdd = true;

function EditPaymentType(PaymentTypeNo) {
    ClearErrorMsg();
    isAdd = false;
    clearForm(isAdd, "divPaymentType");
    $.ajax({
        url: "/System/GetPaymentType",
        type: 'GET',
        cache: false,
        data: { PaymentTypeNo: PaymentTypeNo },
        success: function (result) {
            if (result.code == 0) {
                $("#TypeName").val(result.data.TypeName);
                $("#PaymentTypeNo").val(result.data.PaymentTypeNo);
                showPaymentType();
            }

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

    return false;
}

function DeletePaymentType(PaymentTypeNo) {

    synConfirm("确定删除吗？", function () {
        $.ajax({
            url: "/System/DeletePaymentType",
            type: 'POST',
            cache: false,
            data: { PaymentTypeNo: PaymentTypeNo },
            success: function (result) {
                if (result.code == 0) {
                    location.href = '/System/PaymentTypeList';
                }
                else {
                    alert(result.message);
                }

            },
            error: function (e) {
                alert('删除失败：该数据已被引用，无法删除');
            }
        });
    });

    return false;
}

$(function () {

    setOperationBtns();

    TdRowOverOut("tPayment");

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

    $('#btnPaymentTypeNew').click(function () {
        isAdd = true;
        clearForm(isAdd, "divPaymentType");
        showPaymentType();
    });
 
    $('#btnPaymentTypeCancel').click(function () {
        $('#divPaymentType').dialog('close');
    });

    $('#btnPaymentTypeSave').click(function () {
        ClearErrorMsg();
        if (isAdd) {
            if (checkRequired('divPaymentType', 'errorMessagePaymentType') == false) {
                return;
            }
            console.log($('#formPaymentType').serialize());
            $.ajax({
                url: "/System/AddPaymentType",
                type: 'POST',
                cache: false,
                data: $('#formPaymentType').serialize(),
                success: function (result) {
                    if (result.code == 0) {
                        closePaymentType();
                        location.href = "/System/PaymentTypeList";
                    }
                    else {
                        SetErrorMsg(result.message, "divPaymentType");
                    }
                },
                error: function (e) {
                    SetErrorMsg("保存失败", "divPaymentType");
                }
            });
        }
        else {
            if (!checkRequired('divPaymentType', 'errorMessagePaymentType')) {
                return;
            }

            synConfirm("确定保存修改吗？", function () {
                $.ajax({
                    url: "/System/UpdatePaymentType",
                    type: 'POST',
                    cache: false,
                    data: $('#formPaymentType').serialize(),
                    success: function (result) {
                        if (result.code == 0) {
                            closePaymentType();
                            location.href = "/System/PaymentTypeList";
                        }
                        else {
                            SetErrorMsg(result.message, "divPaymentType");
                        }
                    },
                    error: function (e) {
                        SetErrorMsg("保存失败", "divPaymentType");
                    }
                });
            });


        }
    });
      

});

function showPaymentType() {
    $('#divPaymentType').dialog('open');
}

function closePaymentType() {
    $('#divPaymentType').dialog('close');
}


function clearForm(isAdd, div) {
    $("#" + div).find("input").removeClass("redBorder");
    $("#" + div).find("input[type=text]").val("");
    $("#" + div).find("textarea").val("");

    ClearErrorMsg();
}

