
<!--{template 'head'}-->
<script type="text/javascript">
function btnDisabled(status){
	Each(['dump','repair','optimize'], function(){
		$('btn_'+this.toString()).disabled = status
	});
}
function database(ac){
	if(ac == undefined) return;
	if(arguments.length == 1){
		var tables = getInputIds('datatable');
		if(tables.length == 0){
			alert("请选择需要操作的数据表");
			return;
		};
	}else{
		var tables = [arguments[1]];
	}
	btnDisabled(true);
	var page = 1;
	var limit = 0;
	var table = tables.shift();
	var start = 1;
	(function(){
		var url = "plugin.php?plugin=database&action="+ac+"&page="+page.toString()+"&table="+table+"&limit="+limit+"&start="+start;
		start = 0;
		var A = arguments;
		var msg = '';
		switch(ac){
			case 'dump':
				msg = '<p class="loading">数据表&lt;'+table+'&gt;备份中（<font color="green">'+page+'</font>），请等待...</p>';
			break;

			case 'repair':
				msg = '<p class="loading">数据表&lt;'+table+'&gt;修复中，请等待...</p><p style="padding-left:24px;">数据表内容较多时，可能会占用较多时间</p>';
			break;

			case 'optimize':
				msg = '<p class="loading">数据表&lt;'+table+'&gt;优化中中，请等待...</p>';
			break;
		}
		qmeCms.dialogView.form("数据库管理", msg, {width:400,type:'fixed'});
		Ajax.request(url,{method:'get',onComplete: function(resp){
			$('ids_'+table).checked = false;
			removeClassName($('item-'+table) ,'item-hover');
			removeClassName($('item-'+table) ,'item-click');
			if(tables.length == 0){
				$('chkall').checked = false;
				btnDisabled(false);
				qmeCms.dialogView.form("数据库管理", "<p>操作完毕</p>", {width:400,type:'fixed'});
				window.location.href = window.location.href.split('#')[0];
				return;
			}
			var json = evaljson(resp);
			if(json != null && json['status'] == 200){
				if(typeof json.page == 'undefined' || json.page >= json.pages){
					table = tables.shift();
					page = 1;
					limit = 0;
					A.callee();
				}else{
					limit = json['limit'];
					page++;
					A.callee();
				}
			}else{
				if(typeof json['msg'] != 'undefined') alert(json['msg']);
				table = tables.shift();
				page = 1;
				limit = 0;
				A.callee();
			}
			return true;
		}});
	})();
};

function loadDatabaseInfo(name){
	qmeCms.dialogView.form("数据库管理", '<p class="loading">数据初始化中，请等待...</p>', {width:400,type:'fixed'});
	var url = "plugin.php?plugin=database&action=load_init&name="+name;
	Ajax.request(url,{method:'get',onComplete: function(resp){
		var json = evaljson(resp);
		if(json != null && json['status'] == 200){
			if(json['data'].length == 0){
				alert("未发现可用数据");
				return false;
			}else{
				loadDatabase(json['data'], name);
			}
		}else{
			alert("出了点问题，请刷新重试");
		}
	}});
}

function loadDatabase(data, name){
	if(data.length == 0){
		qmeCms.dialogView.form("数据库恢复", '<p class="padding">数据恢复完成</p>', {width:400,type:'fixed'});
		window.location.href = window.location.href.split('#')[0];
		return;
	}
	var item = data.shift();
	(function(info){
		var A = arguments;
		if(A.length == 2){
			var index = A[1];
		}else{
			var index = 1;
		}
		if(index > info['max']){
			loadDatabase(data, name);
			return;
		}
		var index = arguments.length == 2 ? arguments[1] : 1;
		var url = "plugin.php?plugin=database&action=load&name="+name+"&table="+info['table']+'&index='+index;
		qmeCms.dialogView.setMessage("<p class='loading'>数据表&lt;"+info['table']+"&gt;恢复中（"+index.toString()+"/"+info['max']+"）,请等待....</p>");
		Ajax.request(url,{method:'get',onComplete: function(resp){
			A.callee(info, index+1);
		}});
	})(item);
}

