var FactoryNo = "";
var trId = "";
var isAdd = true;

function EditFactory(FactoryNo) {  
    isAdd = false;

    clearForm(isAdd);

    $.ajax({
        url: "/Customer/GetFactory",
        type: 'GET',
        cache: false,
        data: { FactoryNo: FactoryNo },
        success: function (result) {
            if (result.FactoryNo != "") {
                $("#FactoryName").val(result.FactoryName);
                $("#Description").val(result.Description);
                $("#FactoryNo").val(result.FactoryNo);
                $("#Contact").val(result.Contact);
                $("#SearchKey").val(result.SearchKey);
                $("#Mobile").val(result.Mobile);
                $("#Phone").val(result.Phone);
                $("#QQ").val(result.QQ);
                $("#Other").val(result.Other);

                showFactory();
            }

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

    return false;
}


function DeleteFactory(FactoryNo) {

    synConfirm("确定删除吗？<br>提醒：该外协的报价信息等将一并删除。", function () {

        $.ajax({
            url: "/Customer/DeleteFactory",
            type: 'POST',
            cache: false,
            data: { FactoryNo: FactoryNo },
            success: function (result) {
                if (result.code == 0) {
                    location.href = '/Customer/Factory';
                }
                else if (result.code == 1) {
                    alert(result.message);
                }
                else {
                    alert('删除失败：该外协已产生业务数据，无法删除');
                }

            },
            error: function (e) {
                alert('删除失败：该外协已产生业务数据，无法删除');
            }
        });
    });
    return false;
}

function GetPY() {
    $.ajax({
        url: "/Home/GetPYSZM",
        type: 'GET',
        cache: false,
        data: {
            value: $("#FactoryName").val()
        },
        success: function (result) {
            if (result.code == 0) {
                $("#SearchKey").val(result.data);
            }
        },
        error: function (e) {

        }
    });
}

$(function () {

    $("#FactoryName").keyup(function () {
        //GetPY();
    }).change(function () {
        GetPY();
        });

    setOperationBtns();

    TdRowOverOut("tFactory");

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

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

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


    $('#btnSave').click(function () {

        if (isAdd) {
            if (checkRequired('divFactory','errorMessage') == false) {
                return;
            }

            synConfirm("确定保存吗？", function () {

                $.ajax({
                    url: "/Customer/AddFactory",
                    type: 'POST',
                    cache: false,
                    data: $('form').serialize(),
                    success: function (result) {
                        if (result.code == 0) {
                            closeFactory();
                            location.href = "/Customer/Factory";
                        }
                        else {
                            SetErrorMsg(result.message);
                        }
                    },
                    error: function (e) {
                        SetErrorMsg("保存失败");
                    }
                });
            });

        }
        else {
            if (!checkRequired('divFactory','errorMessage')) {
                return;
            }

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

function showFactory() {
    $('#divFactory').dialog('open');
}

function closeFactory() {
    $('#divFactory').dialog('close');
}

function clearForm(isAdd) {
    $("input").removeClass("redBorder");
    $("input[type=text]").val("");
    $("textarea").val("");    
    
    ClearErrorMsg();
    if (isAdd) {       
        $("#FactoryName").focus();
    }
    else {
        $("#FactoryName").focus();
    }
}


function downExcel() {
    location.href = "/Customer/GetFactoryExcel?Keywords=" + $("#Keywords").val();
}