<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" inline>
      <el-form-item label="主题名称：" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入主题名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="主题消息：" prop="payload">
        <el-input v-model="queryParams.payload" placeholder="请输入主题消息" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="创建时间：" style="width: 308px;">
        <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" start-placeholder="开始日期"
          end-placeholder="结束日期"></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-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['mqtt:topiclog:remove']">删除</el-button>
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['mqtt:topiclog:export']">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="topiclogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="主题名称" align="center" prop="name" />
      <el-table-column label="主题消息" align="center" prop="payload" show-overflow-tooltip />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="更新时间" align="center" prop="updateTime" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="handleUpdate(scope.row)"
            v-hasPermi="['mqtt:topiclog:edit']">查看</el-button>
          <el-button link type="danger" @click="handleDelete(scope.row)"
            v-hasPermi="['mqtt:topiclog:remove']">删除</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="500px" append-to-body>
      <el-form ref="topiclogRef" :model="form" label-width="auto" disabled>
        <el-form-item label="主题消息：" prop="payload">
          <el-input v-model="form.payload" type="textarea" />
        </el-form-item>
        <el-form-item label="主题名称：" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="备注：" prop="remark">
          <el-input v-model="form.remark" type="textarea" />
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup name="Topiclog">
import { listTopiclog, getTopiclog, delTopiclog } from "@/api/mqtt/topiclog";

const { proxy } = getCurrentInstance();

const topiclogList = ref([]);
const open = ref(false);
const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    payload: null,
    name: null,
    status: null,
  },
});

const { queryParams, form } = toRefs(data);

/** 查询设备主题日志列表 */
function getList() {
  loading.value = true;
  listTopiclog(proxy.addDateRange(queryParams.value, dateRange.value)).then(res => {
    loading.value = false;
    topiclogList.value = res.rows;
    total.value = res.total;
  });
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    payload: null,
    name: null,
    status: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("topiclogRef");
}

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

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

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

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getTopiclog(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改设备主题日志";
  });
}


/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除设备主题日志编号为"' + _ids + '"的数据项？').then(function () {
    return delTopiclog(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('device/topiclog/export', {
    ...queryParams.value
  }, `topiclog_${new Date().getTime()}.xlsx`)
}

getList();
</script>

<style lang="scss" scoped>
.el-table__cell {
  text-align: center !important;
}

.el-tooltip__popper[x-placement^="bottom"] .popper__arrow {
  border-bottom-color: white !important;
}

.el-tooltip__popper[x-placement^="bottom"] .popper__arrow:after {
  border-bottom-color: white !important;
}

.testtooltip {
  width: 50%;
}
</style>
