<!-- 设备管理 -->
<script lang="ts" setup name="device">
import { ElMessage, ElMessageBox } from 'element-plus'
import editList from './pageAdd.vue'
import { deleteDeviceList, getDeviceListPage } from '@/api/api/index'
import { getDictByCode } from '@/api/system/dict'
const $router = useRouter()
// 查询参数
const searchQuery = reactive({
  offset: 1,
  limit: 20,
  ptn: '',
  deviceType: '',
})
const loadingTable = ref(true)
const columns = ref([
  { text: '设备名称', value: 'deviceName', align: 'center' },
  { text: '设备类型', value: 'deviceTypeName', align: 'center' },
  { text: '设备编号', value: 'deviceNo', align: 'center' },
  { text: '安装位置', value: 'installLocaltion', align: 'center' },
  { text: '安装时间', value: 'installTime', align: 'center' },
  { text: '所属分区', value: 'ptnName', align: 'center' },
  // { text: '监测对象', value: 'monitorObject', align: 'center' },
  { text: '当前状态', value: 'statName', align: 'center' },
  { text: '监测对象', value: 'monitorObjName', align: 'center' },
]) // 表格
const total = ref<number>(0)
const list = ref<any[]>([])
// 获取数据列表
const fetchDataList = () => {
  loadingTable.value = true
  getDeviceListPage(searchQuery).then((res) => {
    list.value = res.data.rows
    total.value = res.data.total
    loadingTable.value = false
  })
}
const ptnList = ref([])
// 获取所属分区列表
const fetchptnList = () => {
  getDictByCode('ptn_type').then((res) => {
    ptnList.value = res.data
  })
}
fetchptnList()
const deviceTypeList = ref([])
// 获取设备类型列表
const fetchDeviceType = () => {
  getDictByCode('device_type').then((res) => {
    deviceTypeList.value = res.data
  })
}
fetchDeviceType()
// 搜索
const search = () => {
  fetchDataList()
}
// 新增或编辑组件
const editRef = ref()
// 新增
const add = () => {
  editRef.value.initDialog('create')
}
// 编辑
const edit = (row: any) => {
  row.ptn = row.ptn.toString()
  row.stat = row.stat.toString()
  row.deviceType = row.deviceType.toString()
  editRef.value.initDialog('update', row)
}
// 删除
const del = (row: any) => {
  console.log(row, 'row')
  ElMessageBox.confirm(
    '确认删除选中的数据吗?',
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  )
    .then(() => {
      deleteDeviceList(row.id).then((res) => {
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        search()
      })
    })
    .catch(() => {
    })
}
// 详情分析
const detail = (row: any) => {
  console.log(row, '设备信息')
  $router.push({
    name: 'DdeviceDetail',
    query: {
      id: row.id,
      monitor: row.monitorObj,
      deviceType: row.deviceType,
    },
  })
}
// 重置
const reset = () => {
  searchQuery.deviceType = ''
  searchQuery.ptn = ''
  searchQuery.offset = 1
  searchQuery.limit = 20
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size?: number; page?: number }) => {
  if (val && val.size) {
    searchQuery.limit = val.size
  }
  if (val && val.page) {
    searchQuery.offset = val.page
  }
  search()
}
// 表格被选中的行
const selectList = ref<any[]>([])
// 表格多选
const multiSelect = (row: any[]) => {
  selectList.value = row
}
onMounted(() => {
  search()
})
</script>

<template>
  <div>
    <!-- 编辑新建页面 -->
    <edit-list ref="editRef" @refresh="search" />
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-select v-model="searchQuery.deviceType" placeholder="设备类型">
          <el-option v-for="item in deviceTypeList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-select v-model="searchQuery.ptn" placeholder="所属分区">
          <el-option v-for="item in ptnList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
    </search-area>
    <table-container>
      <template #btns-right>
        <icon-button icon="icon-add" title="新增" @click="add" />
      </template>
      <!-- 表格区域 -->
      <normal-table
        :data="list" :total="total" :columns="columns as any"
        :query="{ limit: searchQuery.limit, offset: searchQuery.offset }" :list-loading="loadingTable"
        :is-showmulti-select="true" :is-multi="true" @change="changePage" @multiSelect="multiSelect"
      >
        <template #preColumns>
          <el-table-column label="序号" width="55" align="center">
            <template #default="scope">
              {{ (searchQuery.offset - 1) * searchQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
        <template #columns>
          <el-table-column label="详情分析" align="center" width="120">
            <template #default="{ row }">
              <el-button type="primary" link size="small" @click="detail(row)">
                详情分析
              </el-button>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="120">
            <template #default="{ row }">
              <el-button type="primary" link size="small" @click="edit(row)">
                修改
              </el-button>
              <el-button type="primary" link size="small" @click="del(row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </div>
</template>

