<template>
  <div class="my-layout">
    <el-card class="mt8" shadow="never" >
      <el-form :model="state.filterModel" :inline="true" @submit.stop.prevent>
        <el-form-item prop="name">
          <el-input v-model="state.filterModel.createdUserName" placeholder="操作账号" @keyup.enter="onQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="ele-Search" @click="onQuery"> 查询 </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="my-fill mt8" shadow="never">
      <el-table ref="tableRef" v-loading="state.loading" :data="state.oprationLogListData" row-key="id" style="width: 100%" stripe>
        <el-table-column prop="createdUserName" label="操作账号" >
          <template #default="{ row }"> {{ row.createdUserName }}</template>
        </el-table-column>
        <el-table-column prop="operationType" label="操作类型" >
          <template #default="{ row }"> {{ row.operationType }}</template>
        </el-table-column>
        <el-table-column prop="moduleName" label="模块名称" >
          <template #default="{ row }"> {{ row.moduleName }}</template>
        </el-table-column>
        <el-table-column prop="operationContent" label="描述" >
          <template #default="{ row }"> {{ row.operationContent }}</template>
        </el-table-column>
        <el-table-column prop="beforeState" label="操作前" >
          <template #default="{ row }"> {{ row.beforeState }}</template>
        </el-table-column>
        <el-table-column prop="afterState" label="操作后" >
          <template #default="{ row }"> {{ row.afterState }}</template>
        </el-table-column>
        <el-table-column prop="result" label="操作结果" >
          <template #default="{ row }"> {{ row.result }}</template>
        </el-table-column>
        <el-table-column prop="createdTime" label="操作时间" >
          <template #default="{ row }"> {{ row.createdTime }}</template>
        </el-table-column>
      </el-table>
      <div class="my-flex my-flex-end" style="margin-top: 20px">
        <el-pagination
          v-model:currentPage="state.pageInput.currentPage"
          v-model:page-size="state.pageInput.pageSize"
          :total="state.total"
          :page-sizes="[10, 20, 50, 100]"
          small
          background
          @size-change="onSizeChange"
          @current-change="onCurrentChange"
          layout="total, sizes, prev, pager, next, jumper"
        />
      </div>
    </el-card>
  </div>
</template>

<script lang="ts" setup name="admin/oprationLog">
import { reactive, onMounted, ref } from 'vue'
import { OprationLogListOutput, PageInputLogGetPageDto,PageInputOperationGetPageDto,OauthOperationRecordGetPageOutPut } from '/@/api/admin/data-contracts'
import { OprationLogApi } from '/@/api/admin/OprationLog'
import { PermissionApi } from '/@/api/admin/Permission'
import dayjs from 'dayjs'
import type { TableInstance } from 'element-plus'

const tableRef = ref<TableInstance>()

const state = reactive({
  loading: false,
  oprationLogFormTitle: '',
  filterModel: {
    createdUserName: '',
  },
  total: 0,
  pageInput: {
    currentPage: 1,
    pageSize: 20,
  } as PageInputOperationGetPageDto,
  oprationLogListData: [] as Array<OauthOperationRecordGetPageOutPut>,
  oprationLogLogsTitle: '',
})

onMounted(() => {
  onQuery()
})

const formatterTime = (row: any, column: any, cellValue: any) => {
  return dayjs(cellValue).format('YYYY-MM-DD HH:mm:ss')
}

const onQuery = async () => {
  state.loading = true
  state.pageInput.filter = state.filterModel
  const res = await new PermissionApi().getOperationInfo(state.pageInput).catch(() => {
    state.loading = false
  })

  state.oprationLogListData = res?.data?.list ?? []
  state.total = res?.data?.total ?? 0
  state.loading = false
}

const onSizeChange = (val: number) => {
  state.pageInput.pageSize = val
  onQuery()
}

const onCurrentChange = async (val: number) => {
  state.pageInput.currentPage = val
  await onQuery()
  tableRef.value?.setScrollTop(0)
}
</script>

<style scoped lang="scss"></style>
