<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="所属设备序号 (0-9)" prop="deviceIndex">
        <el-input
          v-model="queryParams.deviceIndex"
          placeholder="请输入所属设备序号 (0-9)"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="寄存器起始地址" prop="regAddr">
        <el-input
          v-model="queryParams.regAddr"
          placeholder="请输入寄存器起始地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="寄存器长度" prop="regLength">
        <el-input
          v-model="queryParams.regLength"
          placeholder="请输入寄存器长度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报警是否启用" prop="alarmEnable">
        <el-input
          v-model="queryParams.alarmEnable"
          placeholder="请输入报警是否启用"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="低低报警值" prop="alarmLL">
        <el-input
          v-model="queryParams.alarmLL"
          placeholder="请输入低低报警值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="低报警值" prop="alarmL">
        <el-input
          v-model="queryParams.alarmL"
          placeholder="请输入低报警值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="高报警值" prop="alarmH">
        <el-input
          v-model="queryParams.alarmH"
          placeholder="请输入高报警值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="高高报警值" prop="alarmHh">
        <el-input
          v-model="queryParams.alarmHh"
          placeholder="请输入高高报警值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="原始范围最小值" prop="rawMin">
        <el-input
          v-model="queryParams.rawMin"
          placeholder="请输入原始范围最小值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="原始范围最大值" prop="rawMax">
        <el-input
          v-model="queryParams.rawMax"
          placeholder="请输入原始范围最大值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工程范围最小值" prop="engMin">
        <el-input
          v-model="queryParams.engMin"
          placeholder="请输入工程范围最小值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工程范围最大值" prop="engMax">
        <el-input
          v-model="queryParams.engMax"
          placeholder="请输入工程范围最大值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="补偿或旁路" prop="holdEnable">
        <el-select v-model="queryParams.holdEnable" placeholder="请选择补偿或旁路" clearable>
          <el-option
            v-for="dict in plc_hold_enable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="顺序启用" prop="orderEnable">
        <el-select v-model="queryParams.orderEnable" 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="dataType">
        <el-select v-model="queryParams.dataType" placeholder="请选择数据类型" clearable>
          <el-option
            v-for="dict in plc_e_data_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="字节序" prop="byteOrde">
        <el-select v-model="queryParams.byteOrde" 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>
        <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:paramconfig:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['plc:paramconfig:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['plc:paramconfig:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['plc:paramconfig:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="paramconfigList" @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="configName" />
      <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="所属设备序号 (0-9)" align="center" prop="deviceIndex" />
      <el-table-column label="寄存器起始地址" align="center" prop="regAddr" />
      <el-table-column label="寄存器长度" align="center" prop="regLength" />
      <el-table-column label="报警是否启用" align="center" prop="alarmEnable" />
      <el-table-column label="低低报警值" align="center" prop="alarmLL" />
      <el-table-column label="低报警值" align="center" prop="alarmL" />
      <el-table-column label="高报警值" align="center" prop="alarmH" />
      <el-table-column label="高高报警值" align="center" prop="alarmHh" />
      <el-table-column label="原始范围最小值" align="center" prop="rawMin" />
      <el-table-column label="原始范围最大值" align="center" prop="rawMax" />
      <el-table-column label="工程范围最小值" align="center" prop="engMin" />
      <el-table-column label="工程范围最大值" align="center" prop="engMax" />
      <el-table-column label="补偿或旁路" align="center" prop="holdEnable">
        <template #default="scope">
          <dict-tag :options="plc_hold_enable" :value="scope.row.holdEnable"/>
        </template>
      </el-table-column>
      <el-table-column label="顺序启用" align="center" prop="orderEnable">
        <template #default="scope">
          <dict-tag :options="plc_enable" :value="scope.row.orderEnable"/>
        </template>
      </el-table-column>
      <el-table-column label="数据类型" align="center" prop="dataType">
        <template #default="scope">
          <dict-tag :options="plc_e_data_type" :value="scope.row.dataType"/>
        </template>
      </el-table-column>
      <el-table-column label="字节序" align="center" prop="byteOrde">
        <template #default="scope">
          <dict-tag :options="plc_e_byte_order" :value="scope.row.byteOrde"/>
        </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" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['plc:paramconfig:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['plc:paramconfig: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="paramconfigRef" :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="configName">
          <el-input v-model="form.configName" 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="所属设备序号 (0-9)" prop="deviceIndex">
          <el-input v-model="form.deviceIndex" placeholder="请输入所属设备序号 (0-9)" />
        </el-form-item>
        <el-form-item label="寄存器起始地址" prop="regAddr">
          <el-input v-model="form.regAddr" placeholder="请输入寄存器起始地址" />
        </el-form-item>
        <el-form-item label="寄存器长度" prop="regLength">
          <el-input v-model="form.regLength" placeholder="请输入寄存器长度" />
        </el-form-item>
        <el-form-item label="报警是否启用" prop="alarmEnable">
          <el-input v-model="form.alarmEnable" placeholder="请输入报警是否启用" />
        </el-form-item>
        <el-form-item label="低低报警值" prop="alarmLL">
          <el-input v-model="form.alarmLL" placeholder="请输入低低报警值" />
        </el-form-item>
        <el-form-item label="低报警值" prop="alarmL">
          <el-input v-model="form.alarmL" placeholder="请输入低报警值" />
        </el-form-item>
        <el-form-item label="高报警值" prop="alarmH">
          <el-input v-model="form.alarmH" placeholder="请输入高报警值" />
        </el-form-item>
        <el-form-item label="高高报警值" prop="alarmHh">
          <el-input v-model="form.alarmHh" placeholder="请输入高高报警值" />
        </el-form-item>
        <el-form-item label="原始范围最小值" prop="rawMin">
          <el-input v-model="form.rawMin" placeholder="请输入原始范围最小值" />
        </el-form-item>
        <el-form-item label="原始范围最大值" prop="rawMax">
          <el-input v-model="form.rawMax" placeholder="请输入原始范围最大值" />
        </el-form-item>
        <el-form-item label="工程范围最小值" prop="engMin">
          <el-input v-model="form.engMin" placeholder="请输入工程范围最小值" />
        </el-form-item>
        <el-form-item label="工程范围最大值" prop="engMax">
          <el-input v-model="form.engMax" placeholder="请输入工程范围最大值" />
        </el-form-item>
        <el-form-item label="补偿或旁路" prop="holdEnable">
          <el-select v-model="form.holdEnable" placeholder="请选择补偿或旁路">
            <el-option
              v-for="dict in plc_hold_enable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="顺序启用" prop="orderEnable">
          <el-select v-model="form.orderEnable" 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="dataType">
          <el-select v-model="form.dataType" placeholder="请选择数据类型">
            <el-option
              v-for="dict in plc_e_data_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="字节序" prop="byteOrde">
          <el-select v-model="form.byteOrde" 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="主表id" prop="mianId">
          <el-input v-model="form.mianId" placeholder="请输入主表id" />
        </el-form-item>
        <el-divider content-position="center">参数信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddPlcStParamValue">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeletePlcStParamValue">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="plcStParamValueList" :row-class-name="rowPlcStParamValueIndex" @selection-change="handlePlcStParamValueSelectionChange" ref="plcStParamValue">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="工程值" prop="engValue" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.engValue" placeholder="请输入工程值" />
            </template>
          </el-table-column>
          <el-table-column label="卡采集的时候电压" prop="rawValue" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.rawValue" placeholder="请输入卡采集的时候电压" />
            </template>
          </el-table-column>
          <el-table-column label="上次采集值" prop="lastValue" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.lastValue" placeholder="请输入上次采集值" />
            </template>
          </el-table-column>
          <el-table-column label="保持或者旁路值" prop="holdValue" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.holdValue" placeholder="请输入保持或者旁路值" />
            </template>
          </el-table-column>
          <el-table-column label="报警状态" prop="alarmStatus" width="150">
            <template #default="scope">
              <el-select v-model="scope.row.alarmStatus" placeholder="请选择报警状态">
                <el-option
                  v-for="dict in plc_alarm_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="采集更新时间" prop="cjUpdateTime" width="240">
            <template #default="scope">
              <el-date-picker clearable
                v-model="scope.row.cjUpdateTime"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择采集更新时间">
              </el-date-picker>
            </template>
          </el-table-column>
        </el-table>
      </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="Paramconfig">
import { listParamconfig, getParamconfig, delParamconfig, addParamconfig, updateParamconfig } from "@/api/production/plc/paramconfig/paramconfig"

const { proxy } = getCurrentInstance()
const { plc_hold_enable, plc_e_byte_order, plc_enable, plc_alarm_status, plc_e_data_type, plc_e_word_order } = proxy.useDict('plc_hold_enable', 'plc_e_byte_order', 'plc_enable', 'plc_alarm_status', 'plc_e_data_type', 'plc_e_word_order')

const paramconfigList = ref([])
const plcStParamValueList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const checkedPlcStParamValue = 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,
    configName: null,
    enable: null,
    deviceIndex: null,
    regAddr: null,
    regLength: null,
    alarmEnable: null,
    alarmLL: null,
    alarmL: null,
    alarmH: null,
    alarmHh: null,
    rawMin: null,
    rawMax: null,
    engMin: null,
    engMax: null,
    holdEnable: null,
    orderEnable: null,
    dataType: null,
    byteOrde: null,
    wordOrder: null,
  },
  rules: {
  }
})

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

