<!--
  Description: 设备管理
  Author: 李亚光
  Date: 2024-11-01
 -->
<script lang="ts" setup name="DeviceManageList">
import { ElMessage, ElMessageBox } from 'element-plus'
import addDialog from './components/addDialog.vue'
import { getDeviceList, removeDevice } from '@/api/page/device'
const $router = useRouter()
// 获取页面高度
const pageHeight = ref(window.innerHeight - 50 - 60 - 20 - 20 - 10 - 30 - 15 - 15 - 20)
window.addEventListener('resize', () => {
  pageHeight.value = window.innerHeight - 50 - 60 - 20 - 20 - 10 - 30 - 15 - 15 - 20
})
const loadingTable = ref(true)
//  查询条件
const listQuery = ref({
  limit: 20,
  offset: 1,
  name: '',
  device_type: '',
})
const list = ref<any[]>([])
const total = ref(0)
const columns = ref([
  { text: '摄像头名称', value: 'name', align: 'center' },
  { text: '摄像头编号', value: 'code', align: 'center' },
  { text: 'RTSP地址', value: 'input_stream_url', align: 'center' },
  { text: '识别间隔', value: 'image_save_interval', align: 'center' },
  { text: '告警间隔', value: 'alarm_interval', align: 'center' },
  { text: '关联算法', value: 'relation_model_names', align: 'center' },
  { text: '在线状态', value: 'status', align: 'center' },
  { text: '类型', value: 'typeName', align: 'center' },
])
const fetchData = () => {
  loadingTable.value = true
  getDeviceList(listQuery.value).then((res) => {
    list.value = res.data.items.map((item: any) => ({
      ...item,
      typeName: item.type === '1' ? '摄像头' : '安全树',
    }))
    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,
    name: '',
    device_type: '',
  }
  fetchData()
}
// 查看/编辑/新建
const addRef = ref()
const editRow = (row: any, type: string) => {
  addRef.value.initDialog(row, type)
}
// 删除
const deleteRow = (row: any) => {
  ElMessageBox.confirm(
    '确定要该数据吗？',
    '确认操作',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    },
  ).then(() => {
    removeDevice(row.id).then(() => {
      ElMessage.success('操作成功')
      fetchData()
    })
  })
}
// 预览
const preview = (row: any) => {
  if (row.status !== '在线') {
    ElMessage.warning('设备不在线')
    return
  }
  $router.push({
    path: '/device/preview',
    query: {
      row: JSON.stringify(row),
    },
  })
}
</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.name" placeholder="设备名称" clearable />
      </search-item>
      <search-item>
        <el-select v-model="listQuery.device_type" placeholder="设备类型" clearable class="select" style="width: 192px;">
          <!-- <el-option v-for="item in []" :key="item.id" :value="item.value" :label="item.name" /> -->
          <el-option value="1" label="摄像头" />
          <el-option value="2" label="安全树" />
        </el-select>
      </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"
        :height="pageHeight" @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" @click="preview(scope.row)">
                预览
              </el-button>
              <el-button type="danger" link size="small" @click="deleteRow(scope.row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>
