<rdoc:include file="head.htm" />

<!-- BEGIN PAGE CONTENT-->			
<div class="row">
		<div class="col-md-12">
			
			<div class="portlet box default">
						<div class="portlet-title">
							<div class="caption">
								<i class="fa fa-gift"></i>备份与恢复
							</div>
							<div class="tools">
								<a href="javascript:;" class="collapse">
								</a>
								<a href="#" data-toggle="modal" class="refresh">
								</a>
							</div>
						</div>
						<div class="portlet-body">
						
							<ul class="nav nav-tabs">
								<li class="$navtab0[active]" >
									<a href="#tab_1_0" data-toggle="tab" data-id="$navtab0[id]">
									@t[Backup] </a>
								</li>
								<li class="$navtab1[active]" >
									<a href="#tab_1_1" data-toggle="tab" data-id="$navtab1[id]">
									@t[Restore] </a>
								</li>	
								
								<li class="$navtab2[active]" >
									<a href="#tab_1_2" data-toggle="tab" data-id="$navtab2[id]">
									@t[BackupList] </a>
								</li>							
							</ul>
							<div class="tab-content">
								<div class="tab-pane fade $navtab0[in]" id="tab_1_0">
								    <form name="ruiform1" id="ruiform1">
							        <div class="table-container">
                                    <div class="table-toolbar">
								        <div class="row ">
        									
									        <div class="col-md-12">
										        <div class="btn-group pull-right">
										        <div class="form-actions">
											        <button type="button" class="btn blue startBackup" id="Button2">
																        <i class="fa  fa-copy "></i>
																        <span>
																        @t[Backup] </span>
																        </button></div>
										        </div>
									        </div>
								        </div>
							        </div>

								<table class="table table-striped table-bordered table-hover">
									<thead>
									<tr role="row" class="heading">
										<th>
											<input type="checkbox" class="group-checkable checkall" value="checkall">
										ID
										</th>
										<th>
											
											MODEL

										</th>
										<th>
											总记录
										</th>
										
										<th>
											已备记录
										</th>

										<th width="150">
											 操作
										</th>
									</tr>
									</thead>
									<tbody>
									<!--# foreach($udb as $key=>$v) { #-->		
									<tr>
										<td>
											<input type="checkbox" class="modelchk" name="params[ids][]" value="$v[id]">
											 $v[id]
										</td>

										<td>
											 $v[name]
										</td>
										<td>
											 $v[rows]
										</td>
										
										<td>
											 <span id="backup_status_$v[id]"></span>
										</td>

										<td>
											<button type="button" class="btn green btn-xs btn-circle edit tooltips " data-original-title="修改" href="#">
											<i class="icon-wrench " ></i></button>
										</td>
									</tr>
									<!--# } #-->

									
									</tbody>
									</table>
								
								<div class="table-toolbar">
								        <div class="row ">
        									
									        <div class="col-md-12">
										        <div class="btn-group">
										        <div class="form-actions">
											        <button type="button" class="btn blue startBackup" id="Button1">
																        <i class="fa  fa-copy "></i>
																        <span>
																        @t[Backup] </span>
																        </button></div>
										        </div>
									        </div>
								        </div>
							        </div>
							        	

								</div>
								</form>

								</div>
								<div class="tab-pane fade $navtab1[in]" id="tab_1_1">
								<form name="ruiform2">
								    <div class="table-container">
								        <div class="table-toolbar">
								            <div class="row ">
            									
									            <div class="col-md-12">
										            <div class="btn-group  pull-right">
										            <div class="form-actions">
											            <button type="button" class="btn blue startRestore" id="Button3">
																            <i class="fa fa-import"></i>
																            <span>
																            @t[Restore] </span>
																            </button>
																            
                                                        <button type="button" class="btn red deleteall" id="Button5">
																            <i class="fa fa-trash-o"></i>
																            <span>
																            清理 </span>
																            </button></div>
										            </div>
									            </div>
								            </div>
							            </div>
							        

								    

								<table class="table table-striped table-bordered table-hover">
									<thead>
									<tr role="row" class="heading">
										<th>
											<input type="checkbox" class="group-checkable checkall" value="checkall">
										ID
										</th>
										<th>
											
											MODEL

										</th>
										<th>
											记录
										</th>
										<th>
											文件大小
										</th>
										
										<th>
											备份时间
										</th>
										
										<th>
											恢复状态
										</th>

										<th width="150">
											 操作
										</th>
									</tr>
									</thead>
									<tbody>
									<!--# foreach($fdb as $key=>$v) { #-->		
									<tr>
										<td>
											<input type="checkbox" class="modelchk" name="params[id][]" value="$v[id]">
											 $v[id]
										</td>

										<td>
											 $v[modelname]
										</td>
										
										<td>
											 $v[rows]
										</td>
										
										<td>
											 $v[size]
										</td>
										
										<td>
											 $v[_atime]
										</td>
										
										<td>
											 <span id="restore_status_$v[id]"></span>
										</td>

										<td>
											<button type="button" class="btn blue btn-xs restoreModel tooltips " data-original-title="恢复" href="#" data-id="$v[id]">
											<i class="fa fa-import" >恢复</i></button>
											
											<button type="button" class="btn red btn-xs tooltips delRestore" data-original-title="删除" href="$_base/delrestore?id=$v[id]" 
											    data-id="$v[id]" >
											<i class="fa fa-trash-o" ></i> 删除</button>
										</td>
									</tr>
									<!--# } #-->

									
									</tbody>
									</table>

								    </div>
								    </form>
								</div>
								
								<div class="tab-pane fade $navtab2[in]" id="tab_1_2">
								    <form name="ruiform3">
								    <input type="hidden" name="component" value="$component" />
								    <input type="hidden" name="task" value="simpleupload" />
							        <div class="table-container">
							        
                                    <div class="table-toolbar">
								        <div class="row ">
        									
									        <div class="col-md-12">
										        <div class="btn-group  pull-right">
										        <div class="form-actions">
											        
 <button type="button" class="btn green fileinput-button tileupload" 
	data-loading-text="Loading..." 
	data-style="zoom-in"
	data-url="$_base/simpleupload" 
	data-tpl="simpleupload" 
	data-uptype="bz|zip|gz" 
	data-nopreview="true" 
	data-donereload="true" 
	data-sbt="$sbt">
	<span>上传<input type="file" name="files[]" multiple="" class="inputfile"></span>
