<template>
  <el-drawer
    :title="t('route.InspectionRoute')"
    :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>
      <draggable
        v-model="items"
        item-key="id"
        tag="div"
        class="sortable-container"
        :animation="150"
        draggable="false"
      >
        <template #item="{ element, index }">
          <div class="sortable-item">
            <!-- 序号显示 -->
            <div class="order-number">{{ index + 1 }}</div>

            <!-- 拖动手柄 -->
            <span class="drag-handle">≡</span>

            <!-- 组件内容 -->
            <div class="component-content">
              <span style="font-weight: bold">{{ t('inspect.InspectionItems') }}：</span
              ><span style="font-size: 14px">{{ element.item }}</span
              >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="font-weight: bold">{{ t('inspect.InspectionStandards') }}：</span
              ><span style="font-size: 14px">{{ element.standard }}</span><br/>
              <div v-if="element.ifNormal!=undefined">
                <span style="font-weight: bold;color: #2bb673">{{ t('inspect.isException') }}：</span><span style="font-size: 14px">{{ element.ifNormal?"正常":"异常" }}</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <span style="font-weight: bold;color: #2bb673">{{ t('inspect.exceptionDes') }}：</span
              ><span style="font-size: 14px">{{ element.description }}</span><br/>
                <span style="font-weight: bold;color: #2bb673" v-if="element.picUrl">{{ t('deviceForm.picture') }}：<span style="font-size: 14px;vertical-align: middle"><Icon style="color: deepskyblue" icon="ep:view" @click="openWeb(element.picUrl)" /></span></span>
                <span style="font-weight: bold;color: #2bb673">{{ t('inspect.inspectTime') }}：</span><span style="font-size: 14px">{{element.updateTime? dayjs(element.updateTime).format(format ?? 'YYYY-MM-DD HH:mm:ss') : ''}}</span>
              </div>
            </div>
          </div>
        </template>
      </draggable>
    </ContentWrap>
  </el-drawer>
</template>
<script setup lang="ts">
import { defineEmits, defineOptions, ref } from 'vue'
import draggable from 'vuedraggable'
import dayjs from 'dayjs'
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 ? '50%' : '60%'
})

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

const props = defineProps({
  modelValue: Boolean
})
const openWeb = (url) => {
  window.open(
    'http://1.94.244.160:8012/onlinePreview?url=' + encodeURIComponent(Base64.encode(url))
  )
}
// 打开抽屉
const openDrawer = (itemJson) => {
  drawerVisible.value = true
  items.value = itemJson
}

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

// 关闭抽屉
const handleClose = () => {
  emit('update:modelValue', false)
}
const drawerRef = ref()
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>