/** 查询参数配置列表 */
function getList() {
  loading.value = true
  listParamconfig(queryParams.value).then(response => {
    paramconfigList.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,
    configName: null,
    enable: null,
    deviceIndex: null,
    regAddr: null,
    regLength: null,
    alarmEnable: null,
    alarmLL: null,
    alarmL: null,
    alarmH: null,
    alarmHh: null,
    rawMin: null,
    rawMax: null,
    engMin: null,
    engMax: null,
    holdEnable: null,
    orderEnable: null,
    dataType: null,
    byteOrde: null,
    wordOrder: null,
    mianId: null
  }
  plcStParamValueList.value = []
  proxy.resetForm("paramconfigRef")
}

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

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

/** 参数序号 */
function rowPlcStParamValueIndex({ row, rowIndex }) {
  row.index = rowIndex + 1
}

/** 参数添加按钮操作 */
function handleAddPlcStParamValue() {
  let obj = {}
  obj.remark = ""
  obj.engValue = ""
  obj.rawValue = ""
  obj.lastValue = ""
  obj.holdValue = ""
  obj.alarmStatus = ""
  obj.cjUpdateTime = ""
  plcStParamValueList.value.push(obj)
}

/** 参数删除按钮操作 */
function handleDeletePlcStParamValue() {
  if (checkedPlcStParamValue.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的参数数据")
  } else {
    const plcStParamValues = plcStParamValueList.value
    const checkedPlcStParamValues = checkedPlcStParamValue.value
    plcStParamValueList.value = plcStParamValues.filter(function(item) {
      return checkedPlcStParamValues.indexOf(item.index) == -1
    })
  }
}

/** 复选框选中数据 */
function handlePlcStParamValueSelectionChange(selection) {
  checkedPlcStParamValue.value = selection.map(item => item.index)
}

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

getList()
</script>
