Newer
Older
xc-business-system / src / views / business / measure / item / components / third / templateDetail.vue
dutingting on 29 Nov 49 KB 解决冲突
<!-- 第三套:多功能电气安全校准器标准库 -->
<script lang="ts" setup name="TemplateDetailThird">
import { ElMessage, ElMessageBox } from 'element-plus'
import { clickAdd, clickSub } from '../../useCalculateResolution'
import type { IList } from './templateDetail-interface'
import { suggestions } from './suggestions'
import { getUid } from '@/utils/getUid'
import type { dictType } from '@/global'
import { getDictByCode } from '@/api/system/dict'
import { calc } from '@/utils/useCalc'
import { useCheckList } from '@/commonMethods/useCheckList'
import { calculate, recalculate } from '@/api/business/measure/caculate'
import { useUniqueArray } from '@/utils/useUniqueArray'
import { initTableRow, mergeTableRow, useMergeTableRow } from '@/commonMethods/useMergeTableRow'
import { caculateDecimalDigits } from '@/views/business/measure/item/useCalculateResolution'

const props = defineProps({
  pageType: {
    type: String,
    default: 'add',
  },
  itemCategoryName: {
    type: String,
    require: true,
  }, // 智能模型检定项分类名称
  belongStandardEquipment: { // 检校标准装置code
    type: String,
    require: true,
  },
  list: {
    type: Array as any,
  },
  form: { // 检定项表单
    type: Object as any,
    require: true,
  },
  itemId: { // 检定项id
    type: String,
    default: '',
  },
})
const listTop = ref<IList[]>([]) // 表格数据(上面表格)
const listBottom = ref<IList[]>([]) // 表格数据(下面表格)
const listTopCheckout = ref<IList[]>([]) // 表格数据(上面表格)多选
const listBottomCheckout = ref<IList[]>([]) // 表格数据(下面表格)多选
const showListTop = ref(true) // 上面表格是否显示
const showListBottom = ref(true) // 下面表格是否显示
const list = ref<IList[]>([]) // 表格数据
const tableLoading = ref(false)
const tableLoadingBottom = ref(false)
// ------------------------------------------字典----------------------------------------------
const standardEndButtonVoltageList = ref<dictType[]>([]) // 端钮电压
const standardRangeRangeList = ref<dictType[]>([]) // 量程
const standardResistanceList = ref<dictType[]>([]) // 电阻单位
const standardMaximumErrorList = ref<dictType[]>([]) // 最大允许误差公式
const standard3RangeNumberList = ref<dictType[]>([]) // 数字式接地电阻
const standard3RangeSimulationList = ref<dictType[]>([]) // 模拟式接地电阻
/**
 * 获取字典
 */
function getDict() {
  // 端钮电压
  getDictByCode('standardEndButtonVoltage').then((response) => {
    standardEndButtonVoltageList.value = response.data
  })
  // 量程
  getDictByCode('standardRangeRange').then((response) => {
    standardRangeRangeList.value = response.data
  })
  // 电阻单位
  getDictByCode('standardResistance').then((response) => {
    standardResistanceList.value = response.data
  })
  // 最大允许误差公式
  getDictByCode('standardMaximumError').then((response) => {
    standardMaximumErrorList.value = response.data
  })
  // 数字式接地电阻
  getDictByCode('standard3RangeNumber').then((response) => {
    standard3RangeNumberList.value = response.data
  })
  // 模拟式接地电阻
  getDictByCode('standard3RangeSimulation').then((response) => {
    standard3RangeSimulationList.value = response.data
  })
}

getDict()
// ----------------------------------------表头------------------------------------------------
const columns_top = ref([]) as any // 表头数据--上面表格
const columns_bottom = ref([]) as any // 表头数据--下面表格
const columns_top_voltage_insulation_megger = ref([ // 开路电压(电子式绝缘电阻表(数字式)、电子式绝缘电阻表(指针式)、兆欧式)上面表格
  { text: '检定项目', value: 'params', align: 'center', required: true, width: '100' },
  { text: '标称值(V)', value: 'nominalValue', align: 'center', required: true },
  { text: '最小允许值相关系数a', value: 'minimumAllowFactorA', align: 'center', required: true },
  { text: '最大允许值相关系数b', value: 'maximumAllowFactorB', align: 'center', required: true },
  { text: '最小允许值(V)', value: 'minimumAllowValue', align: 'center', required: false },
  { text: '最大允许值(V)', value: 'maximumAllowValue', align: 'center', required: false },
])

const columns_top_earth_resistanc_meter_number = ref([ // 示值误差(数字式接地电阻表)上面表格
  { text: '检定项目', value: 'params', align: 'center', required: true, width: '100' },
  { text: '量程', value: 'rangeRange', align: 'center', required: true },
  { text: '标准值', value: 'standardValue', align: 'center', required: true, width: '180' },
  { text: '单位', value: 'unit', align: 'center', required: true },
  { text: '分辨力', value: 'resolution', align: 'center', required: true, width: '220', reg: (resolution: string | number) => { return Number(resolution) <= 1 } },
  { text: '误差参数a', value: 'errorParamA', align: 'center', required: true, width: '180' },
  { text: '误差参数b', value: 'errorParamB', align: 'center', required: true, width: '180' },
  { text: '最大允许误差(Ω)', value: 'maximumError', align: 'center', required: true, width: '260' },
])
const columns_top_earth_resistanc_meter_simulation = ref([ // 示值误差(模拟式接地电阻表)上面表格
  { text: '检定项目', value: 'params', align: 'center', required: true, width: '100' },
  { text: '量程', value: 'rangeRange', align: 'center', required: true },
  { text: '被检表示值', value: 'measureIndicationValue', align: 'center', required: true },
  { text: '单位', value: 'unit', align: 'center', required: true },
  { text: '分辨力', value: 'resolution', align: 'center', required: true, width: '220', reg: (resolution: string | number) => { return Number(resolution) <= 1 } },
  { text: '准确度a', value: 'accuracyA', align: 'center', required: true },
  { text: '最大允许误差', value: 'maximumError', align: 'center', required: false },
])

