<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title><!--{$UUDEV_PAGE_TITLE}--></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--{include file=elements/header.css.html}-->
<!--{include file=elements/header.js.html type=common}-->
<!--{include file=elements/header.js.html type=form}-->
<!--{include file=elements/header.js.html type=popbox}-->
<!--{include file=elements/header.js.html type=valid}-->

<script src="js/jquery/jquery.blockUI.js"></script>
</head>
<body> 
<div id="outside">
	<!--{include file=frame/top.html}-->
	<div id="mainareaContent">
		<!--{include file=frame/left.html}-->
		<div id="rightContent">
			<div id="topContent">
				<div class="sitmap">
					<span class="r"><!--{include file="elements/uuadvad.html"}--></span>网站管理&nbsp;&gt;&nbsp;网站备份&nbsp;&gt;&nbsp;数据库备份
				</div>
			</div>
			<div class="tabcontent">
				<a href="index.php?r=Database.backup" class="nolink">数据库备份</a>
				<a href="index.php?r=Database.restore" class="link">数据库还原</a>
			</div>
			<div id="secrightContent">
				<form id="theForm">
					<div class="tableContent">
						<table class="tbl_edit" width="100%">
							<tr style="display:none;">
								<td width="230px">使用扩展插入(Extended Insert)方式</td>
								<td><input type="radio" name="ext_insert" class="radio" value='1'>是&nbsp;&nbsp;<input type="radio" name="ext_insert" class="radio" value='0' checked="checked">否</td>
							</tr>
							<tr>
								<td width="230px"> 分卷备份&nbsp;--&nbsp;文本大小限制</td>
								<td><input type="text" name="vol_size" value="<!--{$vol_size}-->">KB<img class="tiptip" title="以此长度字节大小作为一个备份分卷文本的大小" src="images/quest.gif"/></td>
							</tr>
							<tr>
								<td>备份文件名</td>
								<td><input type="text" name="sql_file_name" value="<!--{$sql_name}-->"></td>
							</tr>
							<tr>
								<td class="noborder"><input type="hidden" name="act" value="dumpsql"></td>
								<td class="noborder"><button type="submit" class="but4font">开始备份</button></td>
							</tr>
						</table>
					</div>
					<div class="tableContent4">
						<div class="title">选择备份表</div>
						<table width="100%" cellpadding="0" cellspacing="0" border="0">
							<tr>
								<td height="30"  colspan="4"><input name="chkall" onClick="checkall(this.form, 'customtables[]')" type="checkbox"><b>全部选择</b></td>
							</tr>
							<tr>
								<!--{foreach from=$tables item=table name=table_name}-->
									<!--{if $smarty.foreach.table_name.iteration > 1 and ($smarty.foreach.table_name.iteration-1) % 4 eq 0}-->
									</tr><tr>
									<!--{/if}-->
									<td height="30" width="25%"><input name="customtables[]" value="<!--{$table}-->" type="checkbox">&nbsp;&nbsp;<!--{$table}--></td>
								<!--{/foreach}-->
							</tr>	
						</table>
					</div>
					<input type="hidden" id="backup_time" value="<!--{$backup_time}-->"/>
				</form>
			</div>
		</div>
	</div>
</div>
</body>
<script type="text/javascript">
var maxtime = 5 //一个小时，按秒计算，自己调整!  
function CountDown(){  
 if(maxtime>0){   
  	msg = maxtime+"秒";  
 	$("#timer").html(msg);    
 	--maxtime;  
 }else{  
 	clearInterval(timer); 
	window.location.href = './index.php?r=Database.restore';  
 }  
}  
 
 
function checkall(frm, chk){
    for (var i = 0; i < frm.elements.length; i++) {
		if (frm.elements[i].name == chk) {
			frm.elements[i].checked = frm.elements['chkall'].checked;
		}
	}
}

