<!DOCTYPE html>
<html>
	<head>
		<meta charset="<?php echo $cfg_soft_lang; ?>">
		<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
		<title>SQL命令行工具</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">
		<link rel="stylesheet" href="css/codemirror.css">
		<script src="js/codemirror.js"></script>
		<script src="js/mode/sql/sql.js"></script>
	</head>
	<body>
		<table width="98%" cellpadding="3" cellspacing="1" align="center" class="table maintable mt-3 mb-3">
			<tr>
				<td height="26" background="../static/web/img/tbg.gif">
					<table width="96%" cellspacing="1" cellpadding="1" class="table table-borderless">
						<tr>
							<td width="30%" style="padding-left:10px">SQL命令行工具</td>
							<td width="70%" align="right">
								<a href="sys_data.php" class="btn btn-success btn-sm">数据备份</a>
								<a href="sys_data_revert.php" class="btn btn-success btn-sm">数据还原</a>
							</td>
						</tr>
					</table>
				</td>
			</tr>
			<tr>
				<td>
					<table width="100%" cellspacing="4" cellpadding="2" class="table table-borderless">
						<form action="sys_sql_query.php" method="post" name="infoform" target="stafrm">
							<input type='hidden' name='dopost' value='viewinfo'>
							<input type="hidden" name="_csrf_token" value="<?php echo $GLOBALS['csrf_token']; ?>">
							<tr>
								<td width="260" height="26">系统的表信息：</td>
								<td>
									<table width="100%" cellspacing="0" cellpadding="0">
										<tr>
											<td width="30%">
												<select name="tablename" id="tablename" style="width:100%;height:auto"
													size="12">
													<?php
													if ($cfg_dbtype=="sqlite") {
														$query = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;";
													} else {
														$query = "SHOW TABLES FROM {$GLOBALS['cfg_dbname']} ";
														}
														$dsql->SetQuery($query);
														$dsql->Execute('t');
														while($row = $dsql->GetArray('t',MYSQL_BOTH))
														{
															$dsql->SetQuery("Select count(*) From ".$row[0]);
															$dsql->Execute('n');
															$row2 = $dsql->GetArray('n',MYSQL_BOTH);
															$dd = $row2[0];
															echo "<option value='".$row[0]."'>".$row[0]."(".$dd.")</option>";
														}
													?>
												</select>
											</td>
											<td width="2%"></td>
											<td width="63%" valign="bottom">
												<div style="float:left;margin-right:20px">
													<button type="Submit" name="Submit1" class="btn btn-success btn-sm" onClick="this.form.dopost.value='opimize';">优化选中表</button>
													<br>
													<button type="Submit" name="Submit2" class="btn btn-success btn-sm" onClick="this.form.dopost.value='repair';" style="margin-top:10px">修复选中表</button>
													<br>
													<button type="Submit" name="Submit3" class="btn btn-success btn-sm" onClick="this.form.dopost.value='viewinfo';" style="margin-top:10px">查看表结构</button>
												</div>
												<div style="float:left">
													<button type="Submit" name="Submit5" class="btn btn-success btn-sm" onClick="this.form.dopost.value='opimizeAll';">优化全部表</button>
													<br>
													<button type="Submit" name="Submit6" class="btn btn-success btn-sm" onClick="this.form.dopost.value='repairAll';" style="margin-top:10px">修复全部表</button>
												</div>
											</td>
										</tr>
									</table>
								</td>
							</tr>
							<tr>
								<td height="200">返回信息：</td>
								<td>
									<iframe name="stafrm" frameborder="0" id="stafrm" width="100%" height="100%"></iframe>
								</td>
							</tr>
						</form>
						<form action="sys_sql_query.php" method="post" name="form1" target="stafrm">
							<input type="hidden" name="_csrf_token" value="<?php echo $GLOBALS['csrf_token']; ?>">
							<input type='hidden' name='dopost' value='query'>
							<tr>
								<td height="26" colspan="2" bgcolor="#f8f8f8">运行SQL命令行：
									<label><input name="querytype" type="radio" class="np" value="0"> 单行命令（支持简单查询）</label>
									<label><input name="querytype" type="radio" class="np" value="2" checked> 多行命令</label>
								</td>
							</tr>
							<tr>
								<td height="110" colspan="2" style="border:1px solid #ddd">
									<textarea id="sqlquery" name="sqlquery" cols="60" rows="10" id="sqlquery" style="width:90%"></textarea>
								</td>
							</tr>
							<tr>
								<td colspan="2" align="center" class="py-3"><button type="submit" class="btn btn-success btn-sm">保存</button>
								</td>
							</tr>
						</form>
					</table>
				</td>
			</tr>
		</table>
		<script>
			var editor = CodeMirror.fromTextArea(document.getElementById('sqlquery'), {
				lineNumbers: true,
				lineWrapping: true,
				mode: 'text/x-mysql'
			});
		</script>
	</body>
</html>