const columns_bottom_Electronic_insulation_resistance_meter_number = ref([ // 电子式绝缘电阻表(数字式)--下面表格示值误差
  { text: '检定项目', value: 'params', align: 'center', required: true, width: '100' },
  { text: '端钮电压(V)', value: 'terminalVoltage', align: 'center', required: true, width: '180' },
  { text: '量程', value: 'rangeRange', align: 'center', required: true, width: '180' },
  { text: '标准值', value: 'standardValue', align: 'center', required: true, width: '180' },
  { text: '单位', value: 'unit', align: 'center', required: true },
  { text: '分辨力', value: 'resolution', align: 'center', required: true, width: '220', reg: (resolution: string | number) => { return Number(resolution) <= 1 } },
  { text: '误差参数a', value: 'errorParamA', align: 'center', required: true, width: '180' },
  { text: '误差参数b', value: 'errorParamB', align: 'center', required: true, width: '180' },
  { text: '最大允许误差', value: 'maximumError', align: 'center', required: true, width: '260' },
])

const columns_bottom_Electronic_insulation_resistance_meter_pointer = ref([ // 电子式绝缘电阻表(指针式)--下面表格示值误差
  { text: '检定项目', value: 'params', align: 'center', required: true },
  { text: '端钮电压(V)', value: 'terminalVoltage', align: 'center', required: true, width: '180' },
  { text: '被检表示值', value: 'measureIndicationValue', align: 'center', required: true, width: '180' },
  { text: '单位', value: 'unit', align: 'center', required: true, width: '100' },
  { text: '分辨力', value: 'resolution', align: 'center', required: true, width: '220', reg: (resolution: string | number) => { return Number(resolution) <= 1 } },
  { text: '准确度a', value: 'accuracyA', align: 'center', required: true, width: '180' },
  { text: '最大允许误差', value: 'maximumError', align: 'center', required: false },
])

const columns_bottom_megger = ref([ // 兆欧表--下面表格示值误差
  { text: '检定项目', value: 'params', align: 'center', required: true },
  { text: '被检表示值', value: 'measureIndicationValue', align: 'center', required: true, width: '180' },
  { text: '单位', value: 'unit', align: 'center', required: true },
  { text: '分辨力', value: 'resolution', align: 'center', required: true, width: '220', reg: (resolution: string | number) => { return Number(resolution) <= 1 } },
  { text: '准确度a', value: 'accuracyA', align: 'center', required: true, width: '180' },
  { text: '最大允许误差', value: 'maximumError', align: 'center', required: false },
])

const columns_bottom_earth_resistanc_meter = ref([ // 数字式接地电阻--下面表格(辅助接地电阻试验)
  { text: '检定项目', value: 'params', align: 'center', required: true, width: '150' },
  { text: '标准值', value: 'standardValue', align: 'center', required: true },
  { text: '辅助地阻', value: 'auxiliaryGroundResistance', align: 'center', required: true },
  { text: '单位', value: 'unit', align: 'center', required: true, width: '90' },
  { text: '分辨力', value: 'resolution', align: 'center', required: true, width: '220', reg: (resolution: string | number) => { return Number(resolution) <= 1 } },
])

const columns_bottom_earth_resistanc_simulation = ref([ // 模拟式接地电阻--下面表格(辅助接地电阻试验)
  { text: '检定项目', value: 'params', align: 'center', required: true, width: '150' },
  { text: '被检表示值', value: 'measureIndicationValue', align: 'center', required: true },
  { text: '辅助地阻', value: 'auxiliaryGroundResistance', align: 'center', required: true },
  { text: '单位', value: 'unit', align: 'center', required: true, width: '90' },
  { text: '分辨力', value: 'resolution', align: 'center', required: true, width: '220', reg: (resolution: string | number) => { return Number(resolution) <= 1 } },
])

// ---------------------------------------------表格操作------------------------------------------------
// 表格多选
const handleSelectionChange = (e: any, type: 'top' | 'bottom') => {
  if (type === 'top') { // 上面表格多选
    listTopCheckout.value = e
  }
  else { // 下面表格多选
    listBottomCheckout.value = e
  }
}
// 电子式绝缘电阻表(指针式)、兆欧表校验被检表示值
const checkMeasureIndicationValue = () => {
  for (let i = 0; i < listBottom.value.length; i++) {
    if (isNaN(Number(listBottom.value[i].measureIndicationValue)) && listBottom.value[i].measureIndicationValue !== '∞') {
      ElMessage.warning(`第${i + 1}行 被检表示值只允许输入数字或选择列表其中一项`)
      return false
    }
  }
  return true
}
/**
 * 增加行
 * @param dataType 1上面表格  2下面表格
 */
