<template>
  <div class="operation-logs">
    <div class="filter-bar">
      <el-input 
        v-model="searchKeyword" 
        placeholder="搜索用户名或操作..."
        style="width: 250px"
        clearable
      />
      <el-select v-model="operationType" placeholder="操作类型" style="width: 150px" clearable>
        <el-option label="新增" value="create" />
        <el-option label="修改" value="update" />
        <el-option label="删除" value="delete" />
        <el-option label="查询" value="read" />
      </el-select>
      <el-date-picker
        v-model="dateRange"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      />
      <el-button type="primary" @click="handleSearch">查询</el-button>
      <el-button @click="handleExport">导出</el-button>
    </div>

    <el-table :data="logs" style="width: 100%">
      <el-table-column prop="username" label="操作人" width="120" />
      <el-table-column prop="module" label="操作模块" width="120" />
      <el-table-column prop="operation" label="操作类型" width="100">
        <template #default="{ row }">
          <el-tag :type="getOperationType(row.operation)">
            {{ getOperationText(row.operation) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="操作描述" min-width="200" />
      <el-table-column prop="ipAddress" label="IP地址" width="140" />
      <el-table-column prop="duration" label="耗时(ms)" width="100" />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="{ row }">
          <el-tag :type="row.status === 'success' ? 'success' : 'danger'">
            {{ row.status === 'success' ? '成功' : '失败' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="operationTime" label="操作时间" width="180" />
      <el-table-column label="操作" width="100" fixed="right">
        <template #default="{ row }">
          <el-button size="small" @click="viewDetail(row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { ElInput, ElSelect, ElOption, ElDatePicker, ElButton, ElTable, ElTableColumn, ElTag, ElPagination, ElMessage } from 'element-plus';

const searchKeyword = ref('');
const operationType = ref('');
const dateRange = ref<[Date, Date] | null>(null);
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);
const logs = ref<any[]>([]);

onMounted(() => {
  loadLogs();
});

function loadLogs() {
  // TODO: 加载操作日志
}

function handleSearch() {
  currentPage.value = 1;
  loadLogs();
}

function handleExport() {
  // TODO: 导出日志
  ElMessage.info('导出功能开发中');
}

function getOperationType(type: string) {
  const typeMap: Record<string, any> = {
    'create': 'success',
    'update': 'warning',
    'delete': 'danger',
    'read': 'info'
  };
  return typeMap[type] || 'info';
}

function getOperationText(type: string) {
  const textMap: Record<string, string> = {
    'create': '新增',
    'update': '修改',
    'delete': '删除',
    'read': '查询'
  };
  return textMap[type] || type;
}

function viewDetail(row: any) {
  // TODO: 查看操作详情
}

function handleSizeChange(val: number) {
  pageSize.value = val;
  loadLogs();
}

function handleCurrentChange(val: number) {
  currentPage.value = val;
  loadLogs();
}
</script>

<style lang="scss" scoped>
.operation-logs {
  padding: 20px;
}

.filter-bar {
  display: flex;
  gap: 16px;
  margin-bottom: 20px;
  align-items: center;
}

.el-table {
  margin-bottom: 20px;
}
</style>

