<!--
 * @Descripttion: (工艺参数采集/product_trace_process_parameters)
 * @Author: (admin)
 * @Date: (2026-01-13)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['producttraceprocessparameters:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table
      :data="dataList"
      v-loading="loading"
      ref="table"
      border
      header-cell-class-name="el-table-header-cell"
      highlight-current-row
      @sort-change="sortChange"
      >
      <el-table-column prop="dataType" label="数据类型：FLOAT, INT, STRING, BOOL, AI（模拟量输入）等" align="center" v-if="columns.showColumn('dataType')">
        <template #default="scope">
          <dict-tag :options=" options.dataTypeOptions" :value="scope.row.dataType"  />
        </template>
      </el-table-column>
      <el-table-column prop="updatedTime" label="更新时间" :show-overflow-tooltip="true"  v-if="columns.showColumn('updatedTime')"/>
      <el-table-column prop="createdTime" label="创建时间" :show-overflow-tooltip="true"  v-if="columns.showColumn('createdTime')"/>
      <el-table-column prop="updatedby" label="更新人" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('updatedby')"/>
      <el-table-column prop="createdby" label="创建人" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('createdby')"/>
      <el-table-column prop="operator" label="操作人" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('operator')"/>
      <el-table-column prop="collectTime" label="采集时间" :show-overflow-tooltip="true"  v-if="columns.showColumn('collectTime')"/>
      <el-table-column prop="result" label="1 是合格 0是不合格" align="center" v-if="columns.showColumn('result')"/>
      <el-table-column prop="maxValue" label="最大允许值（用于报警/校验）" align="center" v-if="columns.showColumn('maxValue')"/>
      <el-table-column prop="minValue" label="最小允许值（用于报警/校验）" align="center" v-if="columns.showColumn('minValue')"/>
      <el-table-column prop="standardValue" label="标准/目标值（如目标温度 200.0 ℃）" align="center" v-if="columns.showColumn('standardValue')"/>
      <el-table-column prop="unit" label="单位，如：℃、MPa、秒、mm" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('unit')"/>
      <el-table-column prop="id" label="主键" align="center" v-if="columns.showColumn('id')"/>
      <el-table-column prop="description" label="参数描述，如：模具温度，用于热压工序" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('description')"/>
      <el-table-column prop="plcPoint" label="plc点位" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('plcPoint')"/>
      <el-table-column prop="parameterName" label="显示名称（用于UI展示，可和name一样）" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('parameterName')"/>
      <el-table-column prop="parameterCode" label="参数名称，如：温度、压力、时间" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('parameterCode')"/>
      <el-table-column prop="workstationCode" label="工站code" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('workstationCode')"/>
      <el-table-column prop="productlinebodyCode" label="产线code" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('productlinebodyCode')"/>
      <el-table-column prop="flowCode" label="流程code" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('flowCode')"/>
      <el-table-column prop="operationCode" label="工序号" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('operationCode')"/>
      <el-table-column prop="routingCode" label="工艺路线编码" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('routingCode')"/>
      <el-table-column prop="productSn" label="产品SN" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('productSn')"/>
      <el-table-column prop="workorder" label="工单号" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('workorder')"/>
      <el-table-column label="操作" width="160">
        <template #default="scope">
          <el-button type="success" size="small" icon="edit" title="编辑" v-hasPermi="['producttraceprocessparameters:edit']" @click="handleUpdate(scope.row)"></el-button>
          <el-button type="danger" size="small" icon="delete" title="删除" v-hasPermi="['producttraceprocessparameters:delete']" @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />


    <el-dialog :title="title" :lock-scroll="false" v-model="open" >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">

          <el-col :lg="12">
            <el-form-item label="数据类型：FLOAT, INT, STRING, BOOL, AI（模拟量输入）等" prop="dataType">
              <el-select v-model="form.dataType"  placeholder="请选择数据类型：FLOAT, INT, STRING, BOOL, AI（模拟量输入）等">
                <el-option
                  v-for="item in options.dataTypeOptions" 
                  :key="item.dictValue" 
                  :label="item.dictLabel" 
                  :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="更新时间" prop="updatedTime">
              <el-date-picker
                v-model="form.updatedTime"
                type="datetime"
                placeholder="选择日期时间"
                value-format="YYYY-MM-DD HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="创建时间" prop="createdTime">
              <el-date-picker
                v-model="form.createdTime"
                type="datetime"
                placeholder="选择日期时间"
                value-format="YYYY-MM-DD HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="更新人" prop="updatedby">
              <el-input v-model="form.updatedby" placeholder="请输入更新人" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="创建人" prop="createdby">
              <el-input v-model="form.createdby" placeholder="请输入创建人" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="操作人" prop="operator">
              <el-input v-model="form.operator" placeholder="请输入操作人" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="采集时间" prop="collectTime">
              <el-date-picker
                v-model="form.collectTime"
                type="datetime"
                placeholder="选择日期时间"
                value-format="YYYY-MM-DD HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col>
            
          <el-col :lg="12">
            <el-form-item label="1 是合格 0是不合格" prop="result">
              <el-input v-model.number="form.result" placeholder="请输入1 是合格 0是不合格" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="最大允许值（用于报警/校验）" prop="maxValue">
              <el-input v-model="form.maxValue" placeholder="请输入最大允许值（用于报警/校验）" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="最小允许值（用于报警/校验）" prop="minValue">
              <el-input v-model="form.minValue" placeholder="请输入最小允许值（用于报警/校验）" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="标准/目标值（如目标温度 200.0 ℃）" prop="standardValue">
              <el-input v-model="form.standardValue" placeholder="请输入标准/目标值（如目标温度 200.0 ℃）" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="单位，如：℃、MPa、秒、mm" prop="unit">
              <el-input v-model="form.unit" placeholder="请输入单位，如：℃、MPa、秒、mm" />
            </el-form-item>
          </el-col>
            

          <el-col :lg="12">
            <el-form-item label="参数描述，如：模具温度，用于热压工序" prop="description">
              <el-input v-model="form.description" placeholder="请输入参数描述，如：模具温度，用于热压工序" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="plc点位" prop="plcPoint">
              <el-input v-model="form.plcPoint" placeholder="请输入plc点位" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="显示名称（用于UI展示，可和name一样）" prop="parameterName">
              <el-input v-model="form.parameterName" placeholder="请输入显示名称（用于UI展示，可和name一样）" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="参数名称，如：温度、压力、时间" prop="parameterCode">
              <el-input v-model="form.parameterCode" placeholder="请输入参数名称，如：温度、压力、时间" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="工站code" prop="workstationCode">
              <el-input v-model="form.workstationCode" placeholder="请输入工站code" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="产线code" prop="productlinebodyCode">
              <el-input v-model="form.productlinebodyCode" placeholder="请输入产线code" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="流程code" prop="flowCode">
              <el-input v-model="form.flowCode" placeholder="请输入流程code" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="工序号" prop="operationCode">
              <el-input v-model="form.operationCode" placeholder="请输入工序号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="工艺路线编码" prop="routingCode">
              <el-input v-model="form.routingCode" placeholder="请输入工艺路线编码" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="产品SN" prop="productSn">
              <el-input v-model="form.productSn" placeholder="请输入产品SN" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="工单号" prop="workorder">
              <el-input v-model="form.workorder" placeholder="请输入工单号" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" :loading="state.submitLoading" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="producttraceprocessparameters">
import { listProductTraceProcessParameters,
 addProductTraceProcessParameters, delProductTraceProcessParameters, 
 updateProductTraceProcessParameters,getProductTraceProcessParameters, 
 } 
from '@/api/mes/producttraceprocessparameters.js'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: '',
  sortType: 'asc',
})
const columns = ref([
  { visible: true, align: 'center', type: 'dict', prop: 'dataType', label: '数据类型：FLOAT, INT, STRING, BOOL, AI（模拟量输入）等'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'updatedTime', label: '更新时间'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'createdTime', label: '创建时间'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'updatedby', label: '更新人'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'createdby', label: '创建人'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'operator', label: '操作人'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'collectTime', label: '采集时间'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'result', label: '1 是合格 0是不合格'   },
  { visible: false, align: 'center', type: '', prop: 'maxValue', label: '最大允许值（用于报警/校验）'   },
  { visible: false, align: 'center', type: '', prop: 'minValue', label: '最小允许值（用于报警/校验）'   },
  { visible: false, align: 'center', type: '', prop: 'standardValue', label: '标准/目标值（如目标温度 200.0 ℃）'   },
  { visible: false, align: 'center', type: '', prop: 'unit', label: '单位，如：℃、MPa、秒、mm'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'id', label: '主键'   },
  { visible: false, align: 'center', type: '', prop: 'description', label: '参数描述，如：模具温度，用于热压工序'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'plcPoint', label: 'plc点位'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'parameterName', label: '显示名称（用于UI展示，可和name一样）'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'parameterCode', label: '参数名称，如：温度、压力、时间'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'workstationCode', label: '工站code'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'productlinebodyCode', label: '产线code'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'flowCode', label: '流程code'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'operationCode', label: '工序号'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'routingCode', label: '工艺路线编码'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'productSn', label: '产品SN'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'workorder', label: '工单号'  ,showOverflowTooltip: true  },
  //{ visible: false, prop: 'actions', label: '操作', type: 'slot', width: '160' }
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [
]


function getList(){
  loading.value = true
  listProductTraceProcessParameters(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery(){
  proxy.resetForm("queryRef")
  handleQuery()
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  submitLoading: false,
  form: {},
  rules: {
    dataType: [{ required: true, message: "数据类型：FLOAT, INT, STRING, BOOL, AI（模拟量输入）等不能为空", trigger: "change"     }],
    parameterCode: [{ required: true, message: "参数名称，如：温度、压力、时间不能为空", trigger: "blur"     }],
  },
  options: {
    // 数据类型：FLOAT, INT, STRING, BOOL, AI（模拟量输入）等 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
dataTypeOptions: [],
  }
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel(){
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    dataType: null,
    updatedTime: null,
    createdTime: null,
    updatedby: null,
    createdby: null,
    operator: null,
    collectTime: null,
    result: null,
    maxValue: null,
    minValue: null,
    standardValue: null,
    unit: null,
    id: null,
    description: null,
    plcPoint: null,
    parameterName: null,
    parameterCode: null,
    workstationCode: null,
    productlinebodyCode: null,
    flowCode: null,
    operationCode: null,
    routingCode: null,
    productSn: null,
    workorder: null,
  };
  proxy.resetForm("formRef")
}


// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  state.submitLoading = false
  title.value = '添加工艺参数采集'
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.id || ids.value
  getProductTraceProcessParameters(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '修改工艺参数采集'
      opertype.value = 2

      form.value = {
        ...data,
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      state.submitLoading = true

      if (form.value.id != undefined && opertype.value === 2) {
        updateProductTraceProcessParameters(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
        .finally(() => {
          state.submitLoading = false
        })
      } else {
        addProductTraceProcessParameters(form.value).then((res) => {
            proxy.$modal.msgSuccess("新增成功")
            open.value = false
            getList()
          })
          .finally(() => {
            setTimeout(() => {
              state.submitLoading = false
            }, 800)
          })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.id || ids.value

  proxy
    .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？', "警告", {
      confirmButtonText: proxy.$t('common.ok'),
      cancelButtonText: proxy.$t('common.cancel'),
      type: "warning",
    })
    .then(function () {
      return delProductTraceProcessParameters(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    })
}




handleQuery()
</script>