const addRow = (dataType: string) => {
  if (dataType === '1') { // 开路电压\示值误差
    if (props.itemCategoryName === '电子式绝缘电阻表(数字式)' || props.itemCategoryName === '电子式绝缘电阻表(指针式)' || props.itemCategoryName === '兆欧表(指针式)') {
      if (!useCheckList(listTop.value, columns_top_voltage_insulation_megger.value, '开路电压')) { return false }
      listTop.value.push({
        id: `custom-${new Date().getTime()}`,
        params: '开路电压', // 检定项目
        nominalValue: listTop.value.length ? listTop.value[listTop.value.length - 1].nominalValue : '', //		标称值
        dataType, //		检定项数据类型(一个检定项中区分两个表格)(字典code),同对应的检定项类型
        minimumAllowFactorA: listTop.value.length ? listTop.value[listTop.value.length - 1].minimumAllowFactorA : '', //		最小允许值相关系数a
        maximumAllowFactorB: listTop.value.length ? listTop.value[listTop.value.length - 1].maximumAllowFactorB : '', //		最大允许值相关系数b
        minimumAllowValue: listTop.value.length ? listTop.value[listTop.value.length - 1].minimumAllowValue : '', //		最小允许值
        maximumAllowValue: listTop.value.length ? listTop.value[listTop.value.length - 1].maximumAllowValue : '', //		最大允许值
        itemId: props.itemId, // 检定项id
      })
    }
    else if (props.itemCategoryName === '数字式接地电阻表') {
      if (!useCheckList(listTop.value, columns_top_earth_resistanc_meter_number.value, '示值误差')) { return false }
      for (let i = 1; i <= 10; i++) {
        listTop.value.push({
          id: `custom-${new Date().getTime()}`,
          params: '示值误差', // 检定项目
          dataType, //		检定项数据类型(一个检定项中区分两个表格)(字典code),同对应的检定项类型
          itemId: props.itemId, // 检定项id
          rangeRange: '', // 量程
          standardValue: '', //	标准值
          unit: 'Ω', // 单位
          resolution: listTop.value.length ? listTop.value[listTop.value.length - 1].resolution : '0.1', // 分辨力
          errorParamA: listTop.value.length ? listTop.value[listTop.value.length - 1].errorParamA : '0', // 误差参数a
          errorParamB: listTop.value.length ? listTop.value[listTop.value.length - 1].errorParamB : '0', // 误差参数b
          maximumError: listTop.value.length ? listTop.value[listTop.value.length - 1].maximumError : '±(a * 被检示值 + b * 量程)', // 最大允许误差
        })
      }
    }
    else if (props.itemCategoryName === '模拟式接地电阻表') {
      if (!useCheckList(listTop.value, columns_top_earth_resistanc_meter_simulation.value, '示值误差')) { return false }
      for (let i = 1; i <= 11; i++) {
        listTop.value.push({
          id: `custom-${new Date().getTime()}`,
          params: '示值误差', // 检定项目
          dataType, //		检定项数据类型(一个检定项中区分两个表格)(字典code),同对应的检定项类型
          itemId: props.itemId, // 检定项id
          rangeRange: '', // 量程
          measureIndicationValue: '', //	被检表示值
          unit: 'Ω', // 单位
          resolution: listTop.value.length ? listTop.value[listTop.value.length - 1].resolution : '0.1', // 分辨力
          accuracyA: listTop.value.length ? listTop.value[listTop.value.length - 1].accuracyA : '0', // 准确度a
          maximumError: '', // 最大允许误差
        })
      }
    }
  }
  else { // 示值误差、辅助接地电阻实验
    if (props.itemCategoryName === '电子式绝缘电阻表(数字式)') {
      if (!useCheckList(listBottom.value, columns_bottom_Electronic_insulation_resistance_meter_number.value, '示值误差')) { return false }
      listBottom.value.push({
        id: `custom-${new Date().getTime()}`,
        params: '示值误差', // 检定项目
        dataType, //		检定项数据类型(一个检定项中区分两个表格)(字典code),同对应的检定项类型
        itemId: props.itemId, // 检定项id
        terminalVoltage: listBottom.value.length ? listBottom.value[listBottom.value.length - 1].terminalVoltage : '', // 端钮电压
        rangeRange: listBottom.value.length ? listBottom.value[listBottom.value.length - 1].rangeRange : '', // 量程
        standardValue: listBottom.value.length ? listBottom.value[listBottom.value.length - 1].standardValue : '', //		标称值
        unit: listBottom.value.length ? listBottom.value[listBottom.value.length - 1].unit : '', // 单位
        maximumError: listBottom.value.length ? listBottom.value[listBottom.value.length - 1].maximumError : '', // 最大允许误差
        resolution: listBottom.value.length ? listBottom.value[listBottom.value.length - 1].resolution : '0.1', // 分辨力
        errorParamA: listBottom.value.length ? listBottom.value[listBottom.value.length - 1].errorParamA : '', //	误差参数a
        errorParamB: listBottom.value.length ? listBottom.value[listBottom.value.length - 1].errorParamB : '', //	误差参数b
      })
    }
    else if (props.itemCategoryName === '电子式绝缘电阻表(指针式)') {
      if (!useCheckList(listBottom.value, columns_bottom_Electronic_insulation_resistance_meter_pointer.value, '示值误差')) { return false }
      if (!checkMeasureIndicationValue()) { return false }
      listBottom.value.push({
        id: `custom-${new Date().getTime()}`,
        params: '示值误差', // 检定项目
        dataType, //		检定项数据类型(一个检定项中区分两个表格)(字典code),同对应的检定项类型
        itemId: props.itemId, // 检定项id
        terminalVoltage: listBottom.value.length ? listBottom.value[listBottom.value.length - 1].terminalVoltage : '', // 端钮电压
        measureIndicationValue: listBottom.value.length ? listBottom.value[listBottom.value.length - 1].measureIndicationValue : '', //	被检表示值
        unit: listBottom.value.length ? listBottom.value[listBottom.value.length - 1].unit : '', // 单位
        maximumError: '', // 最大允许误差
        resolution: listBottom.value.length ? listBottom.value[listBottom.value.length - 1].resolution : '0.1', // 分辨力
        accuracyA: listBottom.value.length ? listBottom.value[listBottom.value.length - 1].accuracyA : '', //	准确度a
      })
    }
    else if (props.itemCategoryName === '兆欧表(指针式)') {
      if (!useCheckList(listBottom.value, columns_bottom_megger.value, '示值误差')) { return false }
      if (!checkMeasureIndicationValue()) { return false }
      listBottom.value.push({
        id: `custom-${new Date().getTime()}`,
        params: '示值误差', // 检定项目
        dataType, //		检定项数据类型(一个检定项中区分两个表格)(字典code),同对应的检定项类型
        itemId: props.itemId, // 检定项id
        measureIndicationValue: listBottom.value.length ? listBottom.value[listBottom.value.length - 1].measureIndicationValue : '', //	被检表示值
        unit: listBottom.value.length ? listBottom.value[listBottom.value.length - 1].unit : '', // 单位
        resolution: listBottom.value.length ? listBottom.value[listBottom.value.length - 1].resolution : '0.1', // 分辨力
        accuracyA: listBottom.value.length ? listBottom.value[listBottom.value.length - 1].accuracyA : '', //	准确度a
        maximumError: '', // 最大允许误差
      })
    }
  }
}

// 删除行
const deleteRow = (type: 'top' | 'bottom') => {
  if (type === 'top' && !listTopCheckout.value.length) {
    ElMessage.warning('请选中要删除的行')
  }
  if (type === 'bottom' && !listBottomCheckout.value.length) {
    ElMessage.warning('请选中要删除的行')
  }
  else {
    if (type === 'top') {
      if (props.itemCategoryName === '数字式接地电阻表' || props.itemCategoryName === '模拟式接地电阻表') {
        ElMessageBox.confirm(
          '将会删除所有与选中的量程相同的数值,确认操作吗?',
          '提示',
          {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
          },
        )
          .then(() => {
            listTopCheckout.value.forEach((item) => {
              listTop.value = listTop.value.filter((i: any) => {
                return i.rangeRange !== item.rangeRange
              })
            })
          })
      }
      else {
        listTop.value = listTop.value.filter((item: any) => {
          return !listTopCheckout.value.includes(item)
        })
      }
    }
    else {
      listBottom.value = listBottom.value.filter((item: any) => {
        return !listBottomCheckout.value.includes(item)
      })
    }
  }
}
const selectedRange = ref([]) as any // 已经选的量程
/**
 * 数字式接地电阻量程选择变化事件
 * @param value 选中的值
 * @param index 当前索引
 * @param several 计算到第几位
 */
