
var isAdd = true;

function EditStep(WorkFlowStepNo) {  
    isAdd = false;

    clearForm(isAdd);

    $.ajax({
        url: "/System/GetWorkFlowStep?WorkFlowNo=" + $("#WorkFlowNo").val() + "&WorkFlowStepNo" + WorkFlowStepNo,
        type: 'GET',
        cache: false,
        data: { WorkFlowStepNo: WorkFlowStepNo },
        success: function (result) {
            if (result.code == 0) {
                $("#WorkFlowStepNo").val(result.data.WorkFlowStepNo);
                $("#WorkFlowStepName").val(result.data.WorkFlowStepName);
                $("#SN").val(result.data.SN);
                $("#StatusNo").val(result.data.StatusNo);
                $("#aUser").empty();
                $("#bUser").empty();
             
                for (var i = 0; i < result.data.Users.length; i++) {
                    if (result.data.Users[i].AdditionalValue == "0") {
                        $("#aUser").append("<option value='" + result.data.Users[i].OptionValue + "'>" + result.data.Users[i].OptionText + "</option>");
                    }
                    else {
                        $("#bUser").append("<option value='" + result.data.Users[i].OptionValue + "'>" + result.data.Users[i].OptionText + "</option>");
                    }
                }

                $("#aFollowUp").empty();
                $("#bFollowUp").empty();
                for (var i = 0; i < result.data.FollowUps.length; i++) {
                    if (result.data.FollowUps[i].AdditionalValue == "0") {
                        $("#aFollowUp").append("<option value='" + result.data.FollowUps[i].OptionValue + "'>" + result.data.FollowUps[i].OptionText + "</option>");
                    }
                    else {
                        $("#bFollowUp").append("<option value='" + result.data.FollowUps[i].OptionValue + "'>" + result.data.FollowUps[i].OptionText + "</option>");
                    }
                }

                if (result.data.IsEndPoint) {
                    $("#tr_ctinvolved").hide();
                    $("#tr_ctpercent").hide();
                }
                else {
                    $("#tr_ctinvolved").show();
                    $("#tr_ctpercent").show();
                    if (result.data.CtInvolved) {
                        $("#CtInvolved").prop("checked", true);
                    }
                    else {
                        $("#CtInvolved").prop("checked", false);
                    }

                    $("#CtPercent").val(result.data.CtPercent);
                }

                showStep(result.data.IsBeginPoint,result.data.IsEndPoint);
            }
            else {
                SetErrorMsg(result.message);                
            }

        },
        error: function (e) {
            SetErrorMsg("查找信息失败");
        }
    });

    return false;
}

function AddStep() {
    isAdd = true;

    clearForm(isAdd);
   
    $.ajax({
        url: "/System/GetWorkFlowStep?WorkFlowNo=" + $("#WorkFlowNo").val() ,
        type: 'GET',
        cache: false,
        data: {},
        success: function (result) { 
            if (result.code == 0) {
                $("#WorkFlowStepNo").val(result.data.WorkFlowStepNo);
                $("#WorkFlowStepName").val(result.data.WorkFlowStepName);

                $("#aUser").empty();
                $("#bUser").empty();
                for (var i = 0; i < result.data.Users.length; i++) {
                    if (result.data.Users[i].AdditionalValue == "0") {
                        $("#aUser").append("<option value='" + result.data.Users[i].OptionValue + "'>" + result.data.Users[i].OptionText + "</option>");
                    }
                    else {
                        $("#aUser").append("<option value='" + result.data.Users[i].OptionValue + "'>" + result.data.Users[i].OptionText + "</option>");
                    }
                }

                $("#aFollowUp").empty();
                $("#bFollowUp").empty();
                for (var i = 0; i < result.data.FollowUps.length; i++) {
                    if (result.data.FollowUps[i].AdditionalValue == "0") {
                        $("#aFollowUp").append("<option value='" + result.data.FollowUps[i].OptionValue + "'>" + result.data.FollowUps[i].OptionText + "</option>");
                    }
                    else {
                        $("#bFollowUp").append("<option value='" + result.data.FollowUps[i].OptionValue + "'>" + result.data.FollowUps[i].OptionText + "</option>");
                    }
                }

                $("#CtInvolved").prop("checked", true);

                $("#tr_ctinvolved").show();
                $("#tr_ctpercent").show();

                showStep(false,false);
            }
            else {
                SetErrorMsg(result.message);               
            }

        },
        error: function (e) {
            SetErrorMsg("查找信息失败");
        }
    });

    return false;
}

