<template>
  <div class="device-status-record">
    <section class="library-workbench">
      <div class="library-workbench__header">
        <div class="library-header-main">
          <div class="workbench-title">
            <span class="workbench-title__icon">
              <Icon icon="ep:refresh" />
            </span>
            <div>
              <h3>状态变更记录</h3>
            </div>
          </div>
        </div>
      </div>

      <div class="library-table-wrap">
        <el-auto-resizer>
          <template #default="{ width, height }">
            <ZmTable
              :data="list"
              class="library-table"
              :loading="loading"
              :width="width"
              :height="height"
              :max-height="height"
              :column-max-width="420"
              row-key="id"
              empty-text="暂无数据"
              show-border>
              <ZmTableColumn
                label="设备名称"
                prop="deviceName"
                min-width="180"
                align="left"
                fixed="left">
                <template #default="{ row }">
                  <span class="device-name">{{ row.deviceName || '-' }}</span>
                </template>
              </ZmTableColumn>
              <ZmTableColumn label="设备编码" prop="deviceCode" min-width="150">
                <template #default="{ row }">
                  {{ row.deviceCode || '-' }}
                </template>
              </ZmTableColumn>
              <ZmTableColumn label="变更前状态" prop="oldStatus" min-width="140">
                <template #default="{ row }">
                  <dict-tag :type="DICT_TYPE.PMS_DEVICE_STATUS" :value="row.oldStatus" />
                </template>
              </ZmTableColumn>
              <ZmTableColumn label="变更后状态" prop="newStatus" min-width="140">
                <template #default="{ row }">
                  <dict-tag :type="DICT_TYPE.PMS_DEVICE_STATUS" :value="row.newStatus" />
                </template>
              </ZmTableColumn>
              <ZmTableColumn label="变更原因" prop="reason" min-width="220" align="left">
                <template #default="{ row }">
                  {{ row.reason || '-' }}
                </template>
              </ZmTableColumn>
              <ZmTableColumn label="调整人" prop="creatorName" min-width="140">
                <template #default="{ row }">
                  {{ row.creatorName || '-' }}
                </template>
              </ZmTableColumn>
              <ZmTableColumn label="调整时间" prop="createTime" min-width="180">
                <template #default="{ row }">
                  {{ formatRecordTime(row.createTime) }}
                </template>
              </ZmTableColumn>
            </ZmTable>
          </template>
        </el-auto-resizer>
      </div>

      <div class="library-pagination">
        <Pagination
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          :total="total"
          @pagination="getList" />
      </div>
    </section>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from 'vue'
import { IotDeviceStatusLogApi, IotDeviceStatusLogVO } from '@/api/pms/iotdevicestatuslog'
import { useTableComponents } from '@/components/ZmTable/useTableComponents'
import { DICT_TYPE } from '@/utils/dict'
import { formatDate } from '@/utils/formatTime'

defineOptions({ name: 'DeviceStatusRecord' })

const props = defineProps<{
  deviceId?: number
}>()

type StatusRecordRow = IotDeviceStatusLogVO & {
  createTime?: Date | string | number
  creatorName?: string
  deviceCode?: string
  deviceName?: string
}

const { ZmTable, ZmTableColumn } = useTableComponents<StatusRecordRow>()
const loading = ref(false)
const list = ref<StatusRecordRow[]>([])
const total = ref(0)

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  deviceId: undefined as number | undefined
})

const getList = async () => {
  if (!props.deviceId) return

  loading.value = true
  queryParams.deviceId = props.deviceId
  try {
    const data = await IotDeviceStatusLogApi.getIotDeviceStatusLogPage(queryParams)
    list.value = data?.list || []
    total.value = data?.total || 0
  } finally {
    loading.value = false
  }
}

const formatRecordTime = (value?: Date | string | number) => {
  return value ? formatDate(value as Date) : '-'
}

watch(
  () => props.deviceId,
  () => {
    getList()
  },
  { immediate: true }
)
</script>

<style scoped lang="scss">
.device-status-record {
  display: grid;
  gap: 14px;
}

.library-workbench {
  padding: 14px;
  overflow: hidden;
  background: linear-gradient(180deg, #f8fbff 0, var(--el-bg-color) 96px), var(--el-bg-color);
  border: 1px solid var(--el-border-color-lighter);
  border-radius: 8px;
}

.library-workbench__header {
  display: flex;
  gap: 16px;
  min-height: 56px;
  margin-bottom: 14px;
  align-items: center;
  justify-content: space-between;
}

.library-header-main {
  display: flex;
  gap: 10px;
  min-width: 0;
  align-items: center;
  flex: 1;
  flex-wrap: wrap;
}

.workbench-title {
  display: flex;
  gap: 12px;
  min-width: 0;
  align-items: center;

  h3 {
    margin: 0;
    font-size: 16px;
    font-weight: 700;
    line-height: 24px;
    color: var(--el-text-color-primary);
  }
}

.workbench-title__icon {
  display: flex;
  width: 38px;
  height: 38px;
  font-size: 18px;
  color: #3478f6;
  background: #eaf2ff;
  border-radius: 10px;
  flex: none;
  align-items: center;
  justify-content: center;
}

.library-table-wrap {
  height: 560px;
  min-width: 0;
}

.library-table {
  --el-table-header-bg-color: var(--el-fill-color-lighter);
  --el-table-row-hover-bg-color: #f6faff;

  overflow: hidden;
  border-radius: 8px;

  :deep(.el-table__cell) {
    padding: 9px 0;
  }
}

.device-name {
  display: inline-block;
  max-width: 100%;
  overflow: hidden;
  font-weight: 500;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.library-pagination {
  display: flex;
  height: 40px;
  margin-top: 8px;
  align-items: center;
  justify-content: flex-end;
}

@media (width <= 767px) {
  .library-workbench__header {
    align-items: stretch;
    flex-direction: column;
  }
}
</style>
