<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备是否启用" prop="enable">
        <el-select v-model="queryParams.enable" placeholder="请选择设备是否启用" clearable>
          <el-option
            v-for="dict in plc_enable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="通信类型" prop="commType">
        <el-select v-model="queryParams.commType" placeholder="请选择通信类型" clearable>
          <el-option
            v-for="dict in plc_e_comm_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="IP第1段" prop="ipAddr1">
        <el-input
          v-model="queryParams.ipAddr1"
          placeholder="请输入IP第1段"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="IP第2段" prop="ipAddr2">
        <el-input
          v-model="queryParams.ipAddr2"
          placeholder="请输入IP第2段"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="IP第3段" prop="ipAddr3">
        <el-input
          v-model="queryParams.ipAddr3"
          placeholder="请输入IP第3段"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="IP第4段" prop="ipAddr4">
        <el-input
          v-model="queryParams.ipAddr4"
          placeholder="请输入IP第4段"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="端口号" prop="port">
        <el-input
          v-model="queryParams.port"
          placeholder="请输入端口号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="PLC串口号" prop="wComPort">
        <el-input
          v-model="queryParams.wComPort"
          placeholder="请输入PLC串口号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="485设备地址地址" prop="rtyAddr">
        <el-input
          v-model="queryParams.rtyAddr"
          placeholder="请输入485设备地址地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="波特率" prop="eBaudRate">
        <el-select v-model="queryParams.eBaudRate" placeholder="请选择波特率" clearable>
          <el-option
            v-for="dict in plc_e_baud_rate"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="数据位" prop="eDataBits">
        <el-select v-model="queryParams.eDataBits" placeholder="请选择数据位" clearable>
          <el-option
            v-for="dict in plc_e_data_bits"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="校验位" prop="eParityBits">
        <el-select v-model="queryParams.eParityBits" placeholder="请选择校验位" clearable>
          <el-option
            v-for="dict in plc_e_parity_bites"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="停止位" prop="eStopBits">
        <el-select v-model="queryParams.eStopBits" placeholder="请选择停止位" clearable>
          <el-option
            v-for="dict in plc_e_stop_bits"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="字节序" prop="byteOrder">
        <el-select v-model="queryParams.byteOrder" placeholder="请选择字节序" clearable>
          <el-option
            v-for="dict in plc_e_byte_order"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="字序" prop="wordOrder">
        <el-select v-model="queryParams.wordOrder" placeholder="请选择字序" clearable>
          <el-option
            v-for="dict in plc_e_word_order"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备采集周期" prop="tCollectCycle">
        <el-input
          v-model="queryParams.tCollectCycle"
          placeholder="请输入设备采集周期"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="功能码" prop="functionCode">
        <el-input
          v-model="queryParams.functionCode"
          placeholder="请输入功能码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="起始地址" prop="startAddr">
        <el-input
          v-model="queryParams.startAddr"
          placeholder="请输入起始地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="寄存器数量" prop="quantity">
        <el-input
          v-model="queryParams.quantity"
          placeholder="请输入寄存器数量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据长度" prop="dataLen">
        <el-input
          v-model="queryParams.dataLen"
          placeholder="请输入数据长度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参数数量" prop="paramCount">
        <el-input
          v-model="queryParams.paramCount"
          placeholder="请输入参数数量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="超时时间" prop="timeout">
        <el-input
          v-model="queryParams.timeout"
          placeholder="请输入超时时间"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="重试次数" prop="retryTimes">
        <el-input
          v-model="queryParams.retryTimes"
          placeholder="请输入重试次数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="在线状态" prop="isOnline">
        <el-select v-model="queryParams.isOnline" placeholder="请选择在线状态" clearable>
          <el-option
            v-for="dict in plc_is_online"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="最后通讯时间" prop="lastCommTime">
        <el-input
          v-model="queryParams.lastCommTime"
          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="['plc:deviceconfig:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['plc:deviceconfig:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['plc:deviceconfig:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['plc:deviceconfig:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceconfigList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="设备是否启用" align="center" prop="enable">
        <template #default="scope">
          <dict-tag :options="plc_enable" :value="scope.row.enable"/>
        </template>
      </el-table-column>
      <el-table-column label="通信类型" align="center" prop="commType">
        <template #default="scope">
          <dict-tag :options="plc_e_comm_type" :value="scope.row.commType"/>
        </template>
      </el-table-column>
      <el-table-column label="IP第1段" align="center" prop="ipAddr1" />
      <el-table-column label="IP第2段" align="center" prop="ipAddr2" />
      <el-table-column label="IP第3段" align="center" prop="ipAddr3" />
      <el-table-column label="IP第4段" align="center" prop="ipAddr4" />
      <el-table-column label="端口号" align="center" prop="port" />
      <el-table-column label="PLC串口号" align="center" prop="wComPort" />
      <el-table-column label="485设备地址地址" align="center" prop="rtyAddr" />
      <el-table-column label="波特率" align="center" prop="eBaudRate">
        <template #default="scope">
          <dict-tag :options="plc_e_baud_rate" :value="scope.row.eBaudRate"/>
        </template>
      </el-table-column>
      <el-table-column label="数据位" align="center" prop="eDataBits">
        <template #default="scope">
          <dict-tag :options="plc_e_data_bits" :value="scope.row.eDataBits"/>
        </template>
      </el-table-column>
      <el-table-column label="校验位" align="center" prop="eParityBits">
        <template #default="scope">
          <dict-tag :options="plc_e_parity_bites" :value="scope.row.eParityBits"/>
        </template>
      </el-table-column>
      <el-table-column label="停止位" align="center" prop="eStopBits">
        <template #default="scope">
          <dict-tag :options="plc_e_stop_bits" :value="scope.row.eStopBits"/>
        </template>
      </el-table-column>
      <el-table-column label="字节序" align="center" prop="byteOrder">
        <template #default="scope">
          <dict-tag :options="plc_e_byte_order" :value="scope.row.byteOrder"/>
        </template>
      </el-table-column>
      <el-table-column label="字序" align="center" prop="wordOrder">
        <template #default="scope">
          <dict-tag :options="plc_e_word_order" :value="scope.row.wordOrder"/>
        </template>
      </el-table-column>
      <el-table-column label="设备采集周期" align="center" prop="tCollectCycle" />
      <el-table-column label="功能码" align="center" prop="functionCode" />
      <el-table-column label="起始地址" align="center" prop="startAddr" />
      <el-table-column label="寄存器数量" align="center" prop="quantity" />
      <el-table-column label="数据长度" align="center" prop="dataLen" />
      <el-table-column label="参数数量" align="center" prop="paramCount" />
      <el-table-column label="超时时间" align="center" prop="timeout" />
      <el-table-column label="重试次数" align="center" prop="retryTimes" />
      <el-table-column label="在线状态" align="center" prop="isOnline">
        <template #default="scope">
          <dict-tag :options="plc_is_online" :value="scope.row.isOnline"/>
        </template>
      </el-table-column>
      <el-table-column label="最后通讯时间" align="center" prop="lastCommTime" />
      <el-table-column label="参数索引数组(json格式)" align="center" prop="paramIndex" />
      <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="['plc:deviceconfig:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['plc:deviceconfig: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="deviceconfigRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="设备是否启用" prop="enable">
          <el-select v-model="form.enable" placeholder="请选择设备是否启用">
            <el-option
              v-for="dict in plc_enable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="通信类型" prop="commType">
          <el-select v-model="form.commType" placeholder="请选择通信类型">
            <el-option
              v-for="dict in plc_e_comm_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="IP第1段" prop="ipAddr1">
          <el-input v-model="form.ipAddr1" placeholder="请输入IP第1段" />
        </el-form-item>
        <el-form-item label="IP第2段" prop="ipAddr2">
          <el-input v-model="form.ipAddr2" placeholder="请输入IP第2段" />
        </el-form-item>
        <el-form-item label="IP第3段" prop="ipAddr3">
          <el-input v-model="form.ipAddr3" placeholder="请输入IP第3段" />
        </el-form-item>
        <el-form-item label="IP第4段" prop="ipAddr4">
          <el-input v-model="form.ipAddr4" placeholder="请输入IP第4段" />
        </el-form-item>
        <el-form-item label="端口号" prop="port">
          <el-input v-model="form.port" placeholder="请输入端口号" />
        </el-form-item>
        <el-form-item label="PLC串口号" prop="wComPort">
          <el-input v-model="form.wComPort" placeholder="请输入PLC串口号" />
        </el-form-item>
        <el-form-item label="485设备地址地址" prop="rtyAddr">
          <el-input v-model="form.rtyAddr" placeholder="请输入485设备地址地址" />
        </el-form-item>
        <el-form-item label="波特率" prop="eBaudRate">
          <el-select v-model="form.eBaudRate" placeholder="请选择波特率">
            <el-option
              v-for="dict in plc_e_baud_rate"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数据位" prop="eDataBits">
          <el-select v-model="form.eDataBits" placeholder="请选择数据位">
            <el-option
              v-for="dict in plc_e_data_bits"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="校验位" prop="eParityBits">
          <el-select v-model="form.eParityBits" placeholder="请选择校验位">
            <el-option
              v-for="dict in plc_e_parity_bites"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="停止位" prop="eStopBits">
          <el-select v-model="form.eStopBits" placeholder="请选择停止位">
            <el-option
              v-for="dict in plc_e_stop_bits"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="字节序" prop="byteOrder">
          <el-select v-model="form.byteOrder" placeholder="请选择字节序">
            <el-option
              v-for="dict in plc_e_byte_order"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="字序" prop="wordOrder">
          <el-select v-model="form.wordOrder" placeholder="请选择字序">
            <el-option
              v-for="dict in plc_e_word_order"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备采集周期" prop="tCollectCycle">
          <el-input v-model="form.tCollectCycle" placeholder="请输入设备采集周期" />
        </el-form-item>
        <el-form-item label="功能码" prop="functionCode">
          <el-input v-model="form.functionCode" placeholder="请输入功能码" />
        </el-form-item>
        <el-form-item label="起始地址" prop="startAddr">
          <el-input v-model="form.startAddr" placeholder="请输入起始地址" />
        </el-form-item>
        <el-form-item label="寄存器数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入寄存器数量" />
        </el-form-item>
        <el-form-item label="数据长度" prop="dataLen">
          <el-input v-model="form.dataLen" placeholder="请输入数据长度" />
        </el-form-item>
        <el-form-item label="参数数量" prop="paramCount">
          <el-input v-model="form.paramCount" placeholder="请输入参数数量" />
        </el-form-item>
        <el-form-item label="超时时间" prop="timeout">
          <el-input v-model="form.timeout" placeholder="请输入超时时间" />
        </el-form-item>
        <el-form-item label="重试次数" prop="retryTimes">
          <el-input v-model="form.retryTimes" placeholder="请输入重试次数" />
        </el-form-item>
        <el-form-item label="在线状态" prop="isOnline">
          <el-select v-model="form.isOnline" placeholder="请选择在线状态">
            <el-option
              v-for="dict in plc_is_online"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="最后通讯时间" prop="lastCommTime">
          <el-input v-model="form.lastCommTime" placeholder="请输入最后通讯时间" />
        </el-form-item>
        <el-form-item label="主表id" prop="mainId">
          <el-input v-model="form.mainId" placeholder="请输入主表id" />
        </el-form-item>
        <el-form-item label="参数索引数组(json格式)" prop="paramIndex">
          <el-input v-model="form.paramIndex" type="textarea" 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="Deviceconfig">