function showDatabaseInfo(date){
	qmeCms.dialogView.form("查看数据库"+date+"备份","",{width:600,type:'abs'});
	var url = "plugin.php?plugin=database&action=info&name="+date;
	Ajax.request(url,{method:'get',onComplete: function(resp){
		var json = evaljson(resp);
		if(json['status'] == 200){
			var data = json['data'];
			var html = ["<table border='0' width='100%'><tbody>"];
			for(var i=0;i<data.length;i++){
				html.push("<tr>");
				html.push("<td class='tablerowl' width='50%' style='text-align:right;padding:5px 15px;'>"+data[i]['name']+"</td>");
				html.push("<td class='tablerow'>"+data[i]['size']+"</td>");
				html.push("</tr>");
			}
			html.push("</tbody></table>");
			qmeCms.dialogView.setMessage(html.join(''));
		}else{
			qmeCms.dialogView.setMessage("服务异常，请刷新浏览器重试");
		}
	}});
}
</script>
<table cellpadding="0" cellspacing="1" border="0" align=center class="tableborder border" width="100%">
	<tr>
		<td class="tablerowhighlight" width="160">备份日期</td>
		<td class="tablerowhighlight">管理</td>
	</tr>
	<!--{if $data}-->
	<!--{loop $data $val}-->
	<tr>
		<td class="tablerow" align="center">{$val}</td>
		<td  class="tablerow" style="padding-left:15px;"><a href="###" onclick="showDatabaseInfo('{$val}')">查看备份数据</a> | <a href="###" onclick="loadDatabaseInfo('{$val}')">从该备份恢复数据</a> | <a href="plugin.php?plugin=database&action=remove&name={$val}" onclick="return makeSure()">删除备份</a></td>
	</tr>
	<!--{/loop}-->
	<!--{else}-->
	<tr>
		<td class="tablerow" align="center" colspan="2"><p style="padding:25px;">还没有备份数据</p></td>
	</tr>
	<!--{/if}-->
</table>

<form action="plugin.php?plugin=database&action=manage" method="post" onsubmit="return false;">
<table cellpadding="0" cellspacing="1" border="0" align=center class="tableborder border mt5" width="100%" id="datatable">
	<tr>
		<td class="submenu" align=center width=100% height=26 colspan="7"> 在线备份 </td>
	</tr>
	<tr>
		<td class="tablerowhighlight" width="30">&nbsp;</td>
		<td class="tablerowhighlight" >表名</td>
		<td class="tablerowhighlight" width="80">引擎类型</td>
		<td class="tablerowhighlight" width="80">记录数</td>
		<td class="tablerowhighlight" width="80">大小</td>
		<td class="tablerowhighlight" width="80">多余</td>
		<td class="tablerowhighlight" width="140">更新时间</td>
	</tr>
	<!--{loop $tables $key $val}-->
	<tr class="item" id="item-{$val['Name']}">
		<td class="tablerowl"><input type="checkbox" value="{$val['Name']}" name="tables[]" id="ids_{$val['Name']}" /></td>
		<td style="padding-left:12px;"><!--{if strpos($val['Name'], TABLEPRE) === 0}-->{$val['Name']}<!--{else}--><font color="green" title="非QmeCMS系统数据表">{$val['Name']}</font><!--{/if}--></td>
		<td align="center">{$val['Engine']}</td>
		<td align="center">{$val['Rows']}</td>
		<td align="center">{echo GetSizeUnit($val['Data_length'] + $val['Index_length'])}</td>
		<td align="center">{echo GetSizeUnit($val['Data_free'])}</td>
		<td align="center">{$val['Update_time']}</td>
	</tr>
	<!--{/loop}-->
	<tr>
		<td class="tablerowl" align=center>
			<input name='chkall' type='checkbox' id='chkall' onclick='checkall(this.form)' value='checkbox'>
		</td>
		<td colspan="6" class="tablerow" style="">
			<input type="button" class="f-button" value=" 备份数据库 " onclick="database('dump')" id="btn_dump" />
			<input type="button" class="f-button" value=" 优化数据表 " onclick="database('optimize')" id="btn_optimize" />
			<input type="button" class="f-button" value=" 修复数据表 " onclick="database('repair')" id="btn_repair" />
		</td>
	</tr>
</table>
</form>
<script type="text/javascript">
gridTableEvent('datatable');
</script>
<!--{template 'foot'}-->
