<template>
  <div class="app-container-page">
    <el-form :model="queryParams" class="table-form-search" ref="queryRef" :inline="true" v-show="showSearch"
      label-width="68px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="项目" prop="project">
            <el-input v-model="queryParams.project" placeholder="请输入项目" clearable @keyup.enter="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="仓库" prop="warehouse">
            <el-input v-model="queryParams.warehouse" placeholder="请输入仓库" clearable @keyup.enter="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="8" class="searchBtn">
          <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-col>
      </el-row>
    </el-form>
    <div class="table-list">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="Plus" @click="handleAdd"
            v-hasPermi="['worn:mqttData:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
            v-hasPermi="['worn:mqttData:edit']">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
            v-hasPermi="['worn:mqttData:remove']">删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="Download" @click="handleExport"
            v-hasPermi="['worn:mqttData:export']">导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="mqttDataList" @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="设备ID" align="center" prop="deviceId" />
        <el-table-column label="MQTT Topic" align="center" prop="topic" min-width="180" show-overflow-tooltip />
        <el-table-column label="项目" align="center" prop="project" min-width="100" show-overflow-tooltip />
        <el-table-column label="仓库" align="center" prop="warehouse" />
        <el-table-column label="原始消息" align="center" prop="payload" min-width="300" show-overflow-tooltip />
        <el-table-column label="解析JSON" align="center" prop="dataJson" min-width="300" show-overflow-tooltip />
        <el-table-column label="电量" align="center" prop="battery">
          <template #default="scope">
            <el-tag v-if="scope.row.battery !== null && scope.row.battery !== undefined && scope.row.battery !== ''"
              color="#409EFF" style="color: #fff;">{{ scope.row.battery }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="温度" align="center" prop="temperature">
          <template #default="scope">
            <el-tag
              v-if="scope.row.temperature !== null && scope.row.temperature !== undefined && scope.row.temperature !== ''"
              color="#E6A23C" style="color: #fff;">{{ scope.row.temperature }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="湿度" align="center" prop="humidity">
          <template #default="scope">
            <el-tag v-if="scope.row.humidity !== null && scope.row.humidity !== undefined && scope.row.humidity !== ''"
              color="#00B5D8" style="color: #fff;">{{ scope.row.humidity }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="氨气" align="center" prop="nh3">
          <template #default="scope">
            <el-tag v-if="scope.row.nh3 !== null && scope.row.nh3 !== undefined && scope.row.nh3 !== ''" color="#9C27B0"
              style="color: #fff;">{{ scope.row.nh3 }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="硫化氢" align="center" prop="h2s">
          <template #default="scope">
            <el-tag v-if="scope.row.h2s !== null && scope.row.h2s !== undefined && scope.row.h2s !== ''" color="#EC407A"
              style="color: #fff;">{{ scope.row.h2s }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="烟雾浓度" align="center" prop="concentration">
          <template #default="scope">
            <el-tag color="#607D8B" style="color: #fff;">{{ scope.row.concentration ?? 0 }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="水位状态" align="center" prop="waterStatus">
          <template #default="scope">
            <el-tag
              :type="(scope.row.waterStatus === null || scope.row.waterStatus === undefined || scope.row.waterStatus === '' || scope.row.waterStatus === 0) ? 'success' : 'danger'">
              {{ (scope.row.waterStatus === null || scope.row.waterStatus === undefined || scope.row.waterStatus === ''
                ||
                scope.row.waterStatus === 0) ? '正常' : '报警' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <!--      <el-table-column label="删除标识" align="center" prop="isDelete" />-->
        <el-table-column label="操作" align="center" fixed="right" width="150">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['worn:mqttData:edit']">修改</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
              v-hasPermi="['worn:mqttData: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>
    <!-- 添加或修改设备数据对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="mqttDataRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="设备ID" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备ID" />
        </el-form-item>
        <el-form-item label="MQTT Topic" prop="topic">
          <el-input v-model="form.topic" placeholder="请输入MQTT Topic" />
        </el-form-item>
        <el-form-item label="项目" prop="project">
          <el-input v-model="form.project" placeholder="请输入项目" />
        </el-form-item>
        <el-form-item label="仓库" prop="warehouse">
          <el-input v-model="form.warehouse" placeholder="请输入仓库" />
        </el-form-item>
        <el-form-item label="原始消息" prop="payload">
          <el-input v-model="form.payload" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="解析JSON" prop="dataJson">
          <el-input v-model="form.dataJson" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="电量" prop="battery">
          <el-input v-model="form.battery" placeholder="请输入电量" />
        </el-form-item>
        <el-form-item label="温度" prop="temperature">
          <el-input v-model="form.temperature" placeholder="请输入温度" />
        </el-form-item>
        <el-form-item label="湿度" prop="humidity">
          <el-input v-model="form.humidity" placeholder="请输入湿度" />
        </el-form-item>
        <el-form-item label="氨气" prop="nh3">
          <el-input v-model="form.nh3" placeholder="请输入氨气" />
        </el-form-item>
        <el-form-item label="硫化氢" prop="h2s">
          <el-input v-model="form.h2s" placeholder="请输入硫化氢" />
        </el-form-item>
        <el-form-item label="烟雾浓度" prop="concentration">
          <el-input v-model="form.concentration" placeholder="请输入烟雾浓度" />
        </el-form-item>
        <el-form-item label="水位状态" prop="waterStatus">
          <el-select v-model="form.waterStatus" placeholder="请选择水位状态">
            <el-option label="正常" :value="0" />
            <el-option label="报警" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="删除标识" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入删除标识" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MqttData">
import { listMqttData, getMqttData, delMqttData, addMqttData, updateMqttData } from "@/api/worn/mqttData"

const { proxy } = getCurrentInstance()

const mqttDataList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    project: null,
    warehouse: null
  },
  rules: {
    deviceId: [
      { required: true, message: "设备ID不能为空", trigger: "blur" }
    ],
  }
})

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

/** 查询设备数据列表 */
function getList() {
  loading.value = true
  listMqttData(queryParams.value).then(response => {
    mqttDataList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    deviceId: null,
    topic: null,
    project: null,
    warehouse: null,
    payload: null,
    dataJson: null,
    battery: null,
    temperature: null,
    humidity: null,
    nh3: null,
    h2s: null,
    concentration: null,
    waterStatus: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    isDelete: null
  }
  proxy.resetForm("mqttDataRef")
}

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

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

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

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加设备数据"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getMqttData(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改设备数据"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["mqttDataRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateMqttData(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addMqttData(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

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

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

getList()
</script>