const changeSelectRange = (value: string, index: number, several = 9) => {
  // 选择好量程之后将其下面9个量程赋值相同的数据
  for (let i = index + 1; i < index + several; i++) {
    listTop.value = listTop.value.map((item, indexNum: number) => {
      return {
        ...item,
        rangeRange: indexNum >= index ? listTop.value[index].rangeRange : item.rangeRange, // 量程
      }
    })
  }
  if (props.itemCategoryName === '数字式接地电阻表') { // 计算标准值
    listTop.value = listTop.value.map((item, index) => {
      // 计算量程的90%
      const range90 = calc(Number(item.rangeRange), 0.9, '*')
      // 10等分
      const average = calc(Number(range90), 9, '/')
      console.log(range90, average, index % 10)
      return {
        ...item,
        standardValue: calc(Number(average), index % 10, '*'),
      }
    })
  }
  else if (props.itemCategoryName === '模拟式接地电阻表') { // 计算被检表示值
    listTop.value = listTop.value.map((item, index) => {
      // 将量程取出来
      const tempRange = Number(item.rangeRange?.slice(1))
      return {
        ...item,
        measureIndicationValue: calc(tempRange, index % 11, '*'),
      }
    })
  }
}

// ---------------------------------------------校验---------------------------------------------------
// 校验表格(点击保存的时候用、生成标准器示值)
function checkList() {
  // return useCheckList(list.value, columns.value, '检定项表格')
}
// ------------------------------------------------------------------------------------------------
// 点击计算结果--上方表格计算
const calculateDataTop = () => {
  if (props.itemCategoryName === '模拟式接地电阻表') { // 最大允许误差
    if (!useCheckList(listTop.value, columns_top_earth_resistanc_meter_simulation.value, '示值误差')) { return false }
  }
  else if (props.itemCategoryName === '电子式绝缘电阻表(数字式)' || props.itemCategoryName === '电子式绝缘电阻表(指针式)' || props.itemCategoryName === '兆欧表(指针式)') {
    if (!useCheckList(listTop.value, columns_top.value, '开路电压')) { return false }
  }
  tableLoading.value = true
  const params = {
    belongStandardEquipment: props.belongStandardEquipment, // 检校标准库
    itemCategoryName: props.itemCategoryName, // 检定项分类名称
    measureItemDataElectricalSafetyList: listTop.value,
  }
  recalculate(params).then((res) => {
    listTop.value = res.data.map((item: { params: string }) => {
      return {
        ...item,
        params: (props.itemCategoryName === '模拟式接地电阻表' || props.itemCategoryName === '数字式接地电阻表') ? '示值误差' : '开路电压',
      }
    })
    tableLoading.value = false
  })
}
// 点击计算结果--下面表格计算
const calculateDataBottom = () => {
  if (props.itemCategoryName === '兆欧表(指针式)') {
    if (!useCheckList(listBottom.value, columns_bottom_megger.value, '示值误差')) { return false }
    // 校验被检表示值
    if (!checkMeasureIndicationValue()) { return false }
  }
  else if (props.itemCategoryName === '电子式绝缘电阻表(指针式)') {
    if (!useCheckList(listBottom.value, columns_bottom_Electronic_insulation_resistance_meter_pointer.value, '开路电压')) { return false }
    // 校验被检表示值
    if (!checkMeasureIndicationValue()) { return false }
  }
  tableLoadingBottom.value = true
  const params = {
    belongStandardEquipment: props.belongStandardEquipment, // 检校标准库
    itemCategoryName: props.itemCategoryName, // 检定项分类名称
    measureItemDataElectricalSafetyList: listBottom.value,
  }
  recalculate(params).then((res) => {
    listBottom.value = res.data.map((item: { params: string }) => {
      return {
        ...item,
        params: '示值误差',
      }
    })
    tableLoadingBottom.value = false
  })
}
const auxiliaryGroundResistanceMap: { [key: number]: number } = {
  1: 500,
  2: 0,
  3: 1000,
  4: 2000,
  5: 5000,
}
// 点击生成辅助接地电阻--下面表格计算
const createDataBottom = () => {
  if (props.itemCategoryName === '数字式接地电阻表') { // 计算标准值
    if (!listTop.value.length) { ElMessage.warning('示值误差不能为空'); return false }
    listBottom.value = []
    for (let i = 1; i <= 5; i++) {
      listBottom.value.push({
        id: '',
        dataType: '2',
        params: '辅助接地电阻试验',
        // standardValue: calc(Number(listTop.value[9].standardValue), Number(listTop.value[0].standardValue), '-'),
        standardValue: listTop.value[9].standardValue,
        auxiliaryGroundResistance: auxiliaryGroundResistanceMap[i],
        unit: 'Ω',
        resolution: '0.01',
        itemId: props.itemId, // 检定项id
      })
    }
  }
  else if (props.itemCategoryName === '模拟式接地电阻表') {
    if (!listTop.value.length) { ElMessage.warning('示值误差不能为空'); return false }
    listBottom.value = []
    for (let i = 1; i <= 5; i++) {
      listBottom.value.push({
        id: '',
        dataType: '2',
        params: '辅助接地电阻试验',
        // measureIndicationValue: calc(Number(listTop.value[10].measureIndicationValue), Number(listTop.value[0].measureIndicationValue), '-'),
        measureIndicationValue: listTop.value[10].measureIndicationValue,
        auxiliaryGroundResistance: auxiliaryGroundResistanceMap[i],
        unit: 'Ω',
        resolution: '0.01',
        itemId: props.itemId, // 检定项id
      })
    }
  }
}

// -----------------------------------------------------------------------------------------------------
watch(() => listTop.value, (newValue) => {
  if (props.itemCategoryName === '数字式接地电阻表' || props.itemCategoryName === '模拟式接地电阻表') {
  // 根据量程数组去重
    selectedRange.value = useUniqueArray(listTop.value.map(item => item.rangeRange))
  }
})

