<!DOCTYPE html>
<html>
	<head>
		<meta charset="<?php echo $cfg_soft_lang; ?>">
		<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
		<title>搜索关键词管理</title>
		<link rel="stylesheet" href="../static/web/font/css/font-awesome.min.css">
		<link rel="stylesheet" href="../static/web/css/bootstrap.min.css">
		<link rel="stylesheet" href="../static/web/css/admin.css">
		<script src="../static/web/js/jquery.min.js"></script>
		<script src="../static/web/js/bootstrap.min.js"></script>
		<script src="js/key.js"></script>
		<script src="../static/web/js/webajax.js"></script>
		<script src="js/main.js"></script>
		<script>
		var pageno = 1;
		var totalrow = "<?php echo $totalRow?>";
		var pagesize = "<?php echo $pagesize?>";
		var orderby = "<?php echo $orderby?>";
		//加载列表
		function ReloadPage(ordertype) {
			orderby = ordertype;
			var listArea = $Obj('rslist');
			var errMsg = "网络通信出错，<a href=\"javascript:ReloadPage('" + ordertype + "')\">重新加载列表</a>";
			fetch("search_keywords_main.php?dopost=getlist&pageno=" + pageno + "&orderby=" + ordertype).then(resp=>{
				if (resp.ok) {
					return resp.text()
				}
				throw new Error(errMsg);
			}).then((d)=>{
				listArea.innerHTML = d;
			}).catch((error) => {
				listArea.innerHTML = errMsg;
			});
		}
		//载入指定页的列表
		function LoadPage(npage) {
			pageno = npage;
			ReloadPage(orderby);
			ReloadPageNum(pageno);
		}
		//更新一个关键词
		function UpdateNote(nid) {
			var listArea = $Obj('rslist');
			var kw = $Obj('keyword' + nid).value;
			var kws = $Obj('spwords' + nid).value;
			var ct = $Obj('count' + nid).value;
			var errMsg = "网络通信出错，<a href=\"javascript:LoadPage('" + pageno + "')\">重新加载列表</a>";
			<?php
			if (strtolower(substr($cfg_soft_lang, 0, 3)) == 'utf') {
			?>
			const formData = new FormData()
			formData.append('dopost', 'update');
			formData.append('aid', nid);
			formData.append('keyword', kw);
			formData.append('spwords', kws);
			formData.append('count', ct);
			formData.append('pageno', pageno);
			formData.append('orderby', orderby);
			fetch('search_keywords_main.php', {
				method: 'POST',
				body: formData
			})
			.then(r => {
				if (r.ok) {
					return r.text()
				}
				throw new Error(errMsg);
			})
			.then(d => {
				listArea.innerHTML = d;
			}).catch((error) => {
				$DE('edsta').innerHTML = errMsg;
			});
			<?php }?>
			ShowMsg('成功更新关键词');
			//myajax.SendGet("search_keywords_main.php?dopost=update&aid="+nid+"&keyword="+kw+"&spwords="+kws+"&count="+ct+"&pageno="+pageno);
		}
		//删除关键词
		function DelNote(nid) {
			var listArea = $Obj('rslist');
			totalrow = totalrow - 1;
			var pagenum = Math.ceil(totalrow / pagesize);
			if (pagenum <= pageno) pageno = pagenum;
			var errMsg = "网络通信出错，<a href=\"javascript:LoadPage('" + pageno + "')\">重新加载列表</a>";
			fetch("search_keywords_main.php?dopost=del&aid=" + nid + "&pageno=" + pageno + "&orderby=" + orderby).then(resp=>{
				if (resp.ok) {
					return resp.text()
				}
				throw new Error(errMsg);
			}).then((d)=>{
				listArea.innerHTML = d;
			}).catch((error) => {
				listArea.innerHTML = errMsg;
			});
			ReloadPageNum(pageno);
		}
		function ReloadPageNum(startnum) {
			var ListArea = $Obj('pagelist');
			var pagenum = Math.ceil(totalrow / pagesize);
			var listsize = 6;
			var ahtml = "<ul class='pagination justify-content-center'>";
			var startloop = 1;
			var endnum = 0;
			ahtml += "<li class='page-item disabled'><span class='page-link'>" + pagenum + "页" + totalrow + "篇</span></li>";
			if (pageno > 1 && pageno != 1) ahtml += "<li class='page-item disabled'><a href='javascript:;' class='page-link' onclick='LoadPage(1)'>首页</a></li>";
			if (pageno > 1) ahtml += "<li class='page-item'><a href='javascript:;' class='page-link' onclick='LoadPage(" + (pageno - 1) + ")'>上页</a></li>";
			if (startnum >= pagenum - listsize) {
				startloop = pagenum - (listsize * 2);
				if (startloop < 1) startloop = 1;
				for (i = startloop; i <= pagenum; i++) {
					if (i == pageno) ahtml += "<li class='page-item disabled'><span class='page-link'>" + i + "</span></li>";
					else ahtml += "<li class='page-item'><a class='page-link' href='javascript:;' onclick='LoadPage(" + i + ")'>" + i + "</a></li>";
				}
			}
			else if (pagenum < listsize) {
				for (i = 1; i <= pagenum; i++) {
					if (i == pageno) ahtml += "<li class='page-item disabled'><span class='page-link'>" + i + "</span></li>";
					else ahtml += "<li class='page-item'><a class='page-link' href='javascript:;' onclick='LoadPage(" + i + ")'>" + i + "</a></li>";
				}
			} else {
				startloop = startnum - listsize;
				if (startloop < 1) { startloop = 1; endnum = startloop + (listsize * 2); }
				else { endnum = startnum + listsize; }
				if (endnum >= pagenum) endnum = pagenum;
				for (i = startloop; i <= endnum; i++) {
					if (i == pageno) ahtml += "<li class='page-item disabled'><span class='page-link'>" + i + "</span></li>";
					else ahtml += "<li class='page-item'><a class='page-link' href='javascript:;' onclick='LoadPage(" + i + ")'>" + i + "</a></li>";
				}
			}
			if (pageno < pagenum) ahtml += "<li class='page-item'><a class='page-link' href='javascript:;' onclick='LoadPage(" + (pageno + 1) + ")'>下页</a></li>";
			if (pageno != pagenum && pagenum > 1) ahtml += "<li class='page-item'><a class='page-link' href='javascript:;' onclick='LoadPage(" + pagenum + ")'>末页</a></li>";
			ListArea.innerHTML = ahtml+"</ul>";
		}
		</script>
	</head>
	<body>
		<div id="rslist"><?php GetKeywordList($dsql,$pageno,$pagesize,$orderby);?></div>
		<table align="center" class="table maintable mb-3">
			<tr>
				<td bgcolor="#f5f5f5" align="center">
					<span id="pagelist"></span>
					<script>ReloadPageNum(1);</script>
				</td>
			</tr>
		</table>
	</body>
</html>