Newer
Older
smart-metering-front / src / views / device / receive / selectDeviceDialog.vue
<!-- 选择设备对话框 -->
<script lang="ts" setup name="ReceiveDialog">
import { ref } from 'vue'
import dayjs from 'dayjs'
import type { IdeviceList } from './receive'
import useUserStore from '@/store/modules/user'
import { listPageApi } from '@/api/device/standingBook'
import type { TableColumn } from '@/components/NormalTable/table_interface'

const props = defineProps({
  dialogSelectDiviceVisible: {
    type: Boolean,
    default: false,
  },
  isMulti: Boolean,
})
const emits = defineEmits(['closeDialog', 'updateDeviceConfirm'])
const loadingTable = ref<boolean>(false)
const userStore = useUserStore()
// 查询条件
const listQuery = ref({
  equipmentName: '', // 设备名称
  equipmentNo: '', // 设备编号
  offset: 1,
  limit: 20,
})
const total = ref(20)
// 多选选中的内容
const checkoutList = ref([])

const list = ref([])// 表格数据
const columns = ref<TableColumn[]>([
  { text: '设备名称', value: 'equipmentName', align: 'center', fixed: true },
  { text: '设备编号', value: 'equipmentNo', align: 'center', fixed: true },
  { text: '型号', value: 'modelNo', align: 'center' },
  { text: '出厂编号', value: 'manufacturingNo', align: 'center' },
  { text: '管理状态', value: 'managerStateName', align: 'center' },
  { text: '使用人', value: 'usePersonName', align: 'center' },
  { text: '有效日期', value: 'validDate', align: 'center' },
])
const table = ref()
const dialogTableVisible = ref(false)// 控制对话框显隐

// 获取数据列表
const fetchData = () => {
  loadingTable.value = true
  listPageApi(listQuery.value).then((res) => {
    list.value = res.data.rows.map((item: IdeviceList) => {
      return {
        ...item,
        validDate: item.validDate ? dayjs(item.validDate).format('YYYY-MM-DD') : item.validDate,
      }
    })
    total.value = res.data.total
    loadingTable.value = false
  }).catch((_) => {
    loadingTable.value = false
  })
}
watch(() => props.dialogSelectDiviceVisible, (newValue) => {
  dialogTableVisible.value = newValue
  fetchData()
  if (!newValue) {
    table.value.clearMulti() // 清除多选选中
  }
})
// 关闭对话框
const close = () => {
  emits('closeDialog')
}
// 确认
const confirm = () => {
  emits('updateDeviceConfirm', checkoutList.value)
  close()
}
// 重置
const clearList = () => {
  listQuery.value.equipmentName = ''
  listQuery.value.equipmentNo = ''
  fetchData()
}
// 搜索
const searchList = () => {
  fetchData()
}
// 多选选中
const handleSelectionChange = (val: any) => {
  console.log('多选', val)
  checkoutList.value = val
}
// 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
const changePage = (val: { size?: number; page?: number }) => {
  if (val && val.size) {
    listQuery.value.limit = val.size
  }
  if (val && val.page) {
    listQuery.value.offset = val.page
  }
  fetchData()
}
</script>

<template>
  <el-dialog v-model="dialogTableVisible" width="60%" title="选择设备" class="select-device-dialog" @close="close">
    <search-area
      :need-clear="true"
      @search="searchList" @clear="clearList"
    >
      <search-item>
        <el-input
          v-model.trim="listQuery.equipmentName"
          placeholder="设备名称"
          clearable
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.equipmentNo"
          placeholder="设备编号"
          clearable
        />
      </search-item>
    </search-area>
    <table-container>
      <normal-table
        ref="table"
        :data="list"
        :total="total"
        :columns="columns"
        :query="listQuery"
        :is-multi="isMulti"
        is-showmulti-select
        :list-loading="loadingTable"
        @change="changePage"
        @multiSelect="handleSelectionChange"
      >
        <template #preColumns>
          <el-table-column label="#" width="55" align="center" fixed>
            <template #default="scope">
              {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="confirm">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
// 样式
.receive-dialog {
  .receive-dialog-item {
    display: flex;
    align-items: center;
    margin-bottom: 28px;

    .text {
      margin-right: 30px;
      white-space: nowrap;
    }
  }
}
</style>