Newer
Older
xc-metering-front / src / views / tested / subpackage / task / components / deviceTable.vue
<!-- 受检设备信息列表 -->
<script lang="ts" setup name="ReviewDeviceTable">
import { ElMessage, ElMessageBox } from 'element-plus'
import { template } from 'lodash-es'
import { useCheckList } from '@/utils/useCheckList'
// import selectDevice from '@/views/tested/MeasurementPlan/plan/components/selectDevice.vue'
import selectDevice from '@/views/tested/MeasurementPlan/task/components/selectDeviceMultiple.vue'
import { uniqueMultiArray } from '@/utils/Array'
const $props = defineProps({
  data: {
    type: Array,
    default: () => ([]),
  },
})
const $route = useRoute()
const $router = useRouter()
// 表头显示标题
const columns = ref([
  {
    text: '设备名称',
    value: 'equipmentName',
    required: true,
    type: 'text',
  },
  {
    text: '规格型号',
    value: 'model',
    required: false,
    type: 'text',
  },
  {
    text: '出厂编号',
    value: 'manufactureNo',
    required: false,
    type: 'text',
  },
  {
    text: '生产厂家',
    value: 'manufacturer',
    required: false,
    type: 'text',
  },
  {
    text: '证书有效期',
    value: 'certificateValid',
    required: false,
    type: 'text',
  },
  {
    text: '上次送检单位',
    value: 'checkOrganization',
    required: false,
    type: 'text',
  },
  {
    text: '使用部门',
    value: 'deptName',
    required: false,
    type: 'text',
  },
  {
    text: '计量标识',
    value: 'meterIdentifyName',
    required: false,
    type: 'text',
  },
  {
    text: '特殊要求',
    value: 'specialRequire',
    required: false,
    type: 'input',
  },
])
const list = ref<any[]>([])
// 检查数据列表
function checkCertificateList() {
  return useCheckList(list.value, columns.value as any, '外送设备清单')
}
// 将列表置为不可编辑状态
function setAllRowReadable() {
  for (const item of list.value) {
    item.editable = false
  }
}
// 双击行显示输入框
const dblclickRow = (row: any) => {
  if ($route.path.includes('detail')) {
    return
  }
  setAllRowReadable()
  row.editable = true
}
const SelectionList = ref()
// 表格选中
const handleSelectionChange = (e: any[]) => {
  SelectionList.value = e
}
watch(() => $props.data, (newVal) => {
  if (newVal) {
    list.value = newVal
    fetchTogetherData()
  }
})
const initDialog = () => {
  list.value = $props.data
}
initDialog()
defineExpose({
  list,
  checkCertificateList,
})
// 选择设备
const deviceRef = ref()
const confirm = (device: any) => {
  // 多选
  device.forEach((item: any) => {
    list.value.push({
      ...item,
      equipmentId: item.id,
      id: null,
      groupId: null,
      editable: true,
    })
  })
}
// 批量添加行
const addRow = () => {
  deviceRef.value.initDialog(true, 'subpackage')
}
// 删除行
const removeRow = () => {
  list.value = list.value.filter((item) => {
    return !SelectionList.value.includes(item)
  })
}
const deviceDetail = (row: any, column: any) => {
  if (column.text === '设备名称') {
    $router.push({
      path: '/info/detail',
      query: {
        id: row.equipmentId,
        statusName: '全部',
        equipmentType: '1',
        row: JSON.stringify(row),
      },
    })
  }
}
// --------------------------------------------表格操作--------------------------------------------
// 表格标识
const tableFlag = ref('normal')// normal普通表格  together聚合表格
// 切换表格
const changeTable = () => {
  if (tableFlag.value === 'normal') {
    tableFlag.value = 'together'
  }
  else {
    tableFlag.value = 'normal'
  }
}

const togetherColumns = ref([
  {
    text: '',
    value: '',
    align: 'center',
    width: '38',
  },
  {
    text: '设备名称',
    value: 'equipmentName',
    align: 'center',
  },
  {
    text: '总计',
    value: 'deviceNum',
    align: 'center',
  },
])
const togetherList = ref<any>([])
const expandList = ref<any>([])
function fetchTogetherData() {
  togetherList.value = []
  const equipmentNames = uniqueMultiArray(list.value, 'equipmentName')
  equipmentNames.forEach((element: any) => {
    togetherList.value.push({
      equipmentName: element.equipmentName,
      deviceNum: list.value.filter((citem: any) => citem.equipmentName === element.equipmentName).length,
    })
  })
}

