<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="模式" prop="mode">
        <el-input v-model="queryParams.mode" placeholder="请输入模式" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="时长" prop="duration">
        <el-input v-model="queryParams.duration" placeholder="请输入时长" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="订阅命令" prop="subscribe">
        <el-input v-model="queryParams.subscribe" placeholder="请输入订阅命令" clearable @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 type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['system:command:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:command:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:command:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['system:command:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commandList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="命令" align="center" prop="command" />
      <el-table-column label="模式" align="center" prop="mode" />
      <el-table-column label="时长" align="center" prop="duration" />
      <el-table-column label="订阅命令" align="center" prop="subscribe" />
      <el-table-column label="设备类型" align="center" prop="deviceType" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button type="text" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:command:edit']">修改</el-button>
          <el-button type="text" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:command: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="title" v-model="open" width="1200px" append-to-body>
      <el-form ref="commandRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="命令实体" prop="command">
          <el-input v-model="form.command" placeholder="请输入命令实体" />
        </el-form-item>
        <el-form-item label="模式" prop="mode">
          <el-select v-model="form.mode" class="m-2" placeholder="Select">
            <el-option v-for="item in dicst['command_mode']" :key="item.dictValue" :label="item.dictLabel"
              :value="item.dictValue" />
          </el-select>
          <!-- <el-input v-model="form.mode" placeholder="请输入模式" /> -->
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceType">

          <el-select v-model="form.deviceType" class="m-2" placeholder="Select">
            <el-option v-for="item in dicst['device_type']" :key="item.dictValue" :label="item.dictLabel"
              :value="item.dictValue" />
          </el-select>


          <!-- <el-input v-model="form.deviceType" placeholder="设备类型" /> -->
        </el-form-item>
        <el-form-item label="时长" prop="duration">
          <el-input v-model="form.duration" placeholder="请输入时长" />
        </el-form-item>
        <el-form-item label="订阅命令" prop="subscribe">
          <el-input v-model="form.subscribe" placeholder="请输入订阅命令" />
        </el-form-item>
        <el-form-item label="命令描述" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入命令描述" />
          <!-- <editor v-model="form.remark" :min-height="192" /> -->
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" 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="Command">
import { listCommand, getCommand, delCommand, addCommand, updateCommand } from "@/api/system/command";
import { getDicts } from "@/api/system/dict/data";

const { proxy } = getCurrentInstance();

const commandList = 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 dicst = ref({})
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    command: null,
    mode: null,
    duration: null,
    subscribe: null,
    deviceType: null,
    sort: null
  },
  rules: {
  }
});

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

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

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

// 表单重置
function reset() {
  form.value = {
    commandId: null,
    command: null,
    mode: null,
    duration: null,
    subscribe: null,
    remark: null,
    deviceType: null,
    sort: null
  };
  proxy.resetForm("commandRef");
}

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

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

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

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

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const commandId = row.commandId || ids.value
  getCommand(commandId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改设备命令";
  });
}

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

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

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

const findDists = () => {
  let dictTypes = ['command_mode', 'device_type']
  let array = []
  for (const item of dictTypes) {
    array.push(getDicts(item))
  }
  Promise.all(array).then(res => {
    for (let i = 0; i < dictTypes.length; i++) {
      dicst.value[dictTypes[i]] = res[i].data
    }
    let booleanDist = [{
      dictValue: true,
      dictLabel: '是'
    }, {
      dictValue: false,
      dictLabel: '否'
    }]
    dicst.value['booleanDist'] = booleanDist
  })
}
findDists();
getList();
</script>