</button>

                                                </div>
										        </div>
									        </div>
								        </div>
							        </div>
							        
							        <table class="table table-striped table-bordered table-hover">
									    <thead>
									    <tr role="row" class="heading">
										    <th>
										    ID
										    </th>
										    <th>
    											
											    备份记录

										    </th>
										    <th>
    											
											    大小

										    </th>
										    <th>
    											
											    备份时间

										    </th>

										    <th width="150">
											     操作
										    </th>
									    </tr>
									    </thead>
									    <tbody>
									    <!--# foreach($bdb as $key=>$v) { #-->		
									    <tr>
										    <td>
											    $v[id]
										    </td>

										    <td>
											     $v[name]
										    </td>
										    
										    <td>
											     $v[size]
										    </td>
										    
										    <td>
											     $v[_atime]
										    </td>
										    

										    <td>
                                                 <a class="btn blue btn-xs tooltips " href="$_base/download?id=$v[id]">
											        下载
											     </a>
											     
											     <a class="btn blue btn-xs tooltips  " href="$_base/restore?id=$v[id]" onclick="return rui.ask('@t[Are you sure to restore?]');">
											        恢复
											     </a>
											     
											     <a class="btn red btn-xs tooltips delBackup "  href="$_base/delete?id=$v[id]" >
											        删除
											     </a>
										    </td>
									    </tr>
									    <!--# } #-->

    									
									    </tbody>
									    </table>
									

								</div>
								</form>

								</div>
							</div>
						</div>
					</div>
		</div>
</div>
<!-- END PAGE CONTENT -->
<script type="text/javascript">


var id_array = new Array();


function updateBackupStatus(id, status)
{
    $("#backup_status_"+id).html(status);
}

function showBackupStatus(res)
{
    //console.log(res);
    if (res.status < 0) {
        updateBackupStatus(res.data.id, "失败");
        return false;
    }
    var id = res.data.id;
    var nr = res.data.total - res.data.left;
    if (res.data.left > 0) { //未备份完
        updateBackupStatus(id, nr);
        var url = G.base+"/backout?id="+id+"&left="+res.data.left+"&wqlen="+id_array.length;
	    	$.getJSON(url, showBackupStatus);    
        return true;
    } else {
        updateBackupStatus(id, nr);
    }
        
    if (id_array.length>0)
    {
        id = id_array.shift();
        doStartBackup(id, id_array.length);
    } else {
         rui.showTSuccess('备份已完成');
         rui.setCookie('atid',2);
         rui.refresh(1);
    }
    
}


function doStartBackup(id, wqlen)
{
    updateBackupStatus(id, "处理中...");
    
    var url = G.base+"/backout?id="+id+"&wqlen="+wqlen+"&r="+Math.random();
		$.getJSON(url, showBackupStatus);
	
		return false;
}

