<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<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/webajax.js"></script>
		<script src="js/main.js"></script>
	</head>
	<body>
		<table cellpadding="2" cellspacing="1" align="center" class="table maintable my-3">
			<tr bgcolor="#f5f5f5">
				<td>
					<input type="text" name="keywds" id="keywds" placeholder="请输入变量说明" class="admin-input-lg mr-3">
					<a href="javascript:getSearch();" class="btn btn-success btn-sm">搜索</a><?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 colspan="2" class="p-0">
					<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" class="table-borderless">
							<tr>
								<td width="90">变量说明</td>
								<td width="370"><input type="text" name="varmsg" id="varmsg" class="admin-input-lg"></td>
								<td width="90">变量值</td>
								<td width="370"><input type="text" name="nvarvalue" id="nvarvalue" class="admin-input-lg"></td>
							</tr>
							<tr>
								<td width="90">变量名称</td>
								<td colspan="3"><input type="text" name="nvarname" id="nvarname" class="admin-input-lg"></td>
							</tr>
							<tr>
								<td width="90">变量类型</td>
								<td width="370">
									<label><input type="radio" name="vartype" value="string" checked="checked"> 文本</label>
									<label><input type="radio" name="vartype" value="number"> 数字</label>
									<label><input type="radio" name="vartype" value="bool"> 布尔(Y/N)</label>
									<label><input type="radio" name="vartype" value="bstring"> 多行文本</label>
									<label><input type="radio" name="vartype" value="img"> 图片</label>
								</td>
								<td width="90">所属组</td>
								<td width="170">
									<?php
									echo "<select name='vargroup' class='admin-input-sm'>";
									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>
		<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 cellspacing="1" cellpadding="1" align="center" id="td<?php echo $n?>" class="table maintable mb-3" style="<?php if ($n!=$gp) echo 'display:none';?>">
					<tr>
						<td bgcolor="#f5f5f5" colspan="3">系统变量设置</td>
					</tr>
					<tr bgcolor="#e9ecef" align="center">
						<td width="360">变量说明</td>
						<td>变量值</td>
						<td width="260">变量名称</td>
					</tr>
					<?php
					$dsql->SetQuery("SELECT * FROM `#@__sysconfig` where groupid='{$dls[0]}' order by aid asc");
					$dsql->Execute();
					$i = 1;
					while($row = $dsql->GetArray()) {
					$i++;
					?>
					<tr align="center">
						<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']}' value='Y' $c1> 是</label> ";
								echo "<label><input type='radio' name='edit___{$row['varname']}' value='N' $c2> 否</label> ";
							} else if ($row['type']=='bstring') {
								$row['value'] = stripslashes($row['value']);
								echo "<textarea name='edit___{$row['varname']}' id='edit___{$row['varname']}' class='admin-textarea-xl'>".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']}' class='w-75'>";
							} else if ($row['type']=='img') {
								echo "<input type='text' name='edit___{$row['varname']}' id='edit___{$row['varname']}' value='{$row['value']}' class='w-50'> <input type='button' name='set9' class='btn btn-success btn-sm' onclick="."SelectImageN('form1.edit___{$row['varname']}','','idd_{$row['varname']}');"." value='选择'>";
									if ($row['value']){
										echo " <img src=".$row['value']." id='idd_{$row['varname']}' class='thumbnail-md'>";
									} else {
									echo " <img src='../static/web/img/thumbnail.jpg' id='idd_{$row['varname']}' class='thumbnail-md'>";
								}
							} 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'])."\" class='w-75'> {$addstr}";
							}
							?>
						</td>
						<td><?php echo $row['varname']?></td>
					</tr>
					<?php }?>
				</table>
				<?php }?>
			</div>
			<table cellspacing="1" cellpadding="1" align="center" class="table maintable mb-3">
				<tr>
					<td bgcolor="#f5f5f5" align="center" colspan="3">
						<button type="submit" class="btn btn-success btn-sm">保存</button>
						<button type="button" class="btn btn-outline-success btn-sm" onclick="document.form1.reset()">重置</button>
					</td>
				</tr>
			</table>
		</form>
		<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 ($Obj('td' + 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('_mainsearch').innerHTML = '';
				searchconfig = true;
			}
			function resetCookieEncode() {
				jQuery.get("sys_info.php?dopost=make_encode", function(data) {
					jQuery("#edit___cfg_cookie_encode").val(data);
				});
			}
		</script>
	</body>
</html>