<template>
  <el-drawer
    :title="t('maintain.record')"
    :append-to-body="true"
    :model-value="modelValue"
    @update:model-value="$emit('update:modelValue', $event)"
    :show-close="false"
    direction="rtl"
    :size="computedSize"
    :before-close="handleClose"
  >
    <ContentWrap>
      <div class="block">
        <el-timeline>
          <!-- 循环渲染时间线项 -->
          <el-timeline-item
            v-for="(item, index) in timelineData"
            :key="index"
            :size="'large'"
            :timestamp="dayjs(item.repairTime).format('YYYY-MM-DD')"
            color="#0bbd87"
            placement="top"
          >
            <el-card>
              <h4>{{ item.repairItem }}</h4>
              <p
                >{{ item.createName }} 提交于
                {{ dayjs(item.createTime).format('YYYY-MM-DD HH:mm:ss') }}</p
              >
            </el-card>
          </el-timeline-item>
        </el-timeline>
      </div>
    </ContentWrap>
  </el-drawer>
</template>
<script setup lang="ts">
import { defineEmits, defineOptions, ref } from 'vue'
import dayjs from 'dayjs'
import { IotJdRecordApi } from '@/api/pms/maintain/record'

const { t } = useI18n() // 国际化
const drawerVisible = ref<boolean>(false)
const emit = defineEmits(['update:modelValue', 'add'])
const items = ref([])
defineOptions({
  name: 'RouteInspectItemDrawer'
})
const windowWidth = ref(window.innerWidth)
// 动态计算百分比
const computedSize = computed(() => {
  return windowWidth.value > 800 ? '30%' : '40%'
})

const timelineData = ref([])
// 打开抽屉
const openDrawer = async (id) => {
  drawerVisible.value = true
  timelineData.value = await IotJdRecordApi.getIotJdRecordByRepairId(id)
}

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

// 关闭抽屉
const handleClose = () => {
  emit('update:modelValue', false)
}
defineExpose({ openDrawer, closeDrawer }) // 暴露方法给父组件
</script>

<style lang="scss" scoped>
.sortable-container {
  display: flex;
  flex-direction: column;
  gap: 12px;
  //max-height: 80vh;
  overflow-y: auto;
  padding: 8px;
}

.sortable-item {
  display: flex;
  align-items: center;
  padding: 16px;
  background: #fff;
  border: 1px solid #ebeef5;
  border-radius: 8px;
  transition:
    transform 0.3s,
    box-shadow 0.3s;
  user-select: none;
}

.sortable-item:hover {
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.order-number {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #409eff;
  color: white;
  border-radius: 50%;
  margin-right: 12px;
  font-weight: bold;
}

.drag-handle {
  padding: 0 12px;
  opacity: 0.4;
  transition: opacity 0.3s;
}

.drag-handle:hover {
  opacity: 1;
}

.component-content {
  flex: 1;
  min-width: 0;
}

/* 优化滚动条 */
.sortable-container::-webkit-scrollbar {
  width: 8px;
}
.sortable-container::-webkit-scrollbar-thumb {
  background: #c0c4cc;
  border-radius: 4px;
}
</style>
