var Id;

$(function () {

    setOperationBtns();

    $("#tOrder tr td").click(function () {
        if ($(this).hasClass("td-last") || $(this).hasClass("td-checkbox")) {
            return;
        }
        else {
            ShowQuotationDetail($(this).parent().attr("id"));
        }
    });


    TdRowOverOutWithSpan("tOrder");
});


function Delete(id, orderName) {
    synConfirm("确定删除报价 [" + orderName + "] 吗?</br><b class='red-text'>警告：此操作将彻底删除数据，删除后无法恢复。</b>",
        function () {
            $.ajax({
                url: "/Order/QuotationDelete",
                type: 'POST',
                cache: false,
                data: { Id: id },
                success: function (result) {
                    if (result.code == 0) {

                        $("#tOrder").find("tr[id='" + id + "']").remove();
                    }
                    else if (result.code == 1) {
                        alert(result.message);
                    }
                },
                error: function (e) {
                    alert('删除失败');
                }
            });
        });
}

function Edit(id, editModel) {
    var url = {};
    url.root = "/Order/QuotationList";
    url.paras = [];

    if ($("#sCustomerName").val() != "") {
        url.paras.push({ name: "sCustomerName", value: $("#sCustomerName").val() });
    }

    if ($("#sBeginDate").val() != "") {
        url.paras.push({ name: "sBeginDate", value: $("#sBeginDate").val() });
    }
    if ($("#sEndDate").val() != "") {
        url.paras.push({ name: "sEndDate", value: $("#sEndDate").val() });
    }

    if ($("#sKey").val() != "") {
        url.paras.push({ name: "sKey", value: $("#sKey").val() });
    }

    url.paras.push({ name: "page", value: $($(".currentnum")[0]).html() });

    if (editModel == "1") {
        location.href = "/Order/QuotationEdit?Id=" + id + "&GoBackURL=" + JSON.stringify(url);
    }
    else if (editModel == "2") {
        location.href = "/Order/QuotationSimpleEdit?Id=" + id + "&GoBackURL=" + JSON.stringify(url);
    }
}

function Download(id) {
    location.href = "/Pdf/PrintQuotationPdf?Ids=" + id;
}


function DownloadExcel(id) {
    location.href = "/Order/PrintQuotationExcel?Ids=" + id;
}

function newQuatation() {
    location.href = "/Order/QuotationCreate";
}

function newSimpleQuatation() {
    location.href = "/Order/QuotationSimpleCreate";
}

function ConvertToOrder(id, editModel) {
    synConfirm("确定将该报价单转换成客户订单吗？</br><b>提示：转换成功后，此报价单将自动删除。</b>",
        function () {
            $.ajax({
                url: "/Order/QuotationConvert",
                type: 'POST',
                cache: false,
                data: { Id: id },
                success: function (result) {
                    if (result.code == 0) {
                        if (editModel == "1") {
                            location.href = "/Order/Edit?message=1&OrderNo=" + result.data;
                        }
                        else {
                            location.href = "/Order/SimpleEdit?message=1&OrderNo=" + result.data;
                        }

                    }
                    else if (result) {
                        alert('转换失败');
                    }
                },
                error: function (e) {
                    alert('转换失败');
                }
            });
        });
}