function startBackup()
{
    for (var i=0;i<ruiform1.elements.length;i++)
	{
		var e = ruiform1.elements[i];
		if(e.type == 'checkbox')
		{
			if(e.checked == true)
			{
			    var ismodelchk = $(e).hasClass('modelchk');
			    if (ismodelchk)
			        id_array.push(e.value);
			}
		}
	}
	
	if (id_array.length == 0)
	{
	    return false;
	}
	
	id = id_array.shift();
	doStartBackup(id, id_array.length);
	
	return true;
}

/////////////////////////////////////////// Restore //////////////////////////////////////
function updateRestoreStatus(id, status)
{
    $("#restore_status_"+id).html(status);
}


function showRestoreStatus(res)
{
    //console.log(res);
    if (res.status < 0) {
        updateBackupStatus(res.data.id, "失败");
        return false;
    }
    var id = res.data.id;
    var nr = res.data.total - res.data.left;
    if (res.data.left > 0) { //未恢复完
        updateRestoreStatus(id, nr);
        var url = G.base+"/backin?id="+id+"&left="+res.data.left+"&wqlen="+id_array.length;
	    $.getJSON(url, showRestoreStatus);    
        return true;
    } else {
        updateRestoreStatus(id, nr);
    }
        
    if (id_array.length>0)
    {
        id = id_array.shift();
        doStartRestore(id, id_array.length);
    } else {
         rui.showTSuccess('恢复已完成');
         //rui.refresh(1);
    }
    
}


function doStartRestore(id, wqlen)
{
    updateRestoreStatus(id, "处理中...");
    
    var url = G.base+"/backin?id="+id+"&wqlen="+wqlen+"&r="+Math.random();
	$.getJSON(url, showRestoreStatus);
	
	return false;
}
function startRestore()
{
    for (var i=0;i<ruiform2.elements.length;i++)
	{
		var e = ruiform2.elements[i];
		if(e.type == 'checkbox')
		{
			if(e.checked == true)
			{
			    var ismodelchk = $(e).hasClass('modelchk');
			    if (ismodelchk)
			        id_array.push(e.value);
			}
		}
	}
	
	if (id_array.length == 0)
	{
	    return false;
	}
	
	id = id_array.shift();
	doStartRestore(id, id_array.length);
	
	return true;
}

function startRestoreOne(id)
{
    id_array = [];
    doStartRestore(id, 0);
}


var initBackup = function () {
    $('.startBackup').click(function(e){
        startBackup();
    });
    $('.startRestore').click(function(e){
        var nr = 0;
        for (var i=0;i<ruiform2.elements.length;i++)
	    {
		    var e = ruiform2.elements[i];
		    if(e.type == 'checkbox')
		    {
			    if(e.checked == true)
			    {
			        var ismodelchk = $(e).hasClass('modelchk');
			        if (ismodelchk)
			            nr ++;
			    }
		    }
	    }
    	
	    if (nr == 0)
	    {
	        return false;
	    }
        bootbox.confirm("@T[Are you sure to restore?]", function(result) {
            if (result) {
                startRestore();
            }
        }); 
    });
    
    $('.restoreModel').click(function(e){
        var id = $(this).attr('data-id');
        bootbox.confirm("@T[Are you sure to restore?]", function(result) {
            if (result) {
                
                startRestoreOne(id);
            }
        }); 
    });
    
    $('.delBackup').click(function(e){
        e.preventDefault();
        var url = $(this).attr('href');

        bootbox.confirm("@T[Are you sure to delete?]", function(result) {
            if (result) {
                $.getJSON(url, function(res) {
                    if (res.status == 0) {
                        rui.showTSuccess('操作成功.'); 
                        rui.reload();  
                    } else {
                        rui.showTError('操作失败:'+res.status);
                    }
                })
                .fail(function() { 
                     self.showTError("系统错误");
                });
            }
        });
    });
    
    
    $('.delRestore').click(function(e){
        e.preventDefault();
        var url = $(this).attr('href');

        bootbox.confirm("@T[Are you sure to delete?]", function(result) {
            if (result) {
                $.getJSON(url, function(res) {
                    if (res.status == 0) {
                        rui.showTSuccess('操作成功.'); 
                        rui.reload();  
                    } else {
                        rui.showTError('操作失败:'+res.status);
                    }
                })
                .fail(function(res) { 
                     self.showTError("系统错误");
                });
            }
        });
    });
    
}

$(document).ready(function() {  
    initBackup();
	$(".tileupload").bupload({
		autoupload:true,
	});	
	
});

</script>
<rdoc:include file="foot.htm" />
