<%@ Page Title="" Language="C#" MasterPageFile="~/Masters/Sys.Master" AutoEventWireup="true" CodeBehind="BuildSchedule.aspx.cs" Inherits="yueyaCMS.Admin.Page.BuildSchedule" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<style type="text/css">
    #PageTable tbody tr td a.btn { margin-right:5px;}
    b.s0 { display:block; width:16px; height:16px; background:url(/Images/s0.png) left top no-repeat; margin:auto;}
    b.s1 { display:block; width:16px; height:16px; background:url(/Images/s1.gif) left top no-repeat; margin:auto;}
    b.s2 { display:block; width:16px; height:16px; background:url(/Images/s2.png) left top no-repeat; margin:auto;}
    b.s3 { display:block; width:16px; height:16px; background:url(/Images/s3.png) left top no-repeat; margin:auto; cursor:pointer;}
    .progress { margin-bottom:0px;}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<ul class="breadcrumb">
<li><i class="icon-tags"></i> 您当前的位置：<a href="/Admin/Site/Default.aspx">系统首页</a></li>
<li><span class="divider">/</span> <a href="/Admin/Site/BuildPages.aspx">生成网站</a></li>
<li><span class="divider">/</span> 生成进度</li>
</ul>

<div class="alert alert-warning">
    <a href="javascript:void(0)" class="btn btn-warning" onclick="GetPageTasks(0)">查看全部</a>
</div>

<table id="TaskTable" class="table table-bordered table-hover" style=" margin-bottom:0px;">
<thead>
<tr>
<th width="60px">编号</th>
<th width="150px">模版名称</th>
<th>任务进度</th>
<th width="40px">状态</th>
<th width="150px">任务耗时</th>
<th width="150px">创建时间</th>
<th width="100">操作</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
            <table width="100%">
            <tr>
                <td><div class="bigPage vm" id="Pagenation" style="float:left;"></div></td>
                <td>&nbsp;&nbsp;&nbsp;&nbsp;<span id="pageTip"></span></td>
                
            </tr>
        </table>

    <div id="myModal" class="modal hide fade">
    <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h3>生成日志</h3>
    </div>
    <div class="modal-body">
    <p>One fine body…</p>
    </div>
    <div class="modal-footer">
    <a href="#" class="btn" onclick="$('#myModal').modal('hide');">关闭</a>
    </div>
    </div>

