<template>
  <div class="app-container scroller">
    <PageTitle>
      <template #title>
        <div style="display: flex;align-items: center;">
          <el-icon class="title-icon"><Document /></el-icon>
          <span style="margin-left: 8px;">操作日志</span>
        </div>
       
      </template>

    </PageTitle>
    <div class="app-container__body">

      <query-form
          ref="QueryFormRef"
          v-model="queryParams"
          @query="handleQuery"
          @reset="onReset"
          @handleAdd="handleExport"
        />
    
      <div style="flex: 1;">
        <el-table
          v-loading="loading"
          :data="logList"
          stripe
          height="100%"
          style="width: 100%"
        >
          <el-table-column prop="user" label="用户" width="200" align="left" />
          <el-table-column prop="operationtime" label="时间" align="left" width="180" />
          <el-table-column prop="operationObject" label="模块编号" align="left" width="120" />
          <el-table-column prop="module" label="操作模块" align="left" width="150" />
          <el-table-column prop="logmessage" label="操作信息" align="left" show-overflow-tooltip />
        </el-table>
      
      </div>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.rows"
        @pagination="getList"
      />
  </div>
    

  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue'
import { Document } from '@element-plus/icons-vue'
import { query, checkEnc, queryMenuTreeAndUserlist, getOpeExcel} from '@/api/logSet/operationLog.js'
import { ElMessageBox, ElMessage } from 'element-plus'
import timeRangeOptions from '@/constants/timeRangeOptions.js'
import QueryForm from './QueryForm.vue'
const loading = ref(true)

const logList = ref([])
// const queryParams = reactive({
//   page: 1,
//   rows: 10,
//   user: undefined,
//   range: '0',
//   module: undefined
// })
const data = reactive({
  queryParams: {
    page: 1,
    rows: 10,
    user: undefined,
    range: '0',
    module: undefined
  }
})
const total = ref(0)

// 表格数据
const { queryParams } = toRefs(data)
const userOptions = ref([
  // { id: 'admin', realname: 'admin' },
  // { id: 'system', realname: 'system' },
  // { id: 'test', realname: 'test' }
])


const moduleTreeOptions = ref([])

const getList = async () => {
  loading.value = true
  try {
    const response = await query(queryParams.value)
    logList.value = response.data.list || []
    total.value = response.data.total || 0
  } catch (error) {
    console.error('Failed to fetch log list:', error)
  } finally {
    loading.value = false
  }
}
const onReset = (formQuery) =>{
    console.log('onReset')
  formQuery.resetFields()
  handleQuery()
}
const handleQuery = () => {
  queryParams.value.page = 1
  getList()
}

const handleExport = async () => {
  ElMessageBox.confirm('是否确认导出所有操作日志数据？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      try {
        const response = await getOpeExcel(queryParams.value)
        const blob = new Blob([response], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
        const link = document.createElement('a')
        link.href = URL.createObjectURL(blob)
        link.download = '操作日志.xls'
        link.click()
        URL.revokeObjectURL(link.href)
        ElMessage.success('导出成功')
      } catch (error) {
        console.error('Failed to export operation logs:', error)
        ElMessage.error('导出出错')
      }
    })
    .catch(() => {
      ElMessage.info('导出已取消')
    })
}

const handleCheck = async () => {
  try {
    const response = await checkEnc(queryParams.value)
    if (response.success) {
      window.$modal.msgSuccess('数据完整性校验完成')
    } else {
      window.$modal.msgError('数据完整性校验失败')
    }
  } catch (error) {
    console.error('Failed to perform data integrity check:', error)
    window.$modal.msgError('数据完整性校验出错')
  }
}

onMounted(async () => {
  

  getList()
})
</script>

<style lang="scss" scoped>
// .app-container {
//   padding: 20px;

//   .page-title {
//     display: flex;
//     align-items: center;
//     margin-bottom: 20px;
//     font-size: 16px;
//     font-weight: bold;

//     .title-icon {
//       margin-right: 8px;
//       font-size: 20px;
//       color: #409EFF;
//     }
//   }
  
//   .search-box {
//     margin-bottom: 20px;
//     background-color: #fff;
//     padding: 20px;
//     border-radius: 4px;
//     text-align: left;

//     :deep(.el-form) {
//       justify-content: flex-start;
//     }

//     :deep(.el-form--inline .el-form-item) {
//       margin-right: 20px;
//     }
//   }
  
//   .el-table {
//     margin-top: 20px;
//   }

//   :deep(.el-table) {
//     text-align: left;
//   }

//   :deep(.el-cascader) {
//     width: 100%;
//   }
// }
</style>