function DeleteStep(WorkFlowStepNo, workFlowStepName) {

    synConfirm('确定删除步骤 [' + workFlowStepName + '] 吗？', function () {
        $.ajax({
            url: "/System/DeleteWorkFlowStep",
            type: 'POST',
            cache: false,
            data: { WorkFlowStepNo: WorkFlowStepNo },
            success: function (result) {
                if (result.code == 0) {
                    location.href = "/System/WorkFlowDetail?WorkFlowNo=" + $("#WorkFlowNo").val();
                }
                else {
                    alert(result.message);
                }

            },
            error: function (e) {
                alert('删除失败');
            }
        });
    });

   return false;
}


$(function () {

    //$("#StatusNo").attr('title', '如果下拉框中没有所需的状态值，请前往“系统管理->列表配置”页面添加新的状态值');
    //$("#StatusNo").tooltip();

    $("#SN").attr('title', '步骤序号只影响在表格中的显示顺序，不影响其它功能');
    $("#SN").tooltip();

    $("#CtPercent").inputmask({ 'mask': "9{0,9}[.9{0,2}]", greedy: false });

    TdRowOverOut("tWorkFlowDetail"); 

    $("#canvas").attr("width", $("#canvas").parent().width());
    $('#divStep').dialog({
        width: 600,
        autoOpen: false,
        modal: true,
        buttons: {
        }
    });

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

    $('#btnNew').click(function () {
        AddStep();
    });

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

    $('#btnCancelStatus').click(function () {
        $('#divStatus').dialog('close');
    });

    $("#btnAddUser").click(function () {
        MoveOptions("aUser", "bUser");
    });

    $("#btnRemoveUser").click(function () {
        MoveOptions("bUser", "aUser");
    });

    $("#btnAddStep").click(function () {
        MoveOptions("aFollowUp", "bFollowUp");
    });

    $("#btnRemoveStep").click(function () {
        MoveOptions("bFollowUp", "aFollowUp");
    });


    $("#btnSaveStatus").click(function () {

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

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

            $.ajax({
                url: "/System/AddOrderStatus",
                type: 'POST',
                cache: false,
                data: {StatusName: $('#StatusName').val()},
                success: function (result) {
                    if (result.code == 0) {
                        closeStatus();
                        // 
                        $("#StatusNo").append("<option value=\"" + result.data + "\">" + $('#StatusName').val() + "</option>");
                        $("#StatusNo").find("option[value='"  + result.data + "']").attr("selected", true);

                    }
                    else {
                        SetErrorMsg(result.message, "divStatus");
                    }
                },
                error: function (e) {
                    SetErrorMsg("保存失败.", "divStatus");
                }
            });
            
           
        });
    });


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

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

        synConfirm("确定要保存吗？", function () {
            var users = [];
            var followups = [];
            $("#bUser option").each(function () {
                users.push(this.value);
            });
            $("#bFollowUp option").each(function () {
                followups.push(this.value);
            });

            if (isAdd) {
                $.ajax({
                    url: "/System/AddWorkFlowStep",
                    type: 'POST',
                    cache: false,
                    data: {
                        Users: JSON.stringify(users),
                        FollowUps: JSON.stringify(followups),
                        WorkFlowStepNo: $("#WorkFlowStepNo").val(),
                        WorkFlowStepName: $("#WorkFlowStepName").val(),
                        WorkFlowNo: $("#WorkFlowNo").val(),
                        StatusNo: $("#StatusNo").val(),
                        SN: $("#SN").val(),
                        CtPercent: $("#CtPercent").val(),
                        CtInvolved: $("#CtInvolved").prop("checked")
                    },
                    success: function (result) {
                        if (result.code == 0) {
                            closeStep();
                            location.href = "/System/WorkFlowDetail?WorkFlowNo=" + $("#WorkFlowNo").val();
                        }
                        else {
                            SetErrorMsg(result.message, "divStep");
                           
                        }
                    },
                    error: function (e) {
                        SetErrorMsg("保存失败.", "divStep");                        
                    }
                });
            }
            else {
                $.ajax({
                    url: "/System/UpdateWorkFlowStep",
                    type: 'POST',
                    cache: false,
                    data: {
                        Users: JSON.stringify(users),
                        FollowUps: JSON.stringify(followups),
                        WorkFlowStepNo: $("#WorkFlowStepNo").val(),
                        WorkFlowStepName: $("#WorkFlowStepName").val(),
                        WorkFlowNo: $("#WorkFlowNo").val(),
                        StatusNo: $("#StatusNo").val(),
                        SN: $("#SN").val(),
                        CtPercent: $("#CtPercent").val(),
                        CtInvolved: $("#CtInvolved").prop("checked")
                    },
                    success: function (result) {
                        if (result.code == 0) {
                            closeStep();
                            location.href = "/System/WorkFlowDetail?WorkFlowNo=" + $("#WorkFlowNo").val();
                        }
                        else {
                            SetErrorMsg(result.message, "divStep");
                            $("#errorMessage").text(result.message);
                        }
                    },
                    error: function (e) {
                        SetErrorMsg("保存失败.", "divStep");                        
                    }
                });
            }
        });
    });

    // Get data and draw the canvas
    $.ajax({
        url: "/System/GetWorkFlowDetail",
        type: 'POST',
        cache: false,
        data: { WorkFlowNo: $("#WorkFlowNo").val() },
        success: function (result) {
            if (result.code == 0) {
                DrawWorkFlowCanvas('canvas',result.data);
            }
            else {
                alert(result.message);
                location.herf = "/System/WorkFlow";
            }
        },
        error: function (e) {
            alert("获取数据失败，请稍后重试");
        }
    });

});

