<div class="current-location">
    <ul>
        <li class="left"><i class="glyphicon glyphicon-home"></i><a href="javascript:void(0)">在线更新</a></li>
    </ul>
</div>
<style type="text/css">
    .step li {
        float: left;
        margin-right: 20px;
        color: #999
    }

        .step li.current {
            font-weight: bold;
            color: #ff6a00
        }

            .step li.current .badge {
                background-color: #ff6a00
            }
    .dropdown-menu {
        left:265px;
        padding: 10px !important
    }

        .dropdown-menu p {
            line-height: 30px;
        }
</style>
<div class="main">
    <form class="form" method="post">
        <div class="form-group">
            <label class="control-label head">更新步骤</label>
            <div class="controls">
                <div class="form-control-static step clearfix">
                    <ul id="step">
                        <li><span class="badge badge-primary">1</span>检测更新</li>
                        <li><span class="badge">2</span> 下载升级文件</li>
                        <li><span class="badge">3</span> 重启网站进程</li>
                        <li><span class="badge">4</span> 更新网站文件</li>
                        <li><span class="badge ">5</span> 升级完毕</li>
                    </ul>
                </div>
                <span class="help-block"></span>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">当前版本</label>
            <div class="controls">
                <p class="form-control-static">@ViewBag.CurrentVersion</p>
                <span class="help-block">
                    <span class="text-success hidden" id="lbIslastversion"><i class="fa fa-info-circle"></i>当前版本已是最新版本!</span>
                </span>
            </div>
        </div>

        <div class="form-group hidden upgradeListBox">
            <label class="control-label">待升级版本</label>
            <div class="controls">
                <div class="form-control-static">
                    <ul id="upgradeList">
                        <li class="item list-template-item">
                            <div class="dropdown dropdown-hover">
                                <a class="dropdown-toggle" data-toggle="dropdown">
                                    {_number}、更新版本：{version}，发布时间：{upgradeDate} 
                                    <span class="caret"></span>
                                </a>
                                <div class="dropdown-menu item-detail" role="menu">
                                    <div style="width:550px">
                                        <p>v{version}更新说明:</p>
                                        <p>{introduction}</p>
                                    </div>
                                </div>
                            </div>
                        </li>
                    </ul>
                </div>
                <span class="help-block"></span>
            </div>
        </div>


        <div class="form-group postarea">
            <div class="controls">
                <button type="button" class="btn btn-sm btn-primary" id="btnCheckUpgrade">检测更新</button>
                <button type="button" class="btn btn-sm btn-primary hidden" id="btnUpgrade">开始升级</button>
                <div class="form-control-static" id="upgradeProcessBox" style="max-height:400px;overflow:auto">
                    <ul id="upgradeProcess">
                    </ul>
                </div>
                <span class="help-block"></span>
            </div>
        </div>

        @*<div class="form-group">
                <label class="control-label">升级进度</label>
                <div class="controls">

                    <span class="help-block"></span>
                </div>
            </div>*@
    </form>
    <div class="alert alert-warning alert-dismissible" role="alert">
        <strong><i class="fa fa-info-circle"></i>说明</strong>
        <p>1、尽量在访客少时候升级，升级过程中请不要关闭浏览器。</p>
        <p>2、升级过程中会导致网站进程重启。</p>
    </div>