watch(() => props.itemCategoryName, (newValue) => {
  if (newValue) {
    switch (newValue) {
      case '电子式绝缘电阻表(数字式)':
        columns_top.value = columns_top_voltage_insulation_megger.value
        columns_bottom.value = columns_bottom_Electronic_insulation_resistance_meter_number.value
        break
      case '电子式绝缘电阻表(指针式)':
        columns_top.value = columns_top_voltage_insulation_megger.value
        columns_bottom.value = columns_bottom_Electronic_insulation_resistance_meter_pointer.value
        break
      case '兆欧表(指针式)':
        columns_top.value = columns_top_voltage_insulation_megger.value
        columns_bottom.value = columns_bottom_megger.value
        break
      case '数字式接地电阻表':
        columns_top.value = columns_top_earth_resistanc_meter_number.value
        columns_bottom.value = columns_bottom_earth_resistanc_meter.value
        break
      case '模拟式接地电阻表':
        columns_top.value = columns_top_earth_resistanc_meter_simulation.value
        columns_bottom.value = columns_bottom_earth_resistanc_simulation.value
        break
    }
  }
}, { immediate: true })
const $route = useRoute()
watch(() => props.list, (newVal) => { // 检定项表格
  const updataOld = $route.query.updataOld as string // 是否更新了检定项或者标准库
  const isFirstConfig = $route.query.isFirstConfig as string // 是否第一次配置

  console.log('是否更新了检定项或者标准库', updataOld)
  console.log('是否第一次配置', isFirstConfig)

  if (newVal && Object.keys(newVal).length && updataOld !== 'true' && isFirstConfig !== 'true') {
    const topList = newVal.filter((item: any) => `${item.dataType}` === '1')
    listTop.value = topList.map((item: any) => {
      return {
        ...item,
        params: (props.itemCategoryName === '数字式接地电阻表' || props.itemCategoryName === '模拟式接地电阻表') ? '示值误差' : '开路电压',
        resolution: Number(item.resolution), // 分辨力
      }
    })

    const bottomList = newVal.filter((item: any) => `${item.dataType}` === '2')
    listBottom.value = bottomList.map((item: any) => {
      return {
        ...item,
        params: (props.itemCategoryName === '数字式接地电阻表' || props.itemCategoryName === '模拟式接地电阻表') ? '辅助接地电阻试验' : '示值误差',
        resolution: Number(item.resolution), // 分辨力
      }
    })
    showListTop.value = !!listTop.value.length
    showListBottom.value = !!listBottom.value.length
    // if(props.pageType === 'detail' && (props.itemCategoryName === '电子式绝缘电阻表(数字式)' || props.itemCategoryName === '电子式绝缘电阻表(指针式)')) {
    //   useMergeTableRow(listBottom.value, ['terminalVoltage']) // 合并单元格--端钮电压
    // } else {
    //   initTableRow(listBottom.value)
    // }
  }
})
defineExpose({ listBottom, listTop, showListTop, showListBottom })
// ---------------------------------------------------表格操作-----------------------------------------------------------------
const randomNum1 = `${new Date().getTime}${Math.random()}`
const randomNum2 = `${new Date().getTime}${Math.random()}`
const tableRef1 = ref()
const tableRef2 = ref()
// 鼠标移出
const mouseoutTable = (type: string) => {
  console.log('鼠标移出')
  if (props.pageType === 'detail') { return }
  if (document.getElementById(type === 'top' ? randomNum1 : randomNum2)) {
    (document.getElementById(type === 'top' ? randomNum1 : randomNum2) as HTMLElement).style.display = 'none'
  }
}
// 点击其他位置隐藏自定义菜单
document.addEventListener('click', () => {
  if (props.pageType === 'detail') { return }
  if (document.getElementById(randomNum1)) {
    (document.getElementById(randomNum1) as HTMLElement).style.display = 'none'
  }
  if (document.getElementById(randomNum2)) {
    (document.getElementById(randomNum2) as HTMLElement).style.display = 'none'
  }
})
// 右击当前行操作
const clickIndex = ref(-1)
const contextmenu1 = (row: any, column: any, event: any, index: number) => {
  if (props.pageType === 'detail') { return }
  // 阻止默认的右键菜单
  event.preventDefault()
  clickIndex.value = listTop.value.findIndex(item => item === row)
  // console.log('右击', clickIndex.value)
  // 获取自定义菜单元素
  var menu = document.getElementById(randomNum1) as HTMLElement
  // 设置自定义菜单的位置并显示
  let positionX = event.clientX
  let positionY = event.clientY
  if (window.innerHeight - event.clientY < 268) {
    positionY = window.innerHeight - 268
  }
  else {
    positionY = event.clientY
  }
  if (window.innerWidth - event.clientX < 146) {
    positionX = window.innerWidth - 146
  }
  else if (event.clientX - 180 < 146) {
    positionX = 180
  }
  else {
    positionX = event.clientX - 146 / 2
  }
  menu.style.top = `${positionY}px`
  menu.style.left = `${positionX}px`
  menu.style.display = 'block'
}
const contextmenu2 = (row: any, column: any, event: any, index: number) => {
  if (props.pageType === 'detail') { return }
  // 阻止默认的右键菜单
  event.preventDefault()
  clickIndex.value = listBottom.value.findIndex(item => item === row)
  // console.log('右击', clickIndex.value)
  // 获取自定义菜单元素
  var menu = document.getElementById(randomNum2) as HTMLElement
  // 设置自定义菜单的位置并显示
  let positionX = event.clientX
  let positionY = event.clientY
  if (window.innerHeight - event.clientY < 268) {
    positionY = window.innerHeight - 268
  }
  else {
    positionY = event.clientY
  }
  if (window.innerWidth - event.clientX < 146) {
    positionX = window.innerWidth - 146
  }
  else if (event.clientX - 180 < 146) {
    positionX = 180
  }
  else {
    positionX = event.clientX - 146 / 2
  }
  menu.style.top = `${positionY}px`
  menu.style.left = `${positionX}px`
  menu.style.display = 'block'
}
// 添加行等操作
const costomAddRow = (type: string, category: string) => {
  const data = category === 'top' ? listTop.value : listBottom.value
  const selectData = category === 'top' ? listTopCheckout.value : listBottomCheckout.value
  const table = category === 'top' ? tableRef1.value : tableRef2.value
  if (type === 'current-pre') {
    // 当前行前方插入
    data.splice(clickIndex.value, 0, JSON.parse(JSON.stringify({ ...data[clickIndex.value], id: `custom-${new Date().getTime()}` })))
  }
  else if (type === 'current-next') {
    // 当前行后方方插入
    data.splice(clickIndex.value + 1, 0, JSON.parse(JSON.stringify({ ...data[clickIndex.value], id: `custom-${new Date().getTime()}` })))
  }
  else if (type === 'list-head') {
    // 列表头行插入
    data.splice(0, 0, JSON.parse(JSON.stringify({ ...data[clickIndex.value], id: `custom-${new Date().getTime()}` })))
  }
  else if (type === 'list-tail') {
    // 列表尾行插入
    data.splice(data.length, 0, JSON.parse(JSON.stringify({ ...data[clickIndex.value], id: `custom-${new Date().getTime()}` })))
  }
  else if (type === 'select-pre') {
    // 选中行前方插入
    if (!selectData.length) {
      ElMessage.warning('未选择数据')
      return
    }
    selectData.forEach((item, index) => {
      const dataIndex = data.findIndex(citem => item === citem)
      data.splice(dataIndex, 0, JSON.parse(JSON.stringify({ ...item, id: `custom-${new Date().getTime()}` })))
    })
    table!.clearSelection()
  }
  else if (type === 'select-next') {
    // 选中行后方插入
    if (!selectData.length) {
      ElMessage.warning('未选择数据')
      return
    }
    selectData.forEach((item, index) => {
      const dataIndex = data.findIndex(citem => item === citem)
      data.splice(dataIndex + 1, 0, JSON.parse(JSON.stringify({ ...item, id: `custom-${new Date().getTime()}` })))
    })
    table!.clearSelection()
  }
  else if (type === 'del-current') {
    data.splice(clickIndex.value, 1)
  }
  else if (type === 'del-select') {
    if (!selectData.length) {
      ElMessage.warning('未选择数据')
      return
    }
    selectData.forEach((item, index) => {
      const dataIndex = data.findIndex(citem => item === citem)
      data.splice(dataIndex, 1)
    })
    table!.clearSelection()
  }
  clickIndex.value = -1
}
</script>

