var teamUsers;
var inUploading = false;
$(function () {
  
    $("#btnNew").click(function () {
        location.href = "/Portal/CreateOrder";
    });

    TdRowOverOut("tOrder");

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

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

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

    $("#btnUpload").click(function () {
        ajaxFileUpload();
    });

});

function ShowDetail(id) {
    $("#OrderNo").val(id);
    $.ajax({
        url: "/Portal/GetDetail",
        type: 'POST',
        cache: false,
        data: { OrderNo: id },
        success: function (result) {
            if (result.code == 0) {
                $("#dStatus").html(result.data.StatusDescription);
                $("#dOrderNo").html(result.data.OrderNo);
                $("#dOrderName").html(result.data.OrderName);
                $("#dCustomer").html(result.data.CustomerShortName);
                $("#dContact").html(result.data.Contact);
                $("#dCreatedTime").html(result.data.CreatedTimeString);               
                $("#dCreator").html(result.data.Creator);
                $("#dDetail").html(result.data.Detail);
                $("#dNumber").html(result.data.Number);
                $("#dExpectDeliveryDate").html(result.data.ExpectDeliveryDateString);
                $("#dHistory").html(result.data.History);
                $("#dMemo").html(result.data.Memo);
                GenerateWorkFlowSteps(result.data.steps);
                $("#dQueryURL").html("");
                var a = $("<a target='_blank' href='" + result.data.QueryURL + "'>" + result.data.QueryURL + "</a>").appendTo($("#dQueryURL"));
               
                var t = $("#tdAttachment");
                t.html("");
                for (var r in result.data.Attachments) {
                    var a = $("<div title='点击下载'><img src='" + result.data.Attachments[r].Addition1 + "' alt=''/><a href='#' style='text-decoration:underline;' onclick=\"javascript:return DownLoadFile('" + result.data.Attachments[r].ItemValue + "');\">" + result.data.Attachments[r].ItemText + "</a> (" + result.data.Attachments[r].Addition2 + ")</div>");
                    a.appendTo(t);
                }

                $('#divDetail').dialog('open');
            }
            else {
                alert("获取信息失败.");
            }

        },
        error: function (e) {
            alert("获取信息失败.");
        }
    });
}

function ajaxFileUpload() {
    ClearErrorMsg();

    if ($("#fileToUpload").val() == "") {
        SetErrorMsg("请选择上传文件", "divAttachment");
        return;
    }

    var point = $("#fileToUpload").val().lastIndexOf(".");
    var type = $("#fileToUpload").val().substr(point).toLowerCase();

    if (type == ".exe" || type == ".bat" || type == ".msi") {
        SetErrorMsg("文件类型不允许", "divAttachment");
        return;
    }
    $("#loading").show();

    $.ajaxFileUpload({
        url: '/Portal/FileUpload',
        secureuri: false,
        fileElementId: 'fileToUpload',
        data: { OrderNo: $("#OrderNo").val() },
        dataType: 'json',
        success: function (d, status) {
            var data = JSON.parse(d);
            data = data.Data;
            if (data.code == "1") {
                $("#divAttachment input").removeAttr("disabled");
                inUploading = false;
                SetErrorMsg(data.message, "divAttachment");
                $("#loading").hide();
            }
            else if (data.code == "0") {
                $("#divAttachment input").removeAttr("disabled");
                inUploading = false;
                $("#loading").hide();
                alert("上传成功！");
                $('#divAttachment').dialog('close');
            }
        },
        error: function (data, status, e) {
            console.log(data);
            console.log(status);
            console.log(e);
            $("#divAttachment input").removeAttr("disabled");
            inUploading = false;
            SetErrorMsg("上传时发生错误", "divAttachment");
            $("#loading").hide();
        }
    });

    $("#divAttachment input").attr("disabled", "disabled");
    inUploading = true;
    return false;
}

function ShowAttachment(OrderNo) {

    ClearErrorMsg();
    $("#OrderNo").val(OrderNo);

    $.ajax({
        url: "/Portal/GetOrderAttachments",
        type: 'GET',
        cache: false,
        data: { OrderNo: $("#OrderNo").val() },
        success: function (result) {
            if (result.code == 0) {
                $("#dAttachment").html("");

                if (result.data.length > 0) {
                    for (var r in result.data) {
                        var tr = $("<tr id='" + result.data[r].ItemValue + "'></tr>");
                        var td = $("<td style='text-align: center;'></td>");
                        var image = $("<img src='" + result.data[r].Addition1 + "' alt=''>");
                        image.appendTo(td);
                        td.appendTo(tr);
                        td = $("<td></td>");
                        td.html(result.data[r].ItemText);
                        td.appendTo(tr);
                        td = $("<td></td>");
                        td.html(result.data[r].Addition2);
                        td.appendTo(tr);
                        td = $("<td></td>");
                        var tno = result.data[r].ItemValue;
                        var t = $("<a href='#' onclick=\"javascript:return DownLoadFile('" + tno + "');\">下载</a>");
                        t.appendTo(td);

                        t = $("<a href='#' style='margin-left:5px;' onclick=\"javascript:return DeleteFile('" + tno + "','" + result.data[r].ItemText + "');\">删除</a>");

                        t.appendTo(td);
                        td.appendTo(tr);

                        tr.appendTo($("#dAttachment"));
                    }
                }
                $('#divAttachment').dialog('open');
            }
            else {
                alert("获取信息失败.");
            }

        },
        error: function (e) {
            alert("获取信息失败.");
        }
    });
}

function DeleteFile(no, name) {
    ClearErrorMsg();

    if (inUploading) {
        return false;
    }

    if (confirm('确定删除附件 ' + name + ' 吗？')) {
        $.ajax({
            url: "/Portal/DeleteAttachment",
            type: 'GET',
            cache: false,
            data: { FileNo: no, OrderNo: $("#OrderNo").val() },
            success: function (result) {
                if (result.code == 0) {
                    $("#tAttachment").find("tr[id=" + result.FileNo + "]").remove();
                }
                else {
                    alert("删除失败.");
                }

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

function DownLoadFile(no) {

    if (inUploading) {
        return false;
    }

    location.href = "/Portal/DownLoadAttachment?FileNo=" + no;
}

function EditOrder(id) {

    var url = {};
    url.root = "/MySpace/Index";
    url.paras = [];
    url.paras.push({ name: "page", value: $($(".currentnum")[0]).html() });
 
    location.href = "/Portal/EditOrder?OrderNo=" + id + "&GoBackURL=" + JSON.stringify(url);
}