<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="寄存器地址" prop="address">
        <el-input v-model="queryParams.address" placeholder="请输入寄存器地址" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="寄存器名称" prop="registerName">
        <el-input v-model="queryParams.registerName" placeholder="请输入寄存器名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="设备数据类型" prop="dataType">
        <el-select v-model="queryParams.dataType" placeholder="请选择设备数据类型" clearable style="width: 200px;">
          <el-option v-for="dict in dcs_data_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="标识码" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入标识码" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="寄存器类型" prop="registerCode">
        <el-select v-model="queryParams.registerCode" placeholder="请选择寄存器类型" clearable style="width: 200px;">
          <el-option v-for="dict in dcs_register_code" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="单位类型" prop="unitType">
        <el-select v-model="queryParams.unitType" placeholder="请选择单位类型" clearable style="width: 200px;">
          <el-option v-for="dict in unit_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="参数类型" prop="paramType">
        <el-select v-model="queryParams.paramType" placeholder="请选择参数类型" clearable style="width: 200px;">
          <el-option v-for="dict in param_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable style="width: 240px">
          <el-option v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.label" :value="dict.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-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="['hnyz:param:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['hnyz:param:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['hnyz:param:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['hnyz:param:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="paramList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="寄存器id" align="center" prop="registerId" />
      <el-table-column label="归属设备" align="center" prop="equipmentName" />
      <el-table-column label="关联设备" align="center" prop="relatedEquipmentName" />
      <el-table-column label="寄存器地址" align="center" prop="address" />
      <el-table-column label="寄存器名称" align="center" prop="registerName" />
      <el-table-column label="设备数据类型" align="center" prop="dataType">
        <template #default="scope">
          <dict-tag :options="dcs_data_type" :value="scope.row.dataType" />
        </template>
      </el-table-column>
      <el-table-column label="唯一标识码" align="center" prop="code" />
      <el-table-column label="寄存器类型" align="center" prop="registerCode">
        <template #default="scope">
          <dict-tag :options="dcs_register_code" :value="scope.row.registerCode" />
        </template>
      </el-table-column>
      <el-table-column label="单位类型" align="center" prop="unitType">
        <template #default="scope">
          <dict-tag :options="unit_type" :value="scope.row.unitType" />
        </template>
      </el-table-column>
      <el-table-column label="参数类型" align="center" prop="paramType">
        <template #default="scope">
          <dict-tag :options="param_type" :value="scope.row.paramType" />
        </template>
      </el-table-column>
      <el-table-column label="启用状态" align="center" width="100">
        <template #default="scope">
          <el-switch v-model="scope.row.status" active-value="0" inactive-value="1"
            @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['hnyz:param:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['hnyz:param: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="500px" append-to-body>
      <el-form ref="paramRef" :model="form" :rules="rules" label-width="100px" label-position="left">
        <el-form-item label="寄存器地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入寄存器地址" />
        </el-form-item>
        <el-form-item label="寄存器名称" prop="registerName">
          <el-input v-model="form.registerName" placeholder="请输入寄存器名称" />
        </el-form-item>
        <el-form-item label="数据类型" prop="dataType">
          <el-select v-model="form.dataType" placeholder="请选择设备数据类型">
            <el-option v-for="dict in dcs_data_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="唯一标识码" prop="code">
          <el-input v-model="form.code" placeholder="请输入唯一标识码" />
        </el-form-item>
        <el-form-item label="寄存器类型" prop="registerCode">
          <el-select v-model="form.registerCode" placeholder="请选择寄存器类型">
            <el-option v-for="dict in dcs_register_code" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单位类型" prop="unitType">
          <el-select v-model="form.unitType" placeholder="请选择单位类型">
            <el-option v-for="dict in unit_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="参数类型" prop="paramType">
          <el-select v-model="form.paramType" placeholder="请选择参数类型">
            <el-option v-for="dict in param_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="归属设备" prop="equipmentId">
          <el-select v-model="form.equipmentId" placeholder="请选择归属设备" filterable>
            <el-option v-for="dict in equipmentOptions" :key="dict.equipmentId" :label="dict.equipmentName"
              :value="dict.equipmentId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关联设备" prop="relationId">
          <!-- filterable支持搜索功能 -->
          <el-select v-model="form.relationId" placeholder="请选择关联设备" filterable>
            <el-option v-for="dict in equipmentOptions" :key="dict.equipmentId" :label="dict.equipmentName"
              :value="dict.equipmentId"></el-option>
          </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="启用状态">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in sys_normal_disable" :key="dict.value" :value="dict.value">{{ dict.label
            }}</el-radio>
          </el-radio-group>
        </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="Param">
import { changeStatus,listParam, getParam, delParam, addParam, updateParam } from "@/api/hnyz/param";
import { getEquipmentList } from "@/api/hnyz/equipment";
import { onMounted } from "vue";

onMounted(() => {
  getEquipmentOptions();
});

const equipmentOptions = ref([]);// 设备下拉树结构
/** 获取设备列表 */
function getEquipmentOptions() {
  getEquipmentList().then(response => {
    equipmentOptions.value = response.data;
  });
}
const { proxy } = getCurrentInstance();
const { unit_type, dcs_register_code, param_type, dcs_data_type } = proxy.useDict('unit_type', 'dcs_register_code', 'param_type', 'dcs_data_type');
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");
const paramList = 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,
    equipmentId: null,
    relationId: null,
    address: null,
    registerName: null,
    dataType: null,
    code: null,
    registerCode: null,
    unitType: null,
    paramType: null,
    status: null,
  },
  rules: {
    equipmentId: [
      { required: true, message: "归属设备不能为空", trigger: "change" }
    ],
    relationId: [
      { required: true, message: "关联设备不能为空", trigger: "change" }
    ],
    address: [
      { required: true, message: "寄存器地址不能为空", trigger: "blur" }
    ],
    registerName: [
      { required: true, message: "寄存器名称不能为空", trigger: "blur" }
    ],
    dataType: [
      { required: true, message: "设备数据类型不能为空", trigger: "change" }
    ],
    code: [
      { required: true, message: "唯一标识码不能为空", trigger: "blur" }
    ],
    registerCode: [
      { required: true, message: "寄存器类型不能为空", trigger: "change" }
    ],
    unitType: [
      { required: true, message: "单位类型不能为空", trigger: "change" }
    ],
    paramType: [
      { required: true, message: "参数类型不能为空", trigger: "change" }
    ]
  }
});

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

/** 查询设备参数管理列表 */
function getList() {
  loading.value = true;
  listParam(queryParams.value).then(response => {
    paramList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

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

// 表单重置
function reset() {
  form.value = {
    registerId: null,
    equipmentId: null,
    relationId: null,
    address: null,
    registerName: null,
    dataType: null,
    code: null,
    registerCode: null,
    unitType: null,
    paramType: null,
    status: "0",
    remark: null,
  };
  proxy.resetForm("paramRef");
}

/** 状态修改 */
function handleStatusChange(row) {
  let text = row.status == "0" ? "启用" : "停用";
  proxy.$modal.confirm('确认要"' + text + '"寄存器名为"' + row.registerName + '"的数据项吗?').then(function () {
    return changeStatus(row.registerId, row.status);
  }).then(() => {
    proxy.$modal.msgSuccess(text + "成功");
  }).catch(function () {
    row.status = row.status == "0" ? "1" : "0";
  });
}


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

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

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

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

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _registerId = row.registerId || ids.value
  getParam(_registerId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改设备参数管理";
  });
}

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

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

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

getList();
</script>