</div>
<script type="text/javascript">
    function LoadedInit()
    {
        $(".dropdown-hover").DropdownHover();
    }
    var upgradeList;
    var $step = $("#step");
    var $btnCheckUpgrade = $("#btnCheckUpgrade");
    var $btnUpgrade = $("#btnUpgrade");
    var $upgradeList = $("#upgradeList");
    var $upgradeProcess = $("#upgradeProcess");
    var $upgradeProcessBox = $("#upgradeProcessBox");
    var $lbIslastversion = $("#lbIslastversion");
    $btnCheckUpgrade.click(function () {
        $btnCheckUpgrade.attr("disabled", "disabled").text("检测中...");
        ChangeStep(0);
        Loading();
        Ajax({ url: "@Url.Action("GetUpgradeList")", async: true }, function (data) { GetUpgradeList(data); }, function (httpRequest, textStatus) { alert("http" + httpRequest.status + "错误:" + httpRequest.responseText + "/" + textStatus) });

    });

    function GetUpgradeList(data)
    {
        if (data == "")
        {
            $lbIslastversion.removeClass("hidden");
            $btnCheckUpgrade.text("检测完毕");
        }
        else
        {
            upgradeList = JsonParse(data);
            if (upgradeList.length == 0) {
                $lbIslastversion.removeClass("hidden");
                $btnCheckUpgrade.text("检测完毕");
            }
            else {
                $(".upgradeListBox").removeClass("hidden");
                $upgradeList.ListBind({ showFooter: false, callBack: "LoadedInit" }, upgradeList);
                $btnCheckUpgrade.addClass("hidden");
                $btnUpgrade.removeClass("hidden");
            }
        }
        CloseLoading();
    }


    $btnUpgrade.on("click", function () {
        DownLoad();
    });

    //改变状态
    function ChangeStep(index)
    {
        var $stepItem = $step.children("li");
        $stepItem.eq(index).addClass("current").siblings().removeClass("current");
    }
    //写进度
    function WriteProcess(msg)
    {
        $upgradeProcess.append("<li>" + msg + "<li>")
        var height = $upgradeProcessBox[0].scrollHeight;
        $upgradeProcessBox.scrollTop(height);
    }

    var dataLength=0;
    var hasError = false;

    //下载文件
    function DownLoad()
    {
        Loading();
        dataLength = upgradeList.length;
        $btnUpgrade.attr("disabled", "disabled").text("升级中...");
        ChangeStep(1);
        DownStep(0);//递归请求
    }


    function DownStep(idx)
    {
        if (hasError) {return;}
        if (idx>=dataLength)
        { 
          RestartApplication();
          return;
        }
        var jsonData = upgradeList[idx];
        var version = jsonData.Version;
        var package = jsonData.Package;
        WriteProcess("开始下载" + version+ "版本升级包...")
            $.ajax({
                url:"@Url.Action("DownLoad")",
                type: "post",
                data: { version: version, package: package},
                dataType: "json",
                success: function (result) {
                    var result = JsonParse(result);
                    if (result.State ==1)
                    {
                        WriteProcess("" + version + "升级包下载完毕");
                        idx++;
                    }
                    else
                    {
                        WriteProcess(result.Msg);
                        hasError = true;
                        $btnUpgrade.attr("disabled", "disabled").text("升级出错");
                    }
                    if (hasError) {
                        CloseLoading();
                        return;
                    }
                    else {
                        setTimeout(function () { DownStep(idx); }, 1000);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    // 状态码
                    console.log(XMLHttpRequest.status);
                    // 状态
                    console.log(XMLHttpRequest.readyState);
                    // 错误信息
                    console.log(textStatus);
                    CloseLoading();
                }
            });
    }


        //重启进程
    function RestartApplication()
    {
        Loading();
        ChangeStep(2);
        WriteProcess("开始重启网站进程...");
        var hasError = false;
        $.ajax({
                url:"@Url.Action("RestartApplication")",
                type: "post",
                dataType: "json",
                success: function (result) {
                    var result = JsonParse(result);
                    if (result.State ==1)
                    {
                        WriteProcess("网站重启完毕");
                    }
                    else
                    {
                        WriteProcess(result.Msg);
                        hasError = true;
                    }
                    CloseLoading();
                    if (hasError) {
                        return;
                    }
                    Visitor();
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    // 状态码
                    console.log(XMLHttpRequest.status);
                    // 状态
                    console.log(XMLHttpRequest.readyState);
                    // 错误信息
                    console.log(textStatus);
                    WriteProcess(textStatus)
                    hasError = true;
                    CloseLoading();
                }
            });
    }


    //重启后发送第一次请求
    function Visitor()
    {
        Loading();
        ChangeStep(3);
        WriteProcess("开始更新升级文件...");
        var hasError = false;
        $.ajax({
                url:"@Url.Action("Visitor")",
                type: "post",
                dataType: "json",
                success: function (result) {
                    var result = JsonParse(result);
                    if (result.State ==1)
                    {
                        WriteProcess("文件更新完毕");
                    }
                    else
                    {
                        WriteProcess(result.Msg);
                        hasError = true;
                    }

                    CloseLoading();
                    if (hasError) {
                        return;
                    }
                    ChangeStep(4);
                    $btnUpgrade.attr("disabled", "disabled").text("升级完毕");
                    WriteProcess("<span class=\"text-success\">已经升级到最新版本^_^</span>");
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    // 状态码
                    console.log(XMLHttpRequest.status);
                    // 状态
                    console.log(XMLHttpRequest.readyState);
                    // 错误信息
                    console.log(textStatus);
                    WriteProcess(textStatus)
                    hasError = true;
                    CloseLoading();
                }
            });
    }
</script>




