<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="设备ID：" prop="deviceUuid">
        <el-input v-model="queryParams.deviceUuid" placeholder="请输入设备ID" clearable />
      </el-form-item>
      <el-form-item label="当前状态：" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable>
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间：" prop="createdAt">
        <el-date-picker clearable v-model="queryParams.createdAt" type="date" value-format="YYYY-MM-DD"
          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-form-item>
    </el-form>

    <el-table v-loading="loading" :data="deviceInfoList">
      <el-table-column label="设备ID" align="center" prop="deviceUuid" />
      <el-table-column label="创建时间" align="center" prop="createdAt">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="当前状态" align="center" prop="state" :formatter="statusFormatter" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="danger" @click="handleDelete(scope.row)" v-hasPermi="['mqtt:list: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" />
  </div>
</template>

<script setup>
import { listDeviceInfo, delDeviceInfo } from "@/api/mqtt/list";

const { proxy } = getCurrentInstance();

const deviceInfoList = ref([]);
const loading = ref(true);
const total = ref(0);

const statusOptions = ref([{
  value: 'offline',
  label: '离线'
}, {
  value: 'online',
  label: '在线'
}])

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    createdAt: null,
    updatedAt: null,
    deletedAt: null,
    deviceUuid: null,
    state: null
  },
});

const { queryParams } = toRefs(data);

/** 查询设备列表列表 */
function getList() {
  loading.value = true;
  listDeviceInfo(queryParams.value).then(response => {
    deviceInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

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

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

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.deviceUuid
  proxy.$modal.confirm('是否确认删除设备ID为"' + _ids + '"的数据项？').then(function () {
    return delDeviceInfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

getList();

// 字典翻译
function statusFormatter(row) {
  return proxy.selectDictLabel(statusOptions.value, row.state)
}
</script>
