<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="运行状态" prop="dRunStatus">
        <el-select v-model="queryParams.dRunStatus" placeholder="请选择运行状态" clearable>
          <el-option
            v-for="dict in plc_run_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="补偿标志" prop="dHoldFlag">
        <el-select v-model="queryParams.dHoldFlag" placeholder="请选择补偿标志" clearable>
          <el-option
            v-for="dict in plc_hold_flag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="压力值" prop="dPf">
        <el-input
          v-model="queryParams.dPf"
          placeholder="请输入压力值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="温度值" prop="dTf">
        <el-input
          v-model="queryParams.dTf"
          placeholder="请输入温度值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="差压值" prop="dDp">
        <el-input
          v-model="queryParams.dDp"
          placeholder="请输入差压值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="脉冲数" prop="dPulseNum">
        <el-input
          v-model="queryParams.dPulseNum"
          placeholder="请输入脉冲数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工况流量输入值" prop="dVflowfInput">
        <el-input
          v-model="queryParams.dVflowfInput"
          placeholder="请输入工况流量输入值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="压力补偿值" prop="dPfHold">
        <el-input
          v-model="queryParams.dPfHold"
          placeholder="请输入压力补偿值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="温度补偿值" prop="dTfHold">
        <el-input
          v-model="queryParams.dTfHold"
          placeholder="请输入温度补偿值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="差压补偿值" prop="dDpHold">
        <el-input
          v-model="queryParams.dDpHold"
          placeholder="请输入差压补偿值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="脉冲数补偿值" prop="dPulseNumHold">
        <el-input
          v-model="queryParams.dPulseNumHold"
          placeholder="请输入脉冲数补偿值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工况流量补偿值" prop="dVflowfinputHold">
        <el-input
          v-model="queryParams.dVflowfinputHold"
          placeholder="请输入工况流量补偿值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="压力参数索引" prop="dPfParamsIndex">
        <el-input
          v-model="queryParams.dPfParamsIndex"
          placeholder="请输入压力参数索引"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="温度参数索引" prop="dTfParamsIndex">
        <el-input
          v-model="queryParams.dTfParamsIndex"
          placeholder="请输入温度参数索引"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="差压参数索引" prop="dDpParamsIndex">
        <el-input
          v-model="queryParams.dDpParamsIndex"
          placeholder="请输入差压参数索引"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="脉冲数参数索引" prop="dPulseNumParamsIndex">
        <el-input
          v-model="queryParams.dPulseNumParamsIndex"
          placeholder="请输入脉冲数参数索引"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工况流量参数索引" prop="dVflowfinputParamsIndex">
        <el-input
          v-model="queryParams.dVflowfinputParamsIndex"
          placeholder="请输入工况流量参数索引"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留字段1" prop="dRyl1">
        <el-input
          v-model="queryParams.dRyl1"
          placeholder="请输入预留字段1"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留字段2" prop="dRyl2">
        <el-input
          v-model="queryParams.dRyl2"
          placeholder="请输入预留字段2"
          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:engineering:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['plc:engineering:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['plc:engineering:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['plc:engineering:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="engineeringList" @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="dRunStatus">
        <template #default="scope">
          <dict-tag :options="plc_run_status" :value="scope.row.dRunStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="补偿标志" align="center" prop="dHoldFlag">
        <template #default="scope">
          <dict-tag :options="plc_hold_flag" :value="scope.row.dHoldFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="压力值" align="center" prop="dPf" />
      <el-table-column label="温度值" align="center" prop="dTf" />
      <el-table-column label="差压值" align="center" prop="dDp" />
      <el-table-column label="脉冲数" align="center" prop="dPulseNum" />
      <el-table-column label="工况流量输入值" align="center" prop="dVflowfInput" />
      <el-table-column label="压力补偿值" align="center" prop="dPfHold" />
      <el-table-column label="温度补偿值" align="center" prop="dTfHold" />
      <el-table-column label="差压补偿值" align="center" prop="dDpHold" />
      <el-table-column label="脉冲数补偿值" align="center" prop="dPulseNumHold" />
      <el-table-column label="工况流量补偿值" align="center" prop="dVflowfinputHold" />
      <el-table-column label="压力参数索引" align="center" prop="dPfParamsIndex" />
      <el-table-column label="温度参数索引" align="center" prop="dTfParamsIndex" />
      <el-table-column label="差压参数索引" align="center" prop="dDpParamsIndex" />
      <el-table-column label="脉冲数参数索引" align="center" prop="dPulseNumParamsIndex" />
      <el-table-column label="工况流量参数索引" align="center" prop="dVflowfinputParamsIndex" />
      <el-table-column label="预留字段1" align="center" prop="dRyl1" />
      <el-table-column label="预留字段2" align="center" prop="dRyl2" />
      <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:engineering:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['plc:engineering: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="engineeringRef" :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="dRunStatus">
          <el-select v-model="form.dRunStatus" placeholder="请选择运行状态">
            <el-option
              v-for="dict in plc_run_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="补偿标志" prop="dHoldFlag">
          <el-select v-model="form.dHoldFlag" placeholder="请选择补偿标志">
            <el-option
              v-for="dict in plc_hold_flag"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="压力值" prop="dPf">
          <el-input v-model="form.dPf" placeholder="请输入压力值" />
        </el-form-item>
        <el-form-item label="温度值" prop="dTf">
          <el-input v-model="form.dTf" placeholder="请输入温度值" />
        </el-form-item>
        <el-form-item label="差压值" prop="dDp">
          <el-input v-model="form.dDp" placeholder="请输入差压值" />
        </el-form-item>
        <el-form-item label="脉冲数" prop="dPulseNum">
          <el-input v-model="form.dPulseNum" placeholder="请输入脉冲数" />
        </el-form-item>
        <el-form-item label="工况流量输入值" prop="dVflowfInput">
          <el-input v-model="form.dVflowfInput" placeholder="请输入工况流量输入值" />
        </el-form-item>
        <el-form-item label="压力补偿值" prop="dPfHold">
          <el-input v-model="form.dPfHold" placeholder="请输入压力补偿值" />
        </el-form-item>
        <el-form-item label="温度补偿值" prop="dTfHold">
          <el-input v-model="form.dTfHold" placeholder="请输入温度补偿值" />
        </el-form-item>
        <el-form-item label="差压补偿值" prop="dDpHold">
          <el-input v-model="form.dDpHold" placeholder="请输入差压补偿值" />
        </el-form-item>
        <el-form-item label="脉冲数补偿值" prop="dPulseNumHold">
          <el-input v-model="form.dPulseNumHold" placeholder="请输入脉冲数补偿值" />
        </el-form-item>
        <el-form-item label="工况流量补偿值" prop="dVflowfinputHold">
          <el-input v-model="form.dVflowfinputHold" placeholder="请输入工况流量补偿值" />
        </el-form-item>
        <el-form-item label="压力参数索引" prop="dPfParamsIndex">
          <el-input v-model="form.dPfParamsIndex" placeholder="请输入压力参数索引" />
        </el-form-item>
        <el-form-item label="温度参数索引" prop="dTfParamsIndex">
          <el-input v-model="form.dTfParamsIndex" placeholder="请输入温度参数索引" />
        </el-form-item>
        <el-form-item label="差压参数索引" prop="dDpParamsIndex">
          <el-input v-model="form.dDpParamsIndex" placeholder="请输入差压参数索引" />
        </el-form-item>
        <el-form-item label="脉冲数参数索引" prop="dPulseNumParamsIndex">
          <el-input v-model="form.dPulseNumParamsIndex" placeholder="请输入脉冲数参数索引" />
        </el-form-item>
        <el-form-item label="工况流量参数索引" prop="dVflowfinputParamsIndex">
          <el-input v-model="form.dVflowfinputParamsIndex" placeholder="请输入工况流量参数索引" />
        </el-form-item>
        <el-form-item label="预留字段1" prop="dRyl1">
          <el-input v-model="form.dRyl1" placeholder="请输入预留字段1" />
        </el-form-item>
        <el-form-item label="预留字段2" prop="dRyl2">
          <el-input v-model="form.dRyl2" placeholder="请输入预留字段2" />
        </el-form-item>
        <el-form-item label="主表id" prop="mainId">
          <el-input v-model="form.mainId" placeholder="请输入主表id" />
        </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="Engineering">
import { listEngineering, getEngineering, delEngineering, addEngineering, updateEngineering } from "@/api/production/plc/engineering/engineering"

const { proxy } = getCurrentInstance()
const { plc_hold_flag, plc_run_status } = proxy.useDict('plc_hold_flag', 'plc_run_status')

const engineeringList = 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,
    dRunStatus: null,
    dHoldFlag: null,
    dPf: null,
    dTf: null,
    dDp: null,
    dPulseNum: null,
    dVflowfInput: null,
    dPfHold: null,
    dTfHold: null,
    dDpHold: null,
    dPulseNumHold: null,
    dVflowfinputHold: null,
    dPfParamsIndex: null,
    dTfParamsIndex: null,
    dDpParamsIndex: null,
    dPulseNumParamsIndex: null,
    dVflowfinputParamsIndex: null,
    dRyl1: null,
    dRyl2: null,
  },
  rules: {
  }
})

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

/** 查询流量输入工程数据列表 */
function getList() {
  loading.value = true
  listEngineering(queryParams.value).then(response => {
    engineeringList.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,
    dRunStatus: null,
    dHoldFlag: null,
    dPf: null,
    dTf: null,
    dDp: null,
    dPulseNum: null,
    dVflowfInput: null,
    dPfHold: null,
    dTfHold: null,
    dDpHold: null,
    dPulseNumHold: null,
    dVflowfinputHold: null,
    dPfParamsIndex: null,
    dTfParamsIndex: null,
    dDpParamsIndex: null,
    dPulseNumParamsIndex: null,
    dVflowfinputParamsIndex: null,
    dRyl1: null,
    dRyl2: null,
    mainId: null
  }
  proxy.resetForm("engineeringRef")
}

/** 搜索按钮操作 */
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
  getEngineering(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改流量输入工程数据"
  })
}

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

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

getList()
</script>
