<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<html lang="zh">
	<head>
		<meta charset="UTF-8" />
		<title>日志管理</title>
		<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}" />
		<script src="{{ url_for('static', filename='utils.js') }}"></script>
		<script>
			function searchData() {
				const operation_type = document.getElementById('operation_type').value
				const operation_module = document.getElementById('operation_module').value
				const username = document.getElementById('username').value
				const startDate = document.getElementById('startDate').value
				const endDate = document.getElementById('endDate').value
				window.location.href = `{{ url_for('log.log_list') }}?u=${username}&ot=${operation_type}&om=${operation_module}&s=${startDate}&e=${endDate}`
			}
			function reSearchData() {
				window.location.href = `{{ url_for('log.log_list') }}`
			}
			function jumpToPage(page) {
				window.location.href = `{{ url_for('log.log_list') }}?u={{username}}&ot={{operation_type}}&om={{operation_module}}&s={{start_date}}&e={{end_date}}&pp={{per_page}}&p=${page}`
			}
		</script>
	</head>
	<body>
		<div class="box">
			<div class="box_header">
				<h3 class="box_title">日志管理</h3>
				{% include 'base/user_info.html' %}
			</div>
			<div class="box_header">
				<div></div>
				<div class="search_box">
					<select class="form-control" id="operation_type">
						<option value="">全部操作类型</option>
						{% for op in operation_type_list %}
						{% if op == operation_type %}
						<option value="{{ op }}" selected>{{ op }}</option>
						{% else %}
						<option value="{{ op }}" >{{ op }}</option>
						{% endif %}
						{% endfor %}
					</select>
					<select class="form-control" id="operation_module">
						<option value="">全部模块</option>
						{% for op in operation_module_list %}
						{% if op == operation_module %}
						<option value="{{ op }}" selected>{{ op }}</option>
						{% else %}
						<option value="{{ op }}" >{{ op }}</option>
						{% endif %}
						{% endfor %}
					</select>
					<input type="text" id="username" class="form-control" placeholder="请输入用户名" value="{{ username }}" />
					<input type="date" id="startDate" class="form-control" placeholder="开始时间" value="{{ start_date }}" />
					<input type="date" id="endDate" class="form-control" placeholder="结束时间" value="{{ end_date }}" />
					<button type="button" class="btn btn-info btn-large" onclick="searchData()">查询</button>
					<button type="button" class="btn btn-warning btn-large" onclick="reSearchData()">重置</button>
				</div>
			</div>

			<div class="box-body">
				<table class="table">
					<thead>
						<tr>
							<th width="80px">ID</th>
							<th width="240px">用户名</th>
							<th width="180px">操作类型</th>
							<th width="180px">操作模块</th>
							<th>操作描述</th>
							<th width="120px">操作结果</th>
							<th width="180px">操作IP</th>
							<th width="180px">操作时间</th>
						</tr>
					</thead>
					<tbody>
						{% if log_list %} {% for log in log_list %}
						<tr>
							<td>{{ log.id }}</td>
							<td>{{ log.username }}</td>
							<td>
								{% if log.operation_type == '登录' %}
								<span class="label label-info">登录</span>
								{% elif log.operation_type == '新增' %}
								<span class="label label-info">新增</span>
								{% elif log.operation_type == '修改' %}
								<span class="label label-info">修改</span>
								{% elif log.operation_type == '删除' %}
								<span class="label label-danger">删除</span>
								{% else %}
								<span class="label label-success">{{ log.operation_type}}</span>
								{% endif %}
							</td>
							<td>{{ log.operation_module }}</td>
							<td title="{{log.operation_data if log.operation_data else ''}}{{'\n'+log.data_changes if log.data_changes else '' }}">
								{{ log.operation_desc }}
							</td>
							<td>
								{% if log.operation_result == 1 %}
								<span class="label label-success">成功</span>
								{% else %}
								<span class="label label-error">失败</span>
								{% endif %}
							</td>
							<td>{{ log.operation_ip }}</td>
							<td>{{ log.created_at }}</td>
						</tr>
						{% endfor %} {% else %}
						<tr>
							<td colspan="8">暂无数据</td>
						</tr>
						{% endif %}
					</tbody>
				</table>
				<div class="pagination">
					<div class="pagination-info">
						{% set total_pages = (total_count|int + per_page|int - 1)//per_page %}
						<span class="page"> 总共 {{ total_count }} 条数据，每页 {{ per_page }} 条，当前第 {{ page }} 页 / 共 {{ total_pages }} 页 </span>
					</div>
					<div class="pagination-links">
						{% if page > 1 %}
						<span class="page page-link" onclick="jumpToPage(1)">首页</span>
						{% endif %} {% if page > 1 %}
						<span class="page page-link" onclick="jumpToPage(`{{page - 1}}`)">上一页</span>
						{% endif %} {% set start_page = [1, page - 2]|max %} {% set end_page = [total_pages, page + 2]|min %} {% if start_page > 1 %}
						<span class="page page-link" onclick="jumpToPage(1)">1</span>
						{% if start_page > 2 %}
						<span class="page">...</span>
						{% endif %} {% endif %} {% for p in range(start_page, end_page + 1) %} {% if p == page %}
						<span class="page page-link active" onclick="jumpToPage(`{{ p }}`)">{{ p }}</span>
						{% else %}
						<span class="page page-link" onclick="jumpToPage(`{{ p }}`)">{{ p }}</span>
						{% endif %} {% endfor %} {% if end_page < total_pages %} {% if end_page < total_pages - 1 %}
						<span class="page">...</span>
						{% endif %}
						<span class="page page-link" onclick="jumpToPage(`{{ total_pages }}`)">{{ total_pages }}</span>
						{% endif %} {% if page < total_pages %}
						<span class="page page-link" onclick="jumpToPage(`{{ page+1 }}`)">下一页</span>
						{% endif %} {% if page < total_pages %}
						<span class="page page-link" onclick="jumpToPage(`{{ total_pages }}`)">末页</span>
						{% endif %}
					</div>
				</div>
			</div>
		</div>
	</body>
</html>
