<!--
  Description: 设备管理
  Author: 李亚光
  Date: 2024-11-01
 -->
<script lang="ts" setup name="DeviceManageList">
import addDialog from './components/addDialog.vue'
import { getDeviceList } from '@/api/page/device'
const loadingTable = ref(true)
//  查询条件
const listQuery = ref({
  limit: 20,
  offset: 1,
})
const list = ref<any[]>([])
const total = ref(0)
const columns = ref([
  { text: '摄像头名称', value: 'name', align: 'center' },
  { text: '摄像头编号', value: 'code', align: 'center' },
  { text: '一体机编号', value: 'deviceTypeName', align: 'center' },
  { text: 'RTSP地址', value: 'output_stream_url', align: 'center' },
  { text: '识别间隔', value: 'deviceTypeName', align: 'center' },
  { text: '告警间隔', value: 'deviceTypeName', align: 'center' },
  { text: '关联算法', value: 'deviceTypeName', align: 'center' },
  { text: '在线状态', value: 'type', align: 'center' },
])
const fetchData = () => {
  loadingTable.value = true
  getDeviceList(listQuery.value).then((res) => {
    console.log(res.data, '')
    list.value = res.data.items
    total.value = res.data.total
    loadingTable.value = false
  }).catch(() => {
    loadingTable.value = false
  })
}
fetchData()
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
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()
}
const reset = () => {
  listQuery.value = {
    limit: 20,
    offset: 1,
  }
  fetchData()
}

const addRef = ref()
const editRow = (row: any, type: string) => {
  addRef.value.initDialog(row, type)
}
</script>

<template>
  <!-- 布局 -->
  <app-container>
    <add-dialog ref="addRef" @refresh="fetchData" />
    <search-area :need-clear="true" @search="fetchData" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.position" placeholder="设备名称" clearable />
      </search-item>
      <search-item>
        <el-select v-model="listQuery.processStatus" placeholder="在线状态" clearable class="select" style="width: 192px;">
          <el-option v-for="item in []" :key="item.id" :value="item.value" :label="item.name" />
        </el-select>
      </search-item>
    </search-area>
    <!-- 表头标题 -->
    <table-container>
      <template #btns-right>
        <!-- 操作 -->
        <div>
          <el-button type="primary" @click="editRow({}, 'create')">
            新建
          </el-button>
        </div>
      </template>
      <!-- 查询结果Table显示 -->
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery" :list-loading="loadingTable"
        @change="changePage"
      >
        <template #preColumns>
          <el-table-column label="序号" width="55" align="center">
            <template #default="scope">
              {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
        <template #columns>
          <el-table-column label="操作" align="center" width="160">
            <template #default="scope">
              <el-button type="primary" link size="small" @click="editRow(scope.row, 'detail')">
                查看
              </el-button>
              <el-button type="primary" link size="small" @click="editRow(scope.row, 'update')">
                编辑
              </el-button>
              <el-button type="primary" link size="small">
                预览
              </el-button>
              <el-button type="danger" link size="small" @click="removeRow(scope.row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>
