<template>
  <el-drawer
    :title="t('configDevice.adjustmentRecords')"
    :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="devicePersons" style="width: 100%">
          <el-table-column prop="deviceName" :label="t('deviceStatus.deviceName')" width="180" />
          <el-table-column prop="deviceCode" :label="t('deviceStatus.deviceCode')" width="180" />
          <el-table-column prop="oldPersonNames" :label="t('deviceStatus.beforePerson')" width="180" />
          <el-table-column prop="newPersonNames" :label="t('deviceStatus.afterPerson')" width="180" />
          <el-table-column prop="reason" :label="t('configDevice.reasonForAdjustment')" width="180" />
          <el-table-column prop="creatorName"  :label="t('deviceStatus.adjuster')" width="180" />
          <el-table-column
            :label="t('deviceStatus.adjustTime')"
            align="center"
            prop="createTime"
            width="180"
            :formatter="dateFormatter"
          />
        </el-table>
        <!-- 分页 -->
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="handlePagination"
        />
      </div>
    </template>

  </el-drawer>
</template>
<script setup lang="ts">
const { t } = useI18n() // 国际化
import { ref, watch, defineOptions, defineEmits, toRefs } from 'vue'
import { ElMessage } from 'element-plus'
import {dateFormatter} from "@/utils/formatTime";
import {DICT_TYPE} from "@/utils/dict";
import {IotDevicePersonLogApi} from "@/api/pms/iotdevicepersonlog";
const drawerVisible = ref<boolean>(false)
const emit = defineEmits(['update:modelValue', 'add', 'delete'])

defineOptions({
  name: 'DevicePersonLogDrawer'
})

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 devicePersons = ref([])

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

// 使用 toRefs 解构响应式属性
const { modelValue, deviceId } = toRefs(props)

// 监听抽屉打开事件
watch(modelValue, (isOpen) => {
  if (isOpen && deviceId.value) {
    loadDevicePersons(deviceId.value)
  }
})

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

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

// 修改分页处理
const handlePagination = (pagination: any) => {
  queryParams.pageNo = pagination.page
  queryParams.pageSize = pagination.limit
  if (props.deviceId) {
    loadDevicePersons(props.deviceId)
  }
}

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

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

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

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

</script>

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