import { listDeviceconfig, getDeviceconfig, delDeviceconfig, addDeviceconfig, updateDeviceconfig } from "@/api/production/plc/deviceconfig/deviceconfig"

const { proxy } = getCurrentInstance()
const { plc_e_byte_order, plc_e_data_bits, plc_e_parity_bites, plc_enable, plc_e_stop_bits, plc_is_online, plc_e_word_order, plc_e_comm_type, plc_e_baud_rate } = proxy.useDict('plc_e_byte_order', 'plc_e_data_bits', 'plc_e_parity_bites', 'plc_enable', 'plc_e_stop_bits', 'plc_is_online', 'plc_e_word_order', 'plc_e_comm_type', 'plc_e_baud_rate')

const deviceconfigList = 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,
    deviceName: null,
    enable: null,
    commType: null,
    ipAddr1: null,
    ipAddr2: null,
    ipAddr3: null,
    ipAddr4: null,
    port: null,
    wComPort: null,
    rtyAddr: null,
    eBaudRate: null,
    eDataBits: null,
    eParityBits: null,
    eStopBits: null,
    byteOrder: null,
    wordOrder: null,
    tCollectCycle: null,
    functionCode: null,
    startAddr: null,
    quantity: null,
    dataLen: null,
    paramCount: null,
    timeout: null,
    retryTimes: null,
    isOnline: null,
    lastCommTime: null,
    paramIndex: null
  },
  rules: {
  }
})

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

/** 查询设备配置列表 */
function getList() {
  loading.value = true
  listDeviceconfig(queryParams.value).then(response => {
    deviceconfigList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

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

// 表单重置
function reset() {
  form.value = {
    id: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    deviceName: null,
    enable: null,
    commType: null,
    ipAddr1: null,
    ipAddr2: null,
    ipAddr3: null,
    ipAddr4: null,
    port: null,
    wComPort: null,
    rtyAddr: null,
    eBaudRate: null,
    eDataBits: null,
    dYl1: null,
    eParityBits: null,
    eStopBits: null,
    byteOrder: null,
    wordOrder: null,
    tCollectCycle: null,
    functionCode: null,
    startAddr: null,
    quantity: null,
    dataLen: null,
    paramCount: null,
    timeout: null,
    retryTimes: null,
    isOnline: null,
    lastCommTime: null,
    mainId: null,
    paramIndex: null
  }
  proxy.resetForm("deviceconfigRef")
}

/** 搜索按钮操作 */
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
  getDeviceconfig(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改设备配置"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["deviceconfigRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateDeviceconfig(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addDeviceconfig(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 delDeviceconfig(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

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

getList()
</script>
