<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :model="queryParams" :inline="true">
      <el-form-item label="缓存键" prop="key">
        <el-input v-model="queryParams.key" clearable placeholder="请输入缓存键" @keyup.enter="handleQuery" />
      </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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-has-permi="['monitor:cache:query']"
          type="info"
          icon="Stopwatch"
          plain
          @click="handleInfo"
        >缓存信息</el-button>
      </el-col>
      <right-toolbar v-model:show-search="showSearch" @query-table="getList" />
    </el-row>

    <el-table v-loading="loading" :data="cacheList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" type="index" align="center" width="80">
        <template #default="scope">
          <span>{{ (queryParams.pageNumber - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="缓存键" align="left" prop="key" min-width="140" />
      <el-table-column label="类型" align="center" prop="type" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            v-has-permi="['monitor:cache:list']"
            type="primary"
            icon="View"
            link
            @click="handleView(scope.row)"
          >详细</el-button>
          <el-button
            v-has-permi="['monitor:cache:remove']"
            type="danger"
            icon="Delete"
            link
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 缓存详情对话框 -->
    <el-dialog v-model="open" title="缓存详情" width="700px" append-to-body>
      <el-descriptions border>
        <el-descriptions-item label="缓存键" :span="3">{{ form.key }}</el-descriptions-item>
        <el-descriptions-item label="类型">{{ form.type }}</el-descriptions-item>
        <el-descriptions-item label="描述">{{ form.description }}</el-descriptions-item>
      </el-descriptions>
      <el-table :data="form.value" style="margin-top: 30px;">
        <el-table-column label="序号" type="index" align="center" width="80">
          <template #default="scope">
            <span>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="缓存值" align="center">
          <template #default="scope">
            <span>{{ scope.row }}</span>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="open=false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 缓存信息对话框 -->
    <el-dialog v-model="openInfo" title="缓存信息" width="700px" append-to-body>
      <el-descriptions>
        <el-descriptions-item label="Redis版本">{{ formInfo.redis_version }}</el-descriptions-item>
        <el-descriptions-item label="运行模式">{{ formInfo.redis_mode }}</el-descriptions-item>
        <el-descriptions-item label="端口">{{ formInfo.tcp_port }}</el-descriptions-item>
        <el-descriptions-item label="客户端数">{{ formInfo.connected_clients }}</el-descriptions-item>
        <el-descriptions-item label="占用内存">{{ formInfo.used_memory_human }}</el-descriptions-item>
        <el-descriptions-item label="AOF是否开启">{{ formInfo.aof_enabled === '0' ? '否' : '是' }}</el-descriptions-item>
        <el-descriptions-item label="RDB是否成功">{{ formInfo.rdb_last_bgsave_status }}</el-descriptions-item>
        <el-descriptions-item label="带宽 输入 / 输出">{{ formInfo.instantaneous_input_kbps + 'kbps / ' + formInfo.instantaneous_output_kbps+ 'kbps' }}</el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="openInfo=false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script name="Cache" setup>
import { listCache, delCache, infoCache } from '@/api/monitor/cache'

const { proxy } = getCurrentInstance()

const cacheList = ref([])
const ids = ref([])
const loading = ref(true)
const showSearch = ref(true)
const multiple = ref(true)
const open = ref(false)
const openInfo = ref(false)
const total = ref(0)
const form = ref({})
const formInfo = ref({})

const queryParams = reactive({
  pageNumber: 1,
  pageSize: 10,
  key: undefined
})

/** 查询在线用户列表 */
function getList() {
  loading.value = true
  listCache(queryParams).then(res => {
    cacheList.value = res.list
    total.value = Number(res.total)
    loading.value = false
  })
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNumber = 1
  getList()
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.$resetForm('queryRef')
  handleQuery()
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.key)
  multiple.value = !selection.length
}
/** 详细按钮操作 */
function handleView(row) {
  proxy.$resetForm('form')
  open.value = true
  form.value = row
}
/** 删除按钮操作 */
function handleDelete(row) {
  // const keys = row.key || ids.value
  let o = {
    name: row.name,
    key: row.key
  }
  proxy.$modal.confirm('是否确认删除缓存键为 "' + row.key + '" 的数据项?').then(function() {
    return delCache(o)
  }).then(() => {
    getList()
    open.value = false
    proxy.$modal.msgSuccess('删除成功')
  }).catch(() => {})
}
/** 缓存信息 */
function handleInfo(row) {
  proxy.$resetForm('formInfo')
  infoCache().then(res => {
    openInfo.value = true
    formInfo.value = res
  })
}

getList()
</script>