function showResponse(data, statusText, xhr, $form)
{
	//alert(data);
	if(data.status==-1)
	{
		if(data.one==1){
			$("#progress").html('<span>备份文件'+data.name+'.sql无法写入。</span>');
		}else{
			$("#progress").html('<span>备份文件'+data.name+'_'+data.vol+'.sql无法写入。</span>');
		}
	}else if(data.status==1){
		if(data.next!=1)
		{
			if(data.one==1)
			{
				$("#progress").html('<span>'+data.name+'.sql备份成功！</span><br><span><img src="images/spinner.gif"/>页面正在跳转...</span> <span id="timer"></span>');
			}else{
				$('<br><span>'+data.name+'_'+data.vol+'.sql备份成功！</span><br><span><img src="images/spinner.gif"/>页面正在跳转...</span> <span id="timer"></span>').appendTo("#progress");
			}
			timer = setInterval("CountDown()",1000); 
		}else{
			if(data.vol==1)
			{
				$("#progress").html('<span>'+data.name+'_1.sql备份成功！</span>');
			}else{
				$("#progress").append('<br><span>'+data.name+'_'+data.vol+'.sql备份成功！</span>');
			}
			
			nextback(data.lnk);
		}
	}
}

function  nextback(lnk)
{
	$.get(lnk,{},function(data){
		if(data.status==-1)
		{
			if(data.one==1){
				$("#progress").html('<span>备份文件'+data.name+'.sql无法写入。</span>');
			}else{
				$("#progress").html('<span>备份文件'+data.name+'_'+data.vol+'.sql无法写入。</span>');
			}
		}else if(data.status==1){
			if(data.next!=1)
			{
				if(data.one==1)
				{
					$("#progress").html('<span>'+data.name+'.sql备份成功！</span><br><span><img src="images/spinner.gif"/>页面正在跳转...</span> <span id="timer"></span>');
				}else{
					$('<br><span>'+data.name+'_'+data.vol+'.sql备份成功！</span><br><span><img src="images/spinner.gif"/>页面正在跳转... </span><span id="timer"></span>').appendTo("#progress");
				}
				timer = setInterval("CountDown()",1000); 
			}else{
				if(data.vol==1)
				{
					$("#progress").html('<span>'+data.name+'_1.sql备份成功！</span>');
				}else{
					$("#progress").append('<br><span>'+data.name+'_'+data.vol+'.sql备份成功！</span>');
				}
				
				nextback(data.lnk);
			}
		}
	},'json');
}

$(document).ready(function() {
	var options = { 
        //target:        '#output2',   // target element(s) to be updated with server response 
        url:"./index.php?r=database.cmd_backup",
		type:"post",
		dataType:'json',
        //beforeSubmit:  validate,  // pre-submit callback 
        success:       showResponse  // post-submit callback 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
 
    // bind to the form's submit event 
    $('#theForm').submit(function() { 
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
		var _this = $(this);
		var frm = document.getElementById("theForm");
		var j = 0;
		var vol_size = 1;
		var sql_file_name=1;
		for (var i = 0; i < frm.elements.length; i++) {
			if (frm.elements[i].name == 'customtables[]') {
				if (frm.elements[i].checked) {
					j++;
				}
			}
			if (frm.elements[i].name == 'vol_size' && frm.elements[i].value == "") {
				vol_size = 0;
			}
			if (frm.elements[i].name == 'sql_file_name' && frm.elements[i].value == "") {
				sql_file_name = 0;
			}
		}
		if (j < 1) {
			$.errorUI({
				message:"你还没有选择要备份的表！",
				showOverlay:true
			});
			return false;
		}
		if (vol_size < 1) {
			$.errorUI({
				message:"文件长度限制不能为空！",
				showOverlay:true
			});
			return false;
		}
		if (sql_file_name < 1) {
			$.errorUI({
				message:"备份文件名不能为空！",
				showOverlay:true
			});
			return false;
		}else{
	        $.confirmUI({
				message:"你确定要备份吗？",
				confirm:function(){
					
					$('#confirmUI-body').empty().html('<div id="progress"><span><img src="images/spinner.gif"/>开始备份...</span></div> ');
					_this.ajaxSubmit(options); 
				},
				showOverlay:true,
				cancel:function(){setTimeout($.unblockUI, 300);}
			});
			return false;
		}
        
 
        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false; 
    }); 
}); 
</script>
</html>