<template>
  <div style="padding: 0 10px;">
    <el-checkbox v-model="props.form.appearanceFunctionCheck" :true-label="1" :false-label="0" :checked="true" :disabled="pageType === 'detail'">
      外观及功能性检查
    </el-checkbox>
  </div>
  <div style="padding: 0 10px;">
    <el-checkbox v-if="props.itemCategoryName === '电子式绝缘电阻表(数字式)' || props.itemCategoryName === '电子式绝缘电阻表(指针式)' || props.itemCategoryName === '兆欧表(指针式)'" v-model="showListTop" :checked="true" :disabled="pageType === 'detail'">
      开路电压
    </el-checkbox>
    <el-checkbox v-if="props.itemCategoryName === '数字式接地电阻表' || props.itemCategoryName === '模拟式接地电阻表'" v-model="showListTop" :checked="true" :disabled="pageType === 'detail'">
      示值误差
    </el-checkbox>
  </div>
  <!-- 上面表格 -->
  <detail-block v-if="showListTop" :title="props.pageType === 'detail' ? '' : ' '" style="margin-top: 0;" @mouseleave="mouseoutTable('top')">
    <template v-if="props.pageType !== 'detail'" #btns>
      <el-button type="primary" @click="addRow('1')">
        增加行
      </el-button>
      <el-button type="info" @click="deleteRow('top')">
        删除行
      </el-button>
      <el-button v-if="props.itemCategoryName !== '数字式接地电阻表'" type="primary" @click="calculateDataTop">
        计算结果
      </el-button>
    </template>
    <el-table
      ref="tableRef1"
      v-loading="tableLoading"
      :data="listTop"
      border
      stripe
      style="width: 100%;"
      :height="listTop.length > 10 ? 500 : null"
      @selection-change="(e: any) => handleSelectionChange(e, 'top')"
      @row-contextmenu="contextmenu1"
    >
      <el-table-column v-if="pageType !== 'detail'" type="selection" width="38" />
      <el-table-column align="center" label="序号" width="80" type="index" />
      <el-table-column
        v-for="item in columns_top"
        :key="item.value"
        :prop="item.value"
        :label="item.text"
        :width="item.width"
        align="center"
      >
        <template #header>
          <span v-show="item.required" style="color: red;">*</span><span>{{ item.text }}</span>
        </template>
        <template #default="scope">
          <!-- 标称值、最小允许值相关系数a、最大允许值相关系数b、准确度a -->
          <precision-input-number
            v-if="props.pageType !== 'detail' && (item.value === 'standardValue' || item.value === 'measureIndicationValue' || item.value === 'nominalValue' || item.value === 'minimumAllowFactorA' || item.value === 'maximumAllowFactorB' || item.value === 'errorParamA' || item.value === 'errorParamB' || item.value === 'accuracyA')"
            v-model="scope.row[item.value]"
            :placeholder="pageType === 'detail' ? '' : `${item.text}`"
            :disabled="pageType === 'detail'"
            class="full-width-input"
          />

          <!-- 标准值 -->
          <precision-input-number
            v-if="pageType !== 'detail' && (item.value === 'standardValue') && props.itemCategoryName === '模拟式接地电阻表'"
            v-model="scope.row[item.value]"
            :placeholder="pageType === 'detail' ? '' : `${item.text}`"
            :disabled="pageType === 'detail'"
            class="full-width-input"
            :precision="caculateDecimalDigits(scope.row.resolution)"
          />
          <!-- 量程(数字式接地电阻) -->
          <el-select v-if="pageType !== 'detail' && item.value === 'rangeRange' && props.itemCategoryName === '数字式接地电阻表' && (scope.$index === 0 || scope.$index === 10 || scope.$index === 20 || scope.$index === 30 || scope.$index === 40 || scope.$index === 50)" v-model="scope.row[item.value]" :placeholder="pageType === 'detail' ? '' : `${item.text}`" :disabled="pageType === 'detail'" class="full-width-input" @change="(value) => changeSelectRange(value, scope.$index, 9)">
            <el-option v-for="i in standard3RangeNumberList" :key="i.id" :label="i.name" :value="i.value" :disabled="selectedRange.includes(i.value)" />
          </el-select>
          <!-- 量程(模拟式接地电阻) -->
          <el-select v-if="pageType !== 'detail' && item.value === 'rangeRange' && props.itemCategoryName === '模拟式接地电阻表' && (scope.$index === 0 || scope.$index === 11 || scope.$index === 22 || scope.$index === 33 || scope.$index === 44)" v-model="scope.row[item.value]" :placeholder="pageType === 'detail' ? '' : `${item.text}`" :disabled="pageType === 'detail'" class="full-width-input" @change="(value) => changeSelectRange(value, scope.$index, 10)">
            <el-option v-for="i in standard3RangeSimulationList" :key="i.id" :label="i.name" :value="i.name" />
          </el-select>
          <!-- 最大允许误差 -->
          <el-select v-if="pageType !== 'detail' && item.value === 'maximumError' && props.itemCategoryName === '数字式接地电阻表'" v-model="scope.row[item.value]" :placeholder="pageType === 'detail' ? '' : `${item.text}`" :disabled="pageType === 'detail'" class="full-width-input">
            <el-option v-for="i in standardMaximumErrorList" :key="i.id" :label="i.name" :value="i.name" />
          </el-select>
          <!-- 分辨力  -->
          <div v-if="item.value === 'resolution' && props.pageType !== 'detail'" style="display: flex;">
            <icon-button v-if="pageType !== 'detail'" style="margin-right: 10px;" size="20" icon="icon-reduce-circle" title="扩大10倍" type="primary" @click="clickSub(scope.row)" />
            <el-input
              v-if="props.pageType !== 'detail' " v-model="scope.row[item.value]" :autofocus="true" :placeholder="`${item.text}`" class="input"
            />
            <icon-button v-if="pageType !== 'detail'" style="margin-left: 10px;" size="20" icon="icon-add-circle" title="缩小10倍" type="primary" @click="clickAdd(scope.row)" />
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 自定义菜单 -->
    <div :id="randomNum1" class="custom-menu">
      <p class="menu-item" @click="costomAddRow('current-pre', 'top')">
        当前行前方插入
      </p>
      <p class="menu-item" @click="costomAddRow('current-next', 'top')">
        当前行后方插入
      </p>
      <p class="menu-item" @click="costomAddRow('list-head', 'top')">
        列表头行插入
      </p>
      <p class="menu-item" @click="costomAddRow('list-tail', 'top')">
        列表尾行插入
      </p>
      <p v-if="pageType !== 'detail'" class="menu-item" @click="costomAddRow('select-pre', 'top')">
        选中行前方插入
      </p>
      <!--  -->
      <p v-if="pageType !== 'detail'" class="menu-item" @click="costomAddRow('select-next', 'top')">
        选中行后方插入
      </p>
      <p class="menu-item" @click="costomAddRow('del-current', 'top')">
        删除当前行
      </p>
      <p v-if="pageType !== 'detail'" class="menu-item" @click="costomAddRow('del-select', 'top')">
        删除选中行
      </p>
    </div>
  </detail-block>

  <div style="padding: 0 10px;">
    <el-checkbox v-if="props.itemCategoryName === '电子式绝缘电阻表(数字式)' || props.itemCategoryName === '电子式绝缘电阻表(指针式)' || props.itemCategoryName === '兆欧表(指针式)'" v-model="showListBottom" :checked="true" :disabled="pageType === 'detail'">
      示值误差
    </el-checkbox>
    <el-checkbox v-if="props.itemCategoryName === '数字式接地电阻表' || props.itemCategoryName === '模拟式接地电阻表'" v-model="showListBottom" :checked="true" :disabled="pageType === 'detail'">
      辅助接地电阻试验
    </el-checkbox>
  </div>
  <!-- 下面表格  -->
  <detail-block v-if="showListBottom" :title="props.pageType === 'detail' ? '' : ' '" style="margin-top: 0;" @mouseleave="mouseoutTable('bottom')">
    <template v-if="props.pageType !== 'detail'" #btns>
      <el-button v-if="props.itemCategoryName === '电子式绝缘电阻表(数字式)' || props.itemCategoryName === '电子式绝缘电阻表(指针式)' || props.itemCategoryName === '兆欧表(指针式)'" type="primary" @click="addRow('2')">
        增加行
      </el-button>
      <el-button v-if="props.itemCategoryName === '电子式绝缘电阻表(数字式)' || props.itemCategoryName === '电子式绝缘电阻表(指针式)' || props.itemCategoryName === '兆欧表(指针式)'" type="info" @click="deleteRow('bottom')">
        删除行
      </el-button>
      <el-button v-if="props.itemCategoryName === '电子式绝缘电阻表(指针式)' || props.itemCategoryName === '兆欧表(指针式)'" type="primary" @click="calculateDataBottom">
        计算结果
      </el-button>
      <el-button v-if="props.itemCategoryName === '数字式接地电阻表' || props.itemCategoryName === '模拟式接地电阻表'" type="primary" @click="createDataBottom">
        生成辅助接地电阻
      </el-button>
    </template>
    <el-table
      ref="tableRef2"
      v-loading="tableLoadingBottom"
      :data="listBottom"
      border
      stripe
      :height="listBottom.length > 10 ? 500 : null"
      style="width: 100%;"
      :span-method="mergeTableRow"
      @selection-change="(e: any) => handleSelectionChange(e, 'bottom')"
      @row-contextmenu="contextmenu2"
    >
      <el-table-column v-if="pageType !== 'detail' && (props.itemCategoryName === '电子式绝缘电阻表(数字式)' || props.itemCategoryName === '电子式绝缘电阻表(指针式)' || props.itemCategoryName === '兆欧表(指针式)')" type="selection" width="38" />
      <el-table-column align="center" label="序号" width="80" type="index" />
      <el-table-column
        v-for="item in columns_bottom"
        :key="item.value"
        :prop="item.value"
        :label="item.text"
        :width="item.width"
        align="center"
      >
        <template #header>
          <span v-show="item.required" style="color: red;">*</span><span>{{ item.text }}</span>
        </template>
        <template #default="scope">
          <!-- 标准值、误差参数a、误差参数b、被检表示值、准确度a -->
          <precision-input-number
            v-if="props.pageType !== 'detail' && (props.itemCategoryName !== '数字式接地电阻表' && props.itemCategoryName !== '模拟式接地电阻表')
              && ((item.value === 'measureIndicationValue' && props.itemCategoryName !== '电子式绝缘电阻表(指针式)' && props.itemCategoryName !== '兆欧表(指针式)')
                || item.value === 'accuracyA' || item.value === 'errorParamA' || item.value === 'errorParamB' || item.value === 'standardValue')"
            v-model="scope.row[item.value]"
            :placeholder="pageType === 'detail' ? '' : `${item.text}`"
            :disabled="pageType === 'detail'"
            class="full-width-input"
          />
          <!-- 被检表示值 -->
          <el-autocomplete
            v-if="props.pageType !== 'detail' && (props.itemCategoryName === '电子式绝缘电阻表(指针式)' || props.itemCategoryName === '兆欧表(指针式)') && item.value === 'measureIndicationValue'"
            v-model="scope.row[item.value]"
            :fetch-suggestions="suggestions"
            :placeholder="pageType === 'detail' ? '' : `${item.text}`"
            :disabled="pageType === 'detail'"
            style="width: 90%;"
            clearable
          />
          <!-- 端钮电压 -->
          <!-- <el-select v-if="pageType !== 'detail' && item.value === 'terminalVoltage'" v-model="scope.row[item.value]" :placeholder="pageType === 'detail' ? '' : `${item.text}`" :disabled="pageType === 'detail'" class="full-width-input">
            <el-option v-for="i in standardEndButtonVoltageList" :key="i.id" :label="i.name" :value="i.value" />
          </el-select> -->
          <!-- 端钮电压 -->
          <select-by-dict
            v-if="pageType !== 'detail' && item.value === 'terminalVoltage'"
            v-model:model-value="scope.row[item.value]"
            :placeholder="pageType === 'detail' ? '' : `${item.text}`"
            :disabled="pageType === 'detail'"
            class="full-width-input"
            dict-code="standardEndButtonVoltage"
          />
          <!-- 量程 -->
          <!-- <el-select v-if="pageType !== 'detail' && item.value === 'rangeRange'" v-model="scope.row[item.value]" :placeholder="pageType === 'detail' ? '' : `${item.text}`" :disabled="pageType === 'detail'" class="full-width-input">
            <el-option v-for="i in standardRangeRangeList" :key="i.id" :label="i.name" :value="i.value" />
          </el-select> -->
          <select-by-dict
            v-if="pageType !== 'detail' && item.value === 'rangeRange'"
            v-model:model-value="scope.row[item.value]"
            :placeholder="pageType === 'detail' ? '' : `${item.text}`"
            :disabled="pageType === 'detail'"
            class="full-width-input"
            dict-code="standardRangeRange"
          />
          <!-- 标准值单位 -->
          <el-select v-if="pageType !== 'detail' && item.value === 'unit' && props.itemCategoryName !== '数字式接地电阻表' && props.itemCategoryName !== '模拟式接地电阻表'" v-model="scope.row[item.value]" :placeholder="pageType === 'detail' ? '' : `${item.text}`" :disabled="pageType === 'detail'" class="full-width-input">
            <el-option v-for="i in standardResistanceList" :key="i.id" :label="i.name" :value="i.name" />
          </el-select>
          <!-- 最大允许误差 -->
          <el-select v-if="pageType !== 'detail' && item.value === 'maximumError' && props.itemCategoryName === '电子式绝缘电阻表(数字式)'" v-model="scope.row[item.value]" :placeholder="pageType === 'detail' ? '' : `${item.text}`" :disabled="pageType === 'detail'" class="full-width-input">
            <el-option v-for="i in standardMaximumErrorList" :key="i.id" :label="i.name" :value="i.name" />
          </el-select>
          <!-- 分辨力  -->
          <div v-if="item.value === 'resolution' && props.pageType !== 'detail'" style="display: flex;">
            <icon-button v-if="pageType !== 'detail'" style="margin-right: 10px;" size="20" icon="icon-reduce-circle" title="扩大10倍" type="primary" @click="clickSub(scope.row)" />
            <el-input
              v-if="props.pageType !== 'detail' " v-model="scope.row[item.value]" :autofocus="true" :placeholder="`${item.text}`" class="input"
            />
            <icon-button v-if="pageType !== 'detail'" style="margin-left: 10px;" size="20" icon="icon-add-circle" title="缩小10倍" type="primary" @click="clickAdd(scope.row)" />
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 自定义菜单 -->
    <div :id="randomNum2" class="custom-menu">
      <p class="menu-item" @click="costomAddRow('current-pre', 'bottom')">
        当前行前方插入
      </p>
      <p class="menu-item" @click="costomAddRow('current-next', 'bottom')">
        当前行后方插入
      </p>
      <p class="menu-item" @click="costomAddRow('list-head', 'bottom')">
        列表头行插入
      </p>
      <p class="menu-item" @click="costomAddRow('list-tail', 'bottom')">
        列表尾行插入
      </p>
      <p v-if="pageType !== 'detail'" class="menu-item" @click="costomAddRow('select-pre', 'bottom')">
        选中行前方插入
      </p>
      <!--  -->
      <p v-if="pageType !== 'detail'" class="menu-item" @click="costomAddRow('select-next', 'bottom')">
        选中行后方插入
      </p>
      <p class="menu-item" @click="costomAddRow('del-current', 'bottom')">
        删除当前行
      </p>
      <p v-if="pageType !== 'detail'" class="menu-item" @click="costomAddRow('del-select', 'bottom')">
        删除选中行
      </p>
    </div>
  </detail-block>
</template>

<style lang="scss" scoped>
.custom-menu {
  display: none;
  position: fixed;
  background-color: #fff;
  border-radius: 5px;
  padding: 5px 0;
  z-index: 1000;
  border: 1px solid #c8c9cc;
  box-shadow: 0 0 12px rgb(0 0 0 / 12%);

  .menu-item {
    display: flex;
    align-items: center;
    white-space: nowrap;
    list-style: none;
    line-height: 22px;
    padding: 5px 16px;
    margin: 0;
    // font-size: var(--el-font-size-base);
    color: #606266;
    cursor: pointer;
    outline: none;

    &:hover {
      background-color: #ecf5ff;
      color: #409eff;
    }
  }
}
</style>