<!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/css/bootstrap.min.css">
		<link rel="stylesheet" href="../static/web/font/css/font-awesome.min.css">
		<link rel="stylesheet" href="../static/web/css/admin.css">
		<script src="../static/web/js/webajax.js"></script>
		<script src="../static/web/js/jquery.min.js"></script>
		<script>
			var searchconfig = false;
			function Nav() {
				if (window.navigator.userAgent.indexOf("MSIE") >= 1) return 'IE';
				else if (window.navigator.userAgent.indexOf("Firefox") >= 1) return 'FF';
				else return "OT";
			}
			function $Obj(objname) {
				return document.getElementById(objname);
			}
			function ShowConfig(em, allgr) {
				if (searchconfig) location.reload();
				for (var i = 1; i <= allgr; i++) {
					if (i == em) $Obj('td' + i).style.display = (Nav() == 'IE' ? 'block' : 'table');
					else $Obj('td' + i).style.display = 'none';
				}
				$Obj('addvar').style.display = 'none';
			}
			function ShowHide(objname) {
				var obj = $Obj(objname);
				if (obj.style.display != "none") obj.style.display = "none";
				else obj.style.display = (Nav() == 'IE' ? 'block' : 'table-row');
			}
			function backSearch() {
				location.reload();
			}
			function getSearch() {
				var searchKeywords = $Obj('keywds').value;
				searchKeywords = searchKeywords.replace(/^cfg_/, "");
				fetch('sys_info.php?dopost=search&keywords=' + searchKeywords).then(resp => {
					if (resp.ok) {
						return resp.text()
					}
					throw new Error('系统错误，无法获取数据');
				}).then((d) => {
					$Obj('_search').innerHTML = d;
				}).catch((error) => {
					$Obj('_search').innerHTML = errMsg;
				});
				$Obj('_searchback').innerHTML = '<button class="btn btn-success btn-sm" name="searchbackBtn" type="button" id="searchbackBtn" onclick="backSearch()">返回</button>'
				$Obj('_mainsearch').innerHTML = '';
				searchconfig = true;
			}
			function resetCookieEncode() {
				jQuery.get("sys_info.php?dopost=make_encode", function(data) {
					jQuery("#edit___cfg_cookie_encode").val(data);
				});
			}
		</script>
	</head>
	<body>
		<div style="min-width:780px">
			<table width="98%" cellpadding="2" cellspacing="1" align="center" class="table maintable mt-3 mb-3">
				<tr>
					<td height="26" background="../static/web/img/tbg.gif" style="padding-left:10px">系统配置变量</td>
				</tr>
				<tr>
					<td height="26">
						<?php
						$ds = file(DEDEADMIN.'/inc/configgroup.txt');
						$totalGroup = count($ds);
						$i = 0;
						foreach($ds as $dl)
						{
							$dl = trim($dl);
							if(empty($dl)) continue;
							$dls = explode(',',$dl);
							$i++;
							if ($i>1) echo "<a href='javascript:ShowConfig($i,$totalGroup)' class='btn btn-success btn-sm'>{$dls[1]}</a>";
							else {
								echo "<a href='javascript:ShowConfig($i,$totalGroup)' class='btn btn-success btn-sm'>{$dls[1]}</a>";
							}
						}
						?>
						<a href="javascript:;" onClick="ShowHide('addvar')" class="btn btn-success btn-sm">添加新变量</a>
					</td>
				</tr>
				<tr id="addvar" style="display:none">
					<td height="26" align="center">
						<form name="fadd" action="sys_info.php" method="post">
							<input type="hidden" name="dopost" value="add">
							<input type="hidden" name="_csrf_token" value="<?php echo $GLOBALS['csrf_token']; ?>">
							<table width="100%" cellspacing="0" cellpadding="0">
								<tr>
									<td width="90" height="26" style="border-top:0">变量说明</td>
									<td width="270" style="border-top:0"><input type="text" name="varmsg" id="varmsg" class="npvar" style="width:260px"></td>
									<td width="90" style="border-top:0">变量值</td>
									<td width="270" style="border-top:0"><input name="nvarvalue" type="text" id="nvarvalue" class="npvar" style="width:260px"></td>
								</tr>
								<tr>
									<td width="90" height="26">变量类型</td>
									<td colspan="3">
										<input name="vartype" type="radio" value="string" class='np' checked='checked'>
										文本
										<input name="vartype" type="radio" value="number" class='np'>
										数字
										<input type="radio" name="vartype" value="bool" class='np'>
										布尔(Y/N)
										<input type="radio" name="vartype" value="bstring" class='np'>
										多行文本
									</td>
								</tr>
								<tr>
									<td width="90" height="26">变量名称</td>
									<td width="270"><input name="nvarname" type="text" id="nvarname" class="npvar" style="width:260px"></td>
									<td width="90">所属组</td>
									<td>
										<?php
										echo "<select name='vargroup' class='npvar' style='margin-right:10px;width:160px'>";
										foreach($ds as $dl){
											$dl = trim($dl);
											if(empty($dl)) continue;
											$dls = explode(',',$dl);
											echo "<option value='{$dls[0]}'>{$dls[1]}</option>";
										}
										echo "</select>";
										?>
										<button type="submit" class="btn btn-success btn-sm">保存变量</button>
									</td>
								</tr>
							</table>
						</form>
					</td>
				</tr>
			</table>
			<table width="98%" cellpadding="0" cellspacing="0" align="center" class="table maintable mt-3 mb-3">
				<tr>
					<td height="26" align="right" background="../static/web/img/tbg.gif">
						<input type="text" name="keywds" id="keywds" placeholder="请输入配置" style="width:260px">
						<button type="button" onclick="getSearch()" class="btn btn-success btn-sm">搜索</button>
						<span id="_searchback"></span>
					</td>
				</tr>
				<tr>
					<td style="padding:0">
						<form action="sys_info.php" method="post" name="form1">
							<input type="hidden" name="_csrf_token" value="<?php echo $GLOBALS['csrf_token']; ?>">
							<input type="hidden" name="dopost" value="save">
							<div id="_search"></div>
							<div id="_mainsearch">
								<?php
								$n = 0;
								if(!isset($gp)) $gp = 1;
								foreach($ds as $dl)
								{
									$dl = trim($dl);
									if(empty($dl)) continue;
									$dls = explode(',',$dl);
									$n++;
								?>
								<table width="100%" cellspacing="1" cellpadding="1" id="td<?php echo $n?>" class="table"
									style="<?php if($n!=$gp) echo 'display:none'; ?>">
									<tr align="center" bgcolor="#F8FCF1">
										<td width="360" style="border-top:0">变量说明</td>
										<td style="border-top:0">变量值</td>
										<td width="260" style="border-top:0">变量名称</td>
									</tr>
									<?php
									$dsql->SetQuery("Select * From `#@__sysconfig` where groupid='{$dls[0]}' order by aid asc");
									$dsql->Execute();
									$i = 1;
									while($row = $dsql->GetArray()) {
										if($i%2==0) {
											$bgcolor = "#f8f8f8";
										} else {
											$bgcolor = "#ffffff";
										}
										$i++;
										?>
									<tr align="center" bgcolor="<?php echo $bgcolor?>">
										<td><?php echo $row['info']; ?></td>
										<td align="left">
											<?php
											if($row['type']=='bool') {
												$c1='';
												$c2 = '';
												$row['value']=='Y' ? $c1=" checked" : $c2=" checked";
												echo "<label><input type='radio' name='edit___{$row['varname']}' class='np' value='Y'$c1> 是 </label> ";
												echo "<label><input type='radio' name='edit___{$row['varname']}' class='np' value='N'$c2> 否 </label> ";
											} else if($row['type']=='bstring') {
												$row['value'] = stripslashes($row['value']);
												echo "<textarea name='edit___{$row['varname']}' row='4' id='edit___{$row['varname']}' class='textarea_info' style='width:98%;height:50px'>".dede_htmlspecialchars($row['value'])."</textarea>";
											} else if($row['type']=='number') {
												echo "<input type='text' name='edit___{$row['varname']}' id='edit___{$row['varname']}' value='{$row['value']}' style='width:30%'>";
											} else {
												$addstr='';
												if ($row['varname']=='cfg_cookie_encode') {
													$addstr=' <a href="javascript:resetCookieEncode();" class="btn btn-success btn-sm">重新生成</a>';
												}
												echo "<input type='text' name='edit___{$row['varname']}' id='edit___{$row['varname']}' value=\"".dede_htmlspecialchars($row['value'])."\" style='width:80%'>{$addstr}";
											}
											?>
										</td>
										<td><?php echo $row['varname']?></td>
									</tr>
									<?php
									}
									?>
								</table>
								<?php
								}
								?>
							</div>
							<table cellspacing="1" cellpadding="1" bgcolor="#f8f8f8" class="table">
								<tr>
									<td align="center" class="py-3">
										<button type="submit" class="btn btn-success btn-sm">保存</button>
										<button type="button" onClick="document.form1.reset()" class="btn btn-success btn-sm">重置</button>
									</td>
								</tr>
							</table>
						</form>
					</td>
				</tr>
			</table>
		</div>
	</body>
</html>