<script type="text/javascript">
    /*转换耗时*/
    function GetUsedTime(_s,_e) {
        var GetTimer = function (_timeString)
        {
            var dateArr = _timeString.split(" ");
            var ymdArr = dateArr[0].split("-");
            var mhsArr = dateArr[1].split(":");
            var yyyy = ymdArr[0]
            var MM = ymdArr[1]
            var dd = ymdArr[2];
            var HH = mhsArr[0];
            var mm = mhsArr[1];
            var ss = mhsArr[2];
            return new Date(yyyy,MM,dd,HH,mm,ss);
        }
        var BeginTime = GetTimer(_s);
        var CurrentTime = GetTimer(_e);
        var date3 = CurrentTime.getTime() - BeginTime.getTime();
        /*计算出相差天数*/
        var days = Math.floor(date3 / (24 * 3600 * 1000));
        /*计算出小时数*/
        var leave1 = date3 % (24 * 3600 * 1000);
        /*计算天数后剩余的毫秒数*/
        var hours = Math.floor(leave1 / (3600 * 1000));
        /*计算相差分钟数*/
        var leave2 = leave1 % (3600 * 1000);
        /*计算小时数后剩余的毫秒数*/
        var minutes = Math.floor(leave2 / (60 * 1000));
        /*计算相差秒数*/
        var leave3 = leave2 % (60 * 1000);
        /*计算分钟数后剩余的毫秒数*/
        var seconds = Math.round(leave3 / 1000)
        return (days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒");
    }

    /*生成进度条*/
    function ProgressFormat(td) {
        var Progress = parseFloat($(td).text());
        if (isNaN(Progress)) { Progress = 100.00;}
        if (Progress > 100) { Progress = 100.00;}
        var ProgressStyle = "";
        if (Progress < 20) { ProgressStyle = "progress-striped active"; }
        else if (Progress < 40) { ProgressStyle = "progress-success progress-striped active"; }
        else if (Progress < 60) { ProgressStyle = "progress-warning progress-striped active"; }
        else if (Progress < 80) { ProgressStyle = "progress-danger progress-striped active"; }
        else if (Progress < 100) { ProgressStyle = "progress-striped active"; }
        else if (Progress >= 100) { ProgressStyle = "progress-striped"; }
        $(td).html("<div title='"+$(td).attr("buildcount")+"/"+$(td).attr("totalcount")+"' class='progress " + ProgressStyle + "'><div class='bar' style='width:" + Progress + "%;'>" + Progress + "%</div></div>");
    }
    /*查看日志*/
    function ShowHistory(_ele)
    {
        $('#myModal').modal({ show: true, backdrop: false });
        var txt = "[" + $(_ele).parent().find("span").text() + "]";
        var ErrorMsg = eval(txt);
        var tmp = '<table class="table table-bordered table-hover"><thead><tr><th>时间</th><th>日志内容</th><th>模板ID</th><th>数据ID</th></tr></thead><tbody>';
        
        for (var i = 0; i < ErrorMsg.length; i++)
        {
            tmp += '<tr><td>' + ErrorMsg[i].Time + '</td><td>' + ErrorMsg[i].Msg + '</td><td><a href="/Admin/Page/Edit.aspx?id=' + ErrorMsg[i].PageId + '" target="_blank">' + ErrorMsg[i].PageId + '</a></td><td>' + ErrorMsg[i].DBID + '</td></tr>';
        }
        tmp += '</tbody></table>';
        
        $('#myModal .modal-body').html(tmp);
    }

    /*停止任务*/
    function StopTask(_TaskID)
    {
        API.Page_Task_Stop(function (r) { GetPageTasks(0); }, "{_TaskID:" + _TaskID + "}");
    }

    /*开始任务*/
    function StartTask(_TaskID)
    {
        API.Page_Task_Start(function (r) { GetPageTasks(0); }, "{_TaskID:" + _TaskID + "}");
    }


    $(function () {
        GetPageTasks(0);
        setInterval('GetPageTasks(0)', 10000);
    });

    /*获取页面生成任务列表*/
    function GetPageTasks(pageIndex)
    {
        API.Page_Task_Get(function (result) {
            if (result.dbList.length < 1) { return; }
            var Page_Temp = '{@each dbList as d,index}<tr id="Page_${d.ID}"><td>${++index}</td><td><a href="Edit.aspx?id=${d.PageID}">${d.PageName}</a></td><td class="progresstd" buildcount="${d.BuildCount}" totalcount="${d.TotalCount}">${(d.BuildCount/d.TotalCount*100).toFixed(2)}</td><td>{@if d.Status==0}<b class="s0"></b>{@else if d.Status==1}<b class="s1"></b>{@else if d.Status==2}<b class="s2"></b>{@else}未知{@/if}</td><td>${d.LastUpdateTime|DateFormat}</td><td>${d.CreateDate|DateFormat}</td><td>{@if d.Status==1}<a href="#" class="btn btn-mini btn-success" onclick="StopTask(${d.ID});">停止</a>{@else if d.Status==3}<a href="#" class="btn btn-mini btn-success" onclick="StartTask(${d.ID});">运行</a>{@else if d.BuildHistory.length>0}<span style="display:none;">${d.BuildHistory}</span><a href="#" onclick="ShowHistory(this);" class="btn btn-mini">日志</a>{@/if}</td></tr>{@/each}';
            $("#TaskTable tbody").html(juicer(Page_Temp, { dbList: result.dbList }));
            API.PageBuilder(result.pageCount, pageIndex, "#Pagenation", "GetPageTasks");
            $("#pageTip").text("当前第" + (pageIndex + 1) + "/" + result.pageCount + "页，每页20条。")
            $.each($("#TaskTable tbody tr"), function (a, b) {
                ProgressFormat($(b).find("td.progresstd"));

                var _s = $(b).find("td:eq(5)").text();
                var _e = $(b).find("td:eq(4)").text();
                $(b).find("td:eq(4)").text(GetUsedTime(_s, _e));
            });
            $(".progress").tooltip();
        }, "{pageIndex:" + pageIndex + ",showLength:20}");
    }
</script>
</asp:Content>