<template>
   <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="140px">
         <el-form-item label="追踪链ID" prop="traceId">
            <el-input
               v-model="queryParams.traceId"
               placeholder="请输入"
               clearable
               style="width: 240px;"
               @keyup.enter="handleQuery"
            />
         </el-form-item>
         <el-form-item label="源系统" prop="sourceSystem">
            <el-input
               v-model="queryParams.sourceSystem"
               placeholder="请输入"
               clearable
               style="width: 240px;"
               @keyup.enter="handleQuery"
            />
         </el-form-item>
         <el-form-item label="同步状态" prop="status">
            <el-select
               v-model="queryParams.status"
               placeholder="请选择"
               clearable
               style="width: 240px"
            >
               <el-option
                  v-for="dict in sync_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
               />
            </el-select>
         </el-form-item>
         <!-- <el-form-item label="操作时间" style="width: 308px">
            <el-date-picker
               v-model="dateRange"
               value-format="YYYY-MM-DD HH:mm:ss"
               type="daterange"
               range-separator="-"
               start-placeholder="开始日期"
               end-placeholder="结束日期"
               :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
            ></el-date-picker>
         </el-form-item> -->
         <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
         </el-form-item>
      </el-form>

      <el-table ref="operlogRef" v-loading="loading" :data="operlogList" @selection-change="handleSelectionChange" :default-sort="defaultSort" @sort-change="handleSortChange">
         <!-- <el-table-column type="selection" width="50" align="center" /> -->
         <el-table-column label="日志ID" align="center" prop="logId" />
         <el-table-column label="追踪链ID" align="center" prop="traceId" :show-overflow-tooltip="true" />
         <el-table-column label="创建人" align="center" width="110" prop="createBy" :show-overflow-tooltip="true" />
         <el-table-column label="开始时间" align="center" prop="startTime" :show-overflow-tooltip="true" sortable="custom" />
         <el-table-column label="结束时间" align="center" prop="endTime" :show-overflow-tooltip="true" sortable="custom" />
         <el-table-column label="源系统" align="center" prop="sourceSystem" width="130" :show-overflow-tooltip="true" />
         <el-table-column label="源表名" align="center" prop="sourceTable" width="130" :show-overflow-tooltip="true" />
         <el-table-column label="目标表名" align="center" prop="targetTable" width="130" :show-overflow-tooltip="true" />
         <el-table-column label="数据量" align="center" prop="dataCount" width="130" :show-overflow-tooltip="true" />
         <el-table-column label="同步状态" align="center" prop="status">
            <template #default="scope">
               <dict-tag :options="sync_status" :value="scope.row.status" />
            </template>
         </el-table-column>
         <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
               <el-button link type="primary" icon="View" @click="handleView(scope.row, scope.index)" v-hasPermi="['monitor:operlog:query']">详细</el-button>
            </template>
         </el-table-column>
      </el-table>

      <pagination
         v-show="total > 0"
         :total="total"
         v-model:page="queryParams.pageNum"
         v-model:limit="queryParams.pageSize"
         @pagination="getList"
      />

      <!-- 日志详细 -->
      <el-dialog title="同步日志详细" v-model="open" width="800px" append-to-body>
         <el-form :model="form" label-width="160px">
            <el-row>
               <el-col :span="12">
                  <el-form-item label="JobID:">{{ form.jobId }}</el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="日志ID：">{{ form.logId }}</el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="创建者:">{{ form.createBy }}</el-form-item>
               </el-col>
               <el-col :span="12">
                  <el-form-item label="创建时间">{{ form.createTime }}</el-form-item>
               </el-col>
               <el-col :span="24">
                  <el-form-item label="开始时间：">{{ form.startTime }}</el-form-item>
               </el-col>
               <el-col :span="24">
                  <el-form-item label="结束时间：">{{ form.endTime }}</el-form-item>
               </el-col>
               <el-col :span="24">
                  <el-form-item label="耗时:">{{ form.duration }}</el-form-item>
               </el-col>
               <el-col :span="24">
                  <el-form-item label="追踪链ID:">{{ form.traceId }}</el-form-item>
               </el-col>
               <el-col :span="24">
                  <el-form-item label="源系统:">{{ form.sourceSystem }}</el-form-item>
               </el-col>
               <el-col :span="24">
                  <el-form-item label="数据量:">{{ form.dataCount }}</el-form-item>
               </el-col>
               <el-col :span="24">
                  <el-form-item label="源表:">{{ form.sourceTable }}</el-form-item>
               </el-col>
               <el-col :span="24">
                  <el-form-item label="目标表:">{{ form.targetTable }}</el-form-item>
               </el-col>
               <el-col :span="24" v-if="form.status === '2'">
                  <el-form-item label="错误信息:">{{ form.errorMsg }}</el-form-item>
               </el-col>
               <el-col :span="24">
                  <el-form-item label="同步状态:"><dict-tag :options="sync_status" :value="form.status" /></el-form-item>
               </el-col>
            </el-row>
         </el-form>
         <template #footer>
            <div class="dialog-footer">
               <el-button @click="open = false">关 闭</el-button>
            </div>
         </template>
      </el-dialog>
   </div>
</template>

<script setup name="Operlog">
import { list } from "@/api/monitor/synclog"

const { proxy } = getCurrentInstance()
const sync_status = [
  { label: "同步中", value: "0", elTagType: "primary" },
  { label: "成功", value: "1", elTagType: "success" },
  { label: "失败", value: "2", elTagType: "danger" }
]

const operlogList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const multiple = ref(true)
const total = ref(0)
// const dateRange = ref([])
const defaultSort = ref({ prop: "operTime", order: "descending" })

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    traceId: undefined,
    sourceSystem: undefined,
    status: undefined
  }
})

const { queryParams, form } = toRefs(data)

/** 查询登录日志 */
function getList() {
  loading.value = true
  list(queryParams.value).then(response => {
    operlogList.value = response?.data.records
    total.value = response?.data.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  queryParams.value.pageNum = 1
  getList()
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.operId)
  multiple.value = !selection.length
}

/** 排序触发事件 */
function handleSortChange(column, prop, order) {
  queryParams.value.orderByColumn = column.prop
  queryParams.value.isAsc = column.order
  getList()
}

/** 详细按钮操作 */
function handleView(row) {
  open.value = true
  form.value = row
}

getList()
</script>
