<template>
  <el-drawer
    title="设备状态调整记录"
    :append-to-body="true"
    :model-value="modelValue"
    @update:model-value="$emit('update:modelValue', $event)"
    :show-close="false"
    direction="rtl"
    :size="computedSize"
    :before-close="handleClose"
  >
    <template v-if="deviceId">
      <div v-loading="loading" style="height: 100%">
        <el-table :data="deviceStatuses" style="width: 100%">
          <el-table-column prop="deviceName" label="设备名称" width="180" />
          <el-table-column prop="deviceCode" label="设备编码" width="180" />
          <el-table-column prop="oldStatus" label="调整前状态" width="180" >
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.PMS_DEVICE_STATUS" :value="scope.row.oldStatus" />
            </template>
          </el-table-column>
          <el-table-column prop="newStatus" label="调用后状态" width="180" >
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.PMS_DEVICE_STATUS" :value="scope.row.newStatus" />
            </template>
          </el-table-column>
          <el-table-column prop="reason" label="调整原因" width="180" />
          <el-table-column prop="creatorName" label="调整人" width="180" />
          <el-table-column
            label="调整时间"
            align="center"
            prop="createTime"
            width="180"
            :formatter="dateFormatter"
          />
        </el-table>
        <!-- 分页 -->
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="loadDeviceStatuses(props.deviceId)"
        />
      </div>
    </template>

  </el-drawer>
</template>
<script setup lang="ts">

import { ref, watch, defineOptions, defineEmits } from 'vue'
import { ElMessage } from 'element-plus'
import * as IotDeviceStatusLogApi from '@/api/pms/iotdevicestatuslog'
import {dateFormatter} from "@/utils/formatTime";
import {DICT_TYPE} from "@/utils/dict";
const drawerVisible = ref<boolean>(false)
const emit = defineEmits(['update:modelValue', 'add', 'delete'])

defineOptions({
  name: 'DeviceStatusLogDrawer'
})

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  createTime: [],
  deviceId: '',
  name: '',
  code: ''
})

const windowWidth = ref(window.innerWidth)
// 动态计算百分比
const computedSize = computed(() => {
  return windowWidth.value > 1200 ? '60%' : '80%'
})

const loading = ref(false)
const total = ref(0) // 列表的总页数
const deviceStatuses = ref([])

const props = defineProps({
  modelValue: Boolean,
  deviceId: Number
})

// 监听bom树节点ID变化
watch(() => props.deviceId, async (newVal) => {
  if (newVal) {
    await loadDeviceStatuses(newVal)
  }
})

// 加载设备的状态调整记录
const loadDeviceStatuses = async (deviceId) => {
  queryParams.deviceId = deviceId
  queryParams.pageNo = 1
  try {
    loading.value = true
    // API调用
    const data = await IotDeviceStatusLogApi.IotDeviceStatusLogApi.getIotDeviceStatusLogPage(queryParams)
    deviceStatuses.value = data.list
    total.value = data.total
  } catch (error) {
    ElMessage.error('数据加载失败')
  } finally {
    loading.value = false
  }
}

// 打开抽屉
const openDrawer = () => {
  drawerVisible.value = true
}

// 关闭抽屉
const closeDrawer = () => {
  drawerVisible.value = false
}

// 关闭抽屉
const handleClose = () => {
  emit('update:modelValue', false)
  deviceStatuses.value = []
}

defineExpose({ openDrawer, closeDrawer, loadDeviceStatuses }) // 暴露方法给父组件

</script>

<style lang="scss" scoped></style>
