Newer
Older
robot_dog_patrol_front / src / views / patrol / navigation / dialog / changeDeviceDialog.vue
<!-- 选择设备 -->
<script lang="ts" setup name="ChangeDeviceDialog">
import { ElMessage } from 'element-plus'
import type { Ref } from 'vue'
import dayjs from 'dayjs'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { SCHEDULE } from '@/utils/scheduleDict'
import { deleteDevice, getDeviceList } from '@/api/device/deviceManage'
import type { IList, IListQuery } from '@/views/device/deviceManage/deviceManage-interface'
const emits = defineEmits(['confirm'])
const dialogFormVisible = ref(false)
// 查询条件
const listQuery: Ref<IListQuery> = ref({
  robotCode: '', // 设备编号
  robotName: '', // 设备名称
  offset: 1,
  limit: 20,
})
// 多选选中的内容
const checkoutList = ref([])
const currentDeviceId = ref('')
const loadingTable = ref<boolean>(false)
const list = ref<IList[]>([]) // 表格数据
const total = ref<number>(0)
const columns = ref<TableColumn[]>([
  { text: '设备编号', value: 'robotCode', align: 'center' },
  { text: '设备名称', value: 'robotName', align: 'center' },
  { text: 'IP', value: 'robotIp', align: 'center' },
  { text: '端口', value: 'robotPort', align: 'center' },
  { text: '状态', value: 'robotStatusName', width: '90', align: 'center', styleFilter: (row: IList) => { return row.robotStatus == '1' ? 'color: #1aa034' : 'red' } },
  { text: '电量', value: 'robotCell', align: 'center' },
])

// 数据查询
function fetchData(isNowPage = false) {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页,否则跳转第一页
    listQuery.value.offset = 1
  }
  // 模拟数据
  getDeviceList(listQuery.value).then((res: { data: { rows: []; total: number } }) => {
    list.value = res.data.rows.map((item: IList) => {
      return {
        ...item,
        robotCell: `${item.robotCell}` ? `${item.robotCell}%` : item.robotCell, // 电量
        robotStatusName: `${item.robotStatus}` === '1' ? '在线' : '离线', // 设备状态
      }
    })
    total.value = res.data.total
    loadingTable.value = false
  })
}
// 重置
const reset = () => {
  listQuery.value = {
    robotCode: '', // 设备编号
    robotName: '', // 设备名称
    offset: 1,
    limit: 20,
  }
  fetchData()
}
// 搜索
const search = () => {
  fetchData(true)
}
// 多选选中
const handleSelectionChange = (val: any) => {
  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
  }
  search()
}
// 点击确定
const confirmSelect = () => {
  if (!checkoutList.value.length) {
    ElMessage.warning('请选择设备')
  }
  else {
    emits('confirm', checkoutList.value)
    dialogFormVisible.value = false
  }
}
// 取消
const resetForm = () => {
  dialogFormVisible.value = false
  reset()
}
// 初始化
const initDialog = (deviceId = '') => {
  currentDeviceId.value = deviceId
  dialogFormVisible.value = true
  fetchData()
}
defineExpose({ initDialog })
</script>

<template>
  <el-dialog v-if="dialogFormVisible" v-model="dialogFormVisible" title="选择设备" width="65%">
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.robotCode" placeholder="设备编号" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.robotName" placeholder="设备名称" clearable />
      </search-item>
    </search-area>

    <!-- 查询结果Table显示 -->
    <div style="padding: 12px;">
      <normal-table
        :data="list"
        :total="total"
        :columns="columns"
        :query="listQuery"
        is-showmulti-select
        :list-loading="loadingTable"
        :is-multi="false"
        :defaultSingleChecked="currentDeviceId"
        :height="360"
        :page-sizes="[20]"
        @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>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="confirmSelect">确认</el-button>
        <el-button @click="resetForm">
          取消
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
:deep(.el-radio__label) {
  display: none;
}
</style>