function showStep(isBengin, isEnd) {
    if (isBengin || isEnd) {        
        $("#WorkFlowStepName").attr("disabled", "disabled");
        $("#SN").attr("disabled", "disabled");
        $("#StatusNo").attr("disabled", "disabled");
        $("#btnAddUser").attr("disabled", "disabled");
        $("#btnRemoveUser").attr("disabled", "disabled");
        $("#aUser").attr("disabled", "disabled");
        $("#bUser").attr("disabled", "disabled");
    }
    else {
        $("#WorkFlowStepName").removeAttr("disabled");
        $("#SN").removeAttr("disabled");
        $("#StatusNo").removeAttr("disabled");
        $("#btnAddUser").removeAttr("disabled");
        $("#btnRemoveUser").removeAttr("disabled");
        $("#aUser").removeAttr("disabled");
        $("#bUser").removeAttr("disabled");
    }

    if (isEnd) {
        $("#aFollowUp").attr("disabled", "disabled");
        $("#bFollowUp").attr("disabled", "disabled");
        $("#btnAddStep").attr("disabled", "disabled");
        $("#btnRemoveStep").attr("disabled", "disabled");
        $("#btnSave").hide();
        btnSave
    }
    else {
        $("#aFollowUp").removeAttr("disabled");
        $("#bFollowUp").removeAttr("disabled");
        $("#btnAddStep").removeAttr("disabled");
        $("#btnRemoveStep").removeAttr("disabled");
        $("#btnSave").show();
    }
    $('#divStep').dialog('open');
}

function closeStep() {
    $('#divStep').dialog('close');
}


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

function MoveOptions(sourceId, destinationId) {
    var x = $("#" + sourceId+" option");
    var aDepartmentList = $("#" + sourceId);
    var departmentList = $("#"+destinationId);
    var msgs = "";
    x.each(function () {
        if (this.selected) {
            departmentList.prepend(this.outerHTML);
            $("#"+ sourceId + " option[value='" + this.value + "']").remove();
        }
    });
}

function addStatus() {
    $("#StatusName").val("");
    $('#divStatus').dialog('open');
}

function closeStatus() {
    $('#divStatus').dialog('close');
}