<!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/jquery.min.js"></script>
		<script src="../static/web/js/bootstrap.bundle.min.js"></script>
		<script src="js/main.js"></script>
		<script src='../static/web/js/webajax.js'></script>
		<script>
			function ShowFields() {
				var exptable = $DE('exptable').options[$DE('exptable').selectedIndex].value;
				var queryUrl = "sys_data_replace.php?exptable=" + exptable + "&action=getfields";
				fetch(queryUrl).then(resp => {
					if (resp.ok) {
						return resp.text()
					}
					throw new Error('x');
				}).then((d) => {
					$DE('fields').innerHTML = d;
				}).catch((error) => {
					$DE('fields').innerHTML = errMsg;
				});
			}
			function CheckSubmit() {
				if ($DE('rpfield').value == "") {
					ShowMsg("您选择的操作为手工指定字段，但您并没指定");
					return false;
				}
				if ($DE('rpstring').value == "") {
					ShowMsg("您没指定要替换的字符串");
					return false;
				}
				return true;
			}
			function pf(v) {
				$DE('rpfield').value = v;
			}
		</script>
	</head>
	<body>
		<table width="98%" cellpadding="1" cellspacing="1" align="center" class="table maintable mt-3 mb-3">
			<form action="sys_data_replace.php" name="form1" method="post" target="stafrm" onSubmit="return CheckSubmit()">
				<input type="hidden" name="action" value="apply">
				<tr>
					<td bgcolor="#f8f8f8">数据库内容替换</td>
				</tr>
				<tr>
					<td>
						<table width="100%" cellpadding="2" cellspacing="2" class="table table-borderless">
							<tr>
								<td colspan="2">
									<div class="alert alert-info mb-0">程序用于批量替换数据库中某字段的内容，此操作极为危险，请小心使用</div>
								</td>
							</tr>
							<tr id='datasel'>
								<td width="15%" height="60">选择数据表与字段：</td>
								<td>
									<table width="98%" cellspacing="0" cellpadding="0">
										<tr>
											<td id="tables">
												<?php
												$dsql->SetQuery("Show Tables");
												$dsql->Execute('t');
												if($dsql->GetError()!=''){
													echo "<span class='text-danger'>找不到您所指定的数据库 $dbname</span><br>";
													echo $qbutton;
												}
												echo "<select name='exptable' id='exptable' size='10' style='width:60%;height:220px' onchange='ShowFields()'>";
												while($row = $dsql->GetArray('t',MYSQL_BOTH)){
													echo "<option value='{$row[0]}'>{$row[0]}</option>";
												}
												echo "</select>";
												$dsql->Close();
												?>
											</td>
										</tr>
										<tr>
											<td id='fields'></td>
										</tr>
										<tr>
											<td>要替换的字段：<input type="text" name="rpfield" id="rpfield"></td>
										</tr>
									</table>
								</td>
							</tr>
							<tr bgcolor="#F8FCF1">
								<td>替换方式：</td>
								<td>
									<label><input type="radio" name="rptype" id="ot1" value="replace" checked="1"> 普通替换</label>
									<label><input type="radio" name="rptype" id="ot2" value="regex"> 正则表达式</label>
									主键字段：<input type="text" name="keyfield" id="keyfield" size="12">（正则模式必须指定）
								</td>
							</tr>
							<tr>
								<td>被替换内容：</td>
								<td><textarea name="rpstring" id="rpstring" style="width:60%;height:50px"></textarea></td>
							</tr>
							<tr>
								<td>替换为：</td>
								<td><textarea name="tostring" id="tostring" style="width:60%;height:50px"></textarea></td>
							</tr>
							<tr>
								<td>替换条件：</td>
								<td><input type="text" name="condition" id="condition" style="width:60%">（空完全替换）</td>
							</tr>
							<tr>
								<td>安全确认码：</td>
								<td>
									<input type="text" name="validate" style="width:80px;text-transform:uppercase">
									<img src="../apps/vdimgck.php" style="vertical-align:middle;cursor:pointer" onClick="this.src='../apps/vdimgck.php?'+new Date().getTime()+Math.round(Math.random() * 10000)" title="验证码">
								</td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<td bgcolor="#F8FCF1" align="center" class="py-3">
						<button type="submit" name="Submit" class="btn btn-success btn-sm">开始替换数据</button>
					</td>
				</tr>
			</form>
			<tr>
				<td>
					<table width="100%" class="table table-borderless">
						<tr>
							<td width="30%">结果：</td>
							<td width="70%" align="right">
								<script>
									function ResizeDiv(obj, ty) {
										if (ty == "+") document.all[obj].style.pixelHeight += 50;
										else if (document.all[obj].style.pixelHeight > 80) document.all[obj].style.pixelHeight = document.all[obj].style.pixelHeight - 50;
									}
								</script>
								<a href="javascript:;" onClick="ResizeDiv('mdv','+');" class="btn btn-success btn-sm">增大</a>
								<a href="javascript:;" onClick="ResizeDiv('mdv','-');" class="btn btn-success btn-sm">缩小</a>
							</td>
						</tr>
					</table>
				</td>
			</tr>
			<tr>
				<td id="mtd">
					<div id="mdv" style="width:100%;height:360px">
						<iframe name="stafrm" frameborder="0" id="stafrm" width="100%" height="100%"></iframe>
					</div>
				</td>
			</tr>
		</table>
	</body>
</html>