<template>
  <div class="system-user-container layout-padding">
    <el-card shadow="hover" class="layout-padding-auto">
      <div class="system-user-search mb15">
        <el-input size="default" placeholder="请输入用户名称" style="max-width: 180px">
        </el-input>
        <el-button size="default" type="primary" class="ml10">
          <el-icon>
            <ele-Search />
          </el-icon>
          查询
        </el-button>
      </div>
      <el-table
        :data="state.tableData.data"
        v-loading="state.tableData.loading"
        style="width: 100%"
        :cell-style="{ textAlign: 'center' }"
        :header-cell-style="{ 'text-align': 'center' }"
      >
        <el-table-column prop="title" label="操作模块" />
        <el-table-column
          prop="operationType"
          label="操作类型"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="httpMethod"
          label="请求方式"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column prop="username" label="操作人员" show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          prop="remoteAddr"
          label="IP地址"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column prop="status" label="操作状态" show-overflow-tooltip>
          <template #default="scope">
            <el-tag type="success" size="small" v-if="scope.row.status == 1">成功</el-tag>
            <el-tag type="danger" size="small" v-else>失败</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="操作时间" show-overflow-tooltip>
          <template #default="scope">
            {{ formatDate(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="useTime" label="消耗时间（ms）" show-overflow-tooltip>
        </el-table-column>

        <el-table-column label="操作" width="100">
          <template #default="scope">
            <el-button size="small" text type="primary" @click="onOpenlookUser(scope.row)"
              >详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="onHandleSizeChange"
        @current-change="onHandleCurrentChange"
        class="mt15"
        :pager-count="5"
        :page-sizes="[10, 20, 30]"
        v-model:current-page="state.tableData.param.pageNum"
        background
        v-model:page-size="state.tableData.param.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="state.tableData.total"
      >
      </el-pagination>
    </el-card>
    <UserDialog ref="userDialogRef" @refresh="getTableData()" />
  </div>
</template>

<script setup lang="ts" name="systemLog">
import { defineAsyncComponent, reactive, onMounted, ref } from "vue";
import { ElMessageBox, ElMessage, ElNotification } from "element-plus";
import { logApi } from "/@/api/sys/role";

// 引入组件
const UserDialog = defineAsyncComponent(() => import("/@/views/sys/log/dialog.vue"));

// 定义变量内容
const userDialogRef = ref();
const state = reactive<any>({
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 10,
    },
  },
});

// 初始化表格数据
const getTableData = async () => {
  state.tableData.loading = true;
  const data = await logApi().getlogList({
    title: "",
    operationType: "",
    status: "",
    username: "",
    page: state.tableData.param.pageNum,
    limit: state.tableData.param.pageSize,
  });
  state.tableData.data = data.data;
  state.tableData.total = data.count;
  state.tableData.loading = false;
};

function formatDate(date: any) {
  var time = new Date(date);
  var newTime =
    time.getFullYear() +
    "-" +
    (time.getMonth() + 1) +
    "-" +
    time.getDate() +
    " " +
    (time.getHours() < 10 ? "0" + time.getHours() : time.getHours()) +
    ":" +
    (time.getMinutes() < 10 ? "0" + time.getMinutes() : time.getMinutes()) +
    ":" +
    (time.getSeconds() < 10 ? "0" + time.getSeconds() : time.getSeconds());
  return newTime;
}

// 打开修改用户弹窗
const onOpenlookUser = (row: RowUserType) => {
  userDialogRef.value.openDialog(row);
};
// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  getTableData();
};
// 页面加载时
onMounted(() => {
  getTableData();
});
</script>

<style scoped lang="scss">
.system-user-container {
  :deep(.el-card__body) {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: auto;
    .el-table {
      flex: 1;
    }
  }
}
</style>
