<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" inline>
      <el-form-item label="IP：" prop="ip">
        <el-input v-model="queryParams.ip" placeholder="请输入IP" clearable />
      </el-form-item>
      <el-form-item label="端口：" prop="port">
        <el-input v-model="queryParams.port" placeholder="请输入端口" clearable />
      </el-form-item>
      <el-form-item label="创建时间：" prop="createdAt">
        <el-date-picker v-model="queryParams.createdAt" type="date" value-format="YYYY-MM-DD" placeholder="请选择创建时间"
          clearable>
        </el-date-picker>
      </el-form-item>
      <el-form-item label="当前状态：" prop="ping">
        <el-select v-model="queryParams.ping" 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>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['mqtt:info:add']">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="mqttList">
      <el-table-column label="IP" align="center" prop="ip" />
      <el-table-column label="端口" align="center" prop="port" />
      <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="ping" :formatter="statusFormatter" />
      <el-table-column label="更新时间" align="center" prop="updatedAt">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="handleChange(scope.row)" v-hasPermi="['mqtt:info:edit']">编辑</el-button>
          <el-button link type="primary" @click="handleDelete(scope.row)" v-hasPermi="['mqtt:info:delete']">删除</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" />

    <!-- 添加或修改mqtt信息列表对话框 -->
    <el-dialog title="新增" v-model="open" width="500px" append-to-body>
      <el-form ref="mqttRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="IP：" prop="ip">
          <el-input v-model="form.ip" placeholder="请输入IP" />
        </el-form-item>
        <el-form-item label="端口：" prop="port">
          <el-input v-model="form.port" placeholder="请输入端口" />
        </el-form-item>
        <el-form-item label="当前状态：" prop="ping">
          <el-select v-model="form.ping" placeholder="请选择状态">
            <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { listMqtt, getMqtt, delMqtt, addMqtt, updateMqtt } from "@/api/mqtt/info";

const { proxy } = getCurrentInstance();

const mqttList = ref([]);
const open = ref(false);
const loading = ref(true);
const total = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    createdAt: null,
    ip: null,
    port: null,
    ping: null
  },
  rules: {
    ip: [{
      required: true, message: "IP不能为空", trigger: "blur"
    }],
    port: [{
      required: true, message: "端口不能为空", trigger: "blur"
    }],
    ping: [{
      required: true, message: '请选择当前状态', trigger: 'change'
    }]
  }
});

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

const statusOptions = ref([{
  value: 0,
  label: '禁用'
}, {
  value: 1,
  label: '启用'
}])

/** 查询mqtt信息列表列表 */
function getList() {
  loading.value = true;
  listMqtt(queryParams.value).then(response => {
    mqttList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

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

// 表单重置
function reset() {
  form.value = {
    id: null,
    ip: null,
    port: null,
    ping: null
  };
  proxy.resetForm("mqttRef");
}

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

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

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
}

// 编辑
function handleChange(row) {
  getMqtt(row.id).then(response => {
    form.value = response.data;
    open.value = true;
  });
}

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

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除HOST为"' + row.ip + row.port + '"的数据项？').then(function () {
    return delMqtt(row.id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

getList();

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