const getRowKeys = (row: any) => { // 获取当前行id
  return row.equipmentName // 这里看这一行中需要根据哪个属性值是id
}
// 控制展开行
const expands = ref<any[]>([])
const expandChange = (row: any, expandedRows: any) => {
  if (expandedRows.length) {
    expands.value = []
    if (row) {
      expands.value.push(row.equipmentName)
    }
  }
  else {
    expands.value = []
  }
}
watch(() => expands.value, (newVal) => {
  expandList.value = list.value.filter((item: any) => item.equipmentName === newVal[0])
},
{
  deep: true,
})
</script>

<template>
  <detail-block-switch title="外送设备清单">
    <select-device ref="deviceRef" @add="confirm" />
    <template #btns>
      <template v-if="!$route.path.includes('detail')">
        <el-button type="primary" @click="addRow">
          批量添加
        </el-button>
        <el-button type="info" @click="removeRow">
          删除行
        </el-button>
      </template>
      <template v-if="$route.path.includes('detail')">
        <icon-button icon="icon-change" title="切换列表状态" @click="changeTable" />
      </template>
    </template>
    <!-- 普通表格 -->
    <el-table
      v-show="tableFlag === 'normal'"
      ref="multipleTableRef" :data="list" style="width: 100%;" border @selection-change="handleSelectionChange"
      @row-dblclick="dblclickRow"
    >
      <el-table-column v-if="!$route.path.includes('detail')" type="selection" width="38" />
      <el-table-column align="center" label="序号" width="80" type="index" />
      <el-table-column v-for="item in columns" :key="item.value" :prop="item.value" :label="item.text" align="center">
        <template #header>
          <span v-show="item.required" style="color: red;">*</span><span>{{ item.text }}</span>
        </template>
        <template #default="scope">
          <span v-if="!scope.row.editable" class="cur" @click="deviceDetail(scope.row, item)">{{ scope.row[item.value]
          }}</span>
          <span v-if="scope.row.editable && item.type === 'text'" class="cur" @click="deviceDetail(scope.row, item)">{{
            scope.row[item.value] }}</span>
          <el-input
            v-if="scope.row.editable && item.type === 'input'" v-model="scope.row[item.value]"
            :placeholder="`${item.text}`" class="input"
          />
          <el-date-picker
            v-if="scope.row.editable && item.type === 'date'" v-model="scope.row[item.value]" type="date"
            :placeholder="`${item.text}`" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
          />
        </template>
      </el-table-column>
    </el-table>
    <!-- 头部 -->
    <normal-table
      v-show="tableFlag === 'together'" class="nortable-header" :data=" [] " :total=" 0 " :columns="togetherColumns as any" :options="
        {
          needIndex: false, // 是否需要序号列
          border: true, // 是否需要上方边框
        }
      " :pagination=" false " :is-showmulti-select=" false " :query="{}"
    />
    <!-- 身体 -->
    <el-table
      v-show="tableFlag === 'together'"
      ref="togetherTableRef" :data=" togetherList " border style="width: 100%;"
      :row-key=" getRowKeys " :expand-row-keys=" expands " :show-header=" false " @expand-change=" expandChange "
    >
      <el-table-column type="expand" width="38">
        <template #default>
          <el-table
            ref="expandTableRef"
            :data=" expandList " border style="width: 100%;"
          >
            <el-table-column v-for="item in columns" :key="item.value" :label="item.text" :prop="item.value" align="center" />
          </el-table>
        </template>
      </el-table-column>
      <el-table-column label="设备名称" prop="equipmentName" align="center" />
      <el-table-column label="数量" align="center">
        <template #default=" scope ">
          共计{{ scope.row.deviceNum }}台
        </template>
      </el-table-column>
    </el-table>
  </detail-block-switch>
</template>

<style lang="scss" scoped>
.el-dialog {
  width: 700px;
}

.el-select {
  width: 100%;
}

.cur {
  &:hover {
    cursor: pointer;
  }
}

.nortable-header {
  ::v-deep(.el-table__body-wrapper) {
    display: none;
  }
}

::v-deep(.el-table__expanded-cell) {
  padding: 0;
}
</style>