
var trId = "";
var isAdd = true;

function EditOrderStatus(StatusNo) {
    ClearErrorMsg();
    isAdd = false;
    clearForm(isAdd, "divOrderStatus");
    $.ajax({
        url: "/System/GetOrderStatus",
        type: 'GET',
        cache: false,
        data: { StatusNo: StatusNo },
        success: function (result) {
            if (result.code == 0) {
                $("#StatusNo").val(result.data.StatusNo);
                $("#StatusName").val(result.data.StatusName);

                showOrderStatus(result.data.Icon.replace(".png",""));
            }

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

    return false;
}

function DeleteOrderStatus(StatusNo) {

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

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




    return false;
}

$(function () {

    setOperationBtns();

    TdRowOverOut("tOrderStatus");
       
    $('#divOrderStatus').dialog({
        width: 600,
        autoOpen: false,
        modal: true,
        buttons: {
        }
    });

    $(".divIcon .item").click(function () {
         
        clearSelectedIcon();
        $(this).addClass("selected");
        $(this).find(".check").removeClass("non-selected");
        $(this).find(".check").addClass("selected");

    });
  
    $('#btnOrderStatusNew').click(function () {
        isAdd = true;
        clearForm(isAdd, "divOrderStatus");
        showOrderStatus("1");
    });

    $('#btnOrderStatusCancel').click(function () {
        $('#divOrderStatus').dialog('close');
    });

    $('#btnOrderStatusSave').click(function () {
        ClearErrorMsg();
        if (isAdd) {
            if (checkRequired('divOrderStatus', 'errorMessageOrderStatus') == false) {
                return;
            }

            $.ajax({
                url: "/System/AddOrderStatus",
                type: 'POST',
                cache: false,
                data: {
                    StatusName: $("#StatusName").val(), StatusNo: $("#StatusNo").val(),Icon:getIcon()},
                success: function (result) {
                    if (result.code == 0) {
                        closeOrderStatus();
                        location.href = "/System/OrderStatusList";
                    }
                    else {
                        SetErrorMsg(result.message, "divOrderStatus");
                    }
                },
                error: function (e) {
                    SetErrorMsg("保存失败.", "divOrderStatus");
                }
            });
        }
        else {
            if (!checkRequired('divOrderStatus', 'errorMessageOrderStatus')) {
                return;
            }

            synConfirm("确定保存修改吗？", function () {
                $.ajax({
                    url: "/System/UpdateOrderStatus",
                    type: 'POST',
                    cache: false,
                    data: { StatusName: $("#StatusName").val(), StatusNo: $("#StatusNo").val(), Icon: getIcon()                },
                    success: function (result) {
                        if (result.code == 0) {
                            closeOrderStatus();
                            location.href = "/System/OrderStatusList";
                        }
                        else {
                            SetErrorMsg(result.message, "divOrderStatus");
                        }
                    },
                    error: function (e) {
                        SetErrorMsg("保存失败.", "divOrderStatus");
                    }
                });
            });
        }
    });

});


function showOrderStatus(icon) {
    console.log(icon);

    var items = $(".divIcon .item");
    $(items).each(function (i, v) {
        if ($(v).attr("id") == "item_" + icon) {
            $(v).addClass("selected");
            $(v).find(".check").removeClass("non-selected");
            $(v).find(".check").addClass("selected");
        }
        

    });

    $('#divOrderStatus').dialog('open');
}
function closeOrderStatus() {
    $('#divOrderStatus').dialog('close');
}


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

    clearSelectedIcon();

    ClearErrorMsg();
}

function clearSelectedIcon() {
    $(".selected").removeClass("selected");
    $(".divIcon .check").addClass("non-selected");
}

function getIcon() {
    console.log($(".divIcon .item.selected").attr("id"));
    return $(".divIcon .item.selected").attr("id");
}