<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>刀客建站系统   管理中心  - PAD 数据导入 </title>
<%{include file="header.html"}%>
</head>
<body>
<h1>
<span class="action-span1"><a href="index.php?act=main">刀客建站系统   管理中心 </a>  - PAD 数据导入 &nbsp;&nbsp;<a href="index.php?act=help.get&h=pad&width=450"   class="jTip"  name="我希望获得更多帮助"><img src='images/help.png'></a> </span>
<span style='float:right;margin-right:10px;'><img src='images/top_help.gif'><a href="http://sitepub.cn/help"> 帮助 </a></span>
<span style='float:right;margin-right:10px;'><img src='images/top_forum.gif'><a href="http://bbs.sitepub.cn"> 论坛 </a></span>
<div style="clear:both"></div>
</h1>
  <div style="background:#FFF; padding: 20px 50px; margin: 2px;">
		<div>
		选择分类 &nbsp;&nbsp;
	 			<select id='cat_id'>
					<%{foreach from=$out.category_res item=c_v key=c_k}%>
					<option value="<%{$c_v.parent.cat_id}%>" ><%{$c_v.parent.cat_name|escape:html}%></option>
						<%{foreach from=$c_v.children item=c_v_c key=c_k_c}%>			
						<option value="<%{$c_v_c.cat_id}%>" >&nbsp;&nbsp;&nbsp;&nbsp;<%{$c_v_c.cat_name|escape:html}%></option>
						<%{/foreach}%>
					<%{/foreach}%>
					</select><span class="require-field">*</span> <span ><a href="javascript:void(0)" onclick='alert_cat_box();' >添加新分类</a></span>   <br><br> 
	 	</div>	
		<div style='float:left;margin-right:20px;'> 
			<table align="center" width="400" >
			<tr><td><b>上传文件</b></td></tr>
			<tr>
	 		<td><input type='file' name='pad_file_name'></td></tr>
			<tr><td>
				<a href="javascript:void(0);" onclick="del_tr(this)"><img src="images/ico-del.gif" style="border:0;vertical-align:middle" /></a> &nbsp;<input type='file' name='pad_file_name'>
			</td></tr>
				<tr><td>
				<a href="javascript:void(0);" onclick="del_tr(this)"><img src="images/ico-del.gif" style="border:0;vertical-align:middle" /></a> &nbsp;<input type='file' name='pad_file_name'>
			</td></tr>
  		    <tr id='add_link' style=" height:30px; line-height:30px;"><td>
  	          <a href='javascript:void(0);' onclick='add_tr()'><img src="images/ico-add.gif" style="border:0; vertical-align:middle;" />&nbsp;添加</a>
			</td></tr>	
			<tr><td><b>外部链接 ( 一行一个链接 ) </b></td></tr>
			<tr><td><textarea style='height:600px;width:500px;' id='padlink_box'></textarea>
			</td></tr>
			</table>
			<div><input type="submit" class="button" id='dump_button' value=" 开始导入 " onclick='start_dump_pad()'/></td></tr></div>
	</div>
	<div id='message_box' style='overflow-y:auto;height:800px;overflow-x:hidden;' >
	</div>
</div>
<%{include file="add_category_layer.html"}%>
<script>
function add_tr()
{
	var tr_html	=	'<tr><td><a href="javascript:void(0);" onclick="del_tr(this)"><img src="images/ico-del.gif" style="border:0;vertical-align:middle" /></a>&nbsp;<input type="file" name="pad_file_name"></td></tr>	';
	$(tr_html).insertBefore("#add_link");
}
function del_tr(obj)
{
		$(obj).parent().parent().remove();
}
function start_dump_pad()
{
	$("#dump_button")[0].disabled	=	true;
	$("#dump_button")[0].value		=	'正在导入...';
	$("#message_box")[0].scrollIntoView();
	pad_complete	=	dump_pad();
	var s	=	setInterval(function(){  
		if(!pad_complete){
			alert('PAD导入完成');	
			$("#dump_button")[0].disabled	=	false;
			$("#dump_button")[0].value		=	'开始导入';
			clearInterval(s);
		}
	},1000);	
}
function dump_pad()
{
	tmp_links_arr		=	$("#padlink_box").val().split("\n");		
	var c		=	0;
	var st	=	1;
	var s		=	tmp_links_arr.length;
	if(s>100){
		st	= 	parseInt(parseInt(s/10)*st);
	}	
	for(i in tmp_links_arr){
		var pad_link	=	$.trim(tmp_links_arr[i]);				
		if(!pad_link){
			continue;
		}	
		c++;
		var fun_send_ajax_pad_link	=	send_ajax_pad_link(pad_link);
		setTimeout(fun_send_ajax_pad_link,st);
	}
	$(":file").each(function(i){
		if(this.name.match("pad_file_name")){
			var pad_file_obj	=	$(this);				
			if(!pad_file_obj.val()){
				return true;
			}
			pad_file_obj.attr("id","pad_file_"+i); 	
			c++;
			pad_file_id	=	"pad_file_"+i;	
			var fun_send_ajax_pad_file		=	send_ajax_pad_file(pad_file_id)
			setTimeout(fun_send_ajax_pad_file,1);
		}
	});
	return c;	
}

function send_ajax_pad_link(pad_link)
{
	return (function(){
	 $.ajax({
				url			:	'index.php?act=soft.save_addpad&is_link=1&is_ajax=1',	
				type			:	'POST',
				data			:	'pad_link='+pad_link+"&cat_id="+$("#cat_id").val(),
				dataType		:	'json',
				success		:	function(json){
					if(json['message']){
						if(json['type']=='success'){
							mess	=	'<p><b style="color:blue;">'+json['message']+'</b><br>&nbsp;&nbsp;'+json['message_info']+'<br></p>';
						}else{
							mess	=	'<p><b style="color:red;">'+json['message']+'</b><br>&nbsp;&nbsp;'+json['message_info']+'<br></p>';
						}
						$("#message_box").html($("#message_box").html()+mess);
					}
					pad_complete--;
				},
				error			:	function(){
					pad_complete--;
				}
			});	
	});
}

function send_ajax_pad_file(pad_file_id)
{
	return (function(){
					$.ajaxFileUpload({
					url				:	'index.php?act=soft.save_addpad&is_file=1&is_ajax=1&cat_id='+$("#cat_id").val(),	
					secureuri		:	false,
					fileElementId	:	pad_file_id,
					dataType			:  'json',
					success			: function(data, status){
						if(data){
							if(data['type']!='error'){
								mess	=	'<p><b style="color:blue;">'+data['message']+'</b><br>&nbsp;&nbsp;'+data['message_info']+'<br></p>';
							}else{
								mess	=	'<p><b style="color:red;">'+data['message']+'</b><br>&nbsp;&nbsp;'+data['message_info']+'<br></p>';
							}
							$("#message_box").html($("#message_box").html()+mess);
						}
						pad_complete--;
					},
					error: function (data, status, e){
						pad_complete--;
					}
				});		
			});
}


</script>

</body>
</html>
