<!--
  Description: 设备管理页面
  Author: 李亚光
  Date: 2023-04-23
 -->
<script lang="ts" setup name="devicelist">
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import editList from './deviceEdit.vue'
import videoPage from './callDialog.vue'
import type { device } from './device-interface'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import type { DictDetail } from '@/views/system/dict/dict-interface'
import { delDevice, getDeviceListPage } from '@/api/device'
import { getDictByCode } from '@/api/system/dict'
const { proxy } = getCurrentInstance() as any
const listQuery = reactive({
  deviceCode: '',
  deviceType: '',
  offset: 1,
  limit: 20,
})
const columns = ref<TableColumn[]>([
  {
    text: '设备类型',
    value: 'deviceTypeName',
    align: 'center',
    // width: 170,
  },
  {
    text: '设备编号',
    value: 'deviceCode',
    align: 'center',
    // width: 100,
  },
  {
    text: '厂家',
    value: 'manufacturer',
    align: 'center',
  },
  {
    text: '是否供电',
    value: 'isPowerSupplyName',
    align: 'center',
  },
  {
    text: '批次',
    value: 'deviceBatch',
    align: 'center',
  },
  {
    text: '注册时间',
    value: 'createTime',
    align: 'center',
  },
  {
    text: '当前状态',
    value: 'statusName',
    align: 'center',
  },
])
const list = ref<device[]>([])
const total = ref(0)
const listLoading = ref(true)
// 获取设备列表数据
const fetchData = () => {
  listLoading.value = true
  getDeviceListPage(listQuery).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    listLoading.value = false
  })
}
fetchData()

// 查询数据
const search = () => {
  fetchData()
}

// 获取设备类型
const deviceList = ref<DictDetail[]>()
const fetchDeviceType = () => {
  getDictByCode('deviceType').then((response) => {
    deviceList.value = response.data
  })
}
fetchDeviceType()
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size: number; page: number }) => {
  if (val && val.size) {
    listQuery.limit = val.size
  }
  if (val && val.page) {
    listQuery.offset = val.page
  }
  fetchData()
}
// 表格被选中的行
const selectList = ref<device[]>([])
// 表格多选
const multiSelect = (row: device[]) => {
  selectList.value = row
}
const editRef = ref()
// 新增、
const add = () => {
  editRef.value.initDialog('create', {})
}
// 编辑
const edit = (row: device) => {
  editRef.value.initDialog('update', row)
}
// 删除
const del = () => {
  if (selectList.value.length) {
    ElMessageBox.confirm('是否删除所选的数据?', '提示', {
      confirmButtonText: '是',
      cancelButtonText: '否',
      type: 'info',
    }).then((res) => {
      // 点击确认
      // 判断所选设备状态
      if (selectList.value.every(item => item.status === '0')) {
        const ids = selectList.value.map(item => item.id)
        delDevice(ids).then((res) => {
          if (res.code === 200) {
            selectList.value = []
            ElMessage.success('删除成功')
            fetchData()
          }
        })
      }
      else {
        ElMessage.warning('所选设备正在使用中，请先解绑')
      }
    }).catch(() => {
      // 点击取消
      selectList.value = []
    })
  }
  else {
    ElMessage.warning('请先选择需要删除的数据')
  }
}
const show = ref(false)
const video = (row: device) => {
  // videoRef.value.initDialog()
  show.value = true
}
const close = () => {
  show.value = false
}
</script>

<template>
  <div class="list-log">
    <video-page v-if="show" ref="videoRef" @close="close" />
    <!-- 布局 -->
    <app-container>
      <!-- 人员编辑 -->
      <edit-list ref="editRef" @watch-child="search" />
      <!-- 筛选条件 -->
      <search-area @search="search">
        <search-item>
          <el-select v-model="listQuery.deviceType" clearable placeholder="设备类型">
            <el-option v-for="item in deviceList" :key="item.value" :label="item.name" :value="item.value" />
          </el-select>
        </search-item>
        <search-item>
          <el-input v-model.trim="listQuery.deviceCode" placeholder="设备编号" clearable />
        </search-item>
        <template #btns>
          <el-button v-if="proxy.hasPerm('/device/add')" class="search-btn" type="primary" @click="add">
            新增
          </el-button>
          <el-button v-if="proxy.hasPerm('/device/del')" class="search-btn" type="info" @click="del">
            删除
          </el-button>
        </template>
      </search-area>
      <table-container>
        <!-- 查询结果Table显示 -->
        <normal-table
          :data="list" :total="total" :columns="columns" :query="listQuery" :is-showmulti-select="true"
          :is-multi="true" :list-loading="listLoading" @change="changePage" @multi-select="multiSelect"
        >
          <template #columns>
            <el-table-column v-if="proxy.hasPerm('/device/update')" label="编辑" width="90" align="center">
              <template #default="scope">
                <el-button link type="primary" size="small" @click="edit((scope as any).row)">
                  编辑
                </el-button>
              </template>
            </el-table-column>
            <el-table-column v-if="proxy.hasPerm('/device/update')" label="实时监控" width="90" align="center">
              <template #default="scope">
                <el-button link type="primary" :disabled="scope.row.deviceCode !== 'HWIH060000001217'" size="small" @click="video((scope as any).row)">
                  查看
                </el-button>
              </template>
            </el-table-column>
          </template>
        </normal-table>
      </table-container>
    </app-container>
  </div>
</template>

