<template>
  <draggable
    v-model="attachmentList"
    animation="300"
    handle=".mover"
    item-key="id"
    @end="onDraggableDed"
  >
    <template #item="{ element, index }">
      <div class="info_item flex-row">
        <img class="avatar" :src="computedAvatar" alt="" />
        <div class="info_con flex-row cross-center flex1">
          <!-- 资料 -->
          <div
            class="info_con_wrap flex1"
            v-if="element.link && element.link.url"
          >
            <div class="material_info flex-row">
              <img :src="element.link.picurl" alt="" />
              <div class="material_info_des flex1">
                <p>{{ element.link.title }}</p>
                <p>{{ element.link.desc }}</p>
              </div>
            </div>
          </div>
          <!-- 图片 -->
          <div v-if="element.image" class="material_image flex1">
            <img :src="element.image.pic_url" alt="" />
          </div>
          <div class="info_con_opt flex-column main-center">
            <img
              @click.stop="onDelFile(index)"
              src="https://wlmvp-cos.weiling.cn/worktable-h5/icon/code_wel_del.png"
              alt=""
            />
            <img
              v-if="attachments.length > 1"
              class="line mover"
              src="https://wlmvp-cos.weiling.cn/worktable-h5/icon/code_wel_line.png"
              alt=""
            />
          </div>
        </div>
      </div>
    </template>
  </draggable>
</template>

<script lang="ts" setup>
import { ref, watchEffect, computed } from "vue";
import draggable from "vuedraggable";

type Props = {
  attachments: any;
  currentUser: any;
};
const props = withDefaults(defineProps<Props>(), {
  attachments: [],
  currentUser: {},
});
const emit = defineEmits<{
  (e: "onDelAttachment", val: number);
  (e: "onDraggableDed", val: any[]);
}>();
const computedAvatar = computed(() => {
  return (
    props.currentUser?.head_img_url ||
    "https://wlmvp-cos.weiling.cn/worktable-h5/icon/default_avatar.png"
  );
});

const attachmentList = ref([]);

// 删除附件
const onDelFile = (i: number) => {
  emit("onDelAttachment", i);
};
// 拖动
const onDraggableDed = () => {
  emit("onDraggableDed", attachmentList.value);
};
watchEffect(() => {
  attachmentList.value = [...props.attachments];
});
</script>

<style scoped lang="scss">
p {
  padding: 0;
  margin: 0;
}

.info_item {
  margin-bottom: 14px;

  &:last-child {
    margin-bottom: 0;
  }

  .avatar {
    width: 34px;
    height: 34px;
    border-radius: 4px;
    margin-right: 8px;
  }

  .info_con_wrap {
    background: #ffffff;
    border-radius: 10px;
    padding: 10px;
    margin-right: 10px;
    position: relative;

    &::before {
      content: "";
      position: absolute;
      width: 0;
      height: 0;
      border-top: 8px solid transparent;
      border-right: 10px solid #fff;
      border-bottom: 8px solid transparent;
      left: -5px;
      top: 8px;
    }
  }

  .info_con_opt {
    img {
      width: 18px;
      height: 18px;
    }

    img.line {
      margin-top: 20px;
    }
  }
}

.material_image {
  margin-right: 10px;

  img {
    min-width: 100px;
    height: 100px;
    object-fit: cover;
    border-radius: 6px;
  }
}

.material_info {
  img {
    width: 50px;
    height: 50px;
    border-radius: 6px;
    margin-right: 10px;
    flex-shrink: 0;
  }

  .material_info_des {
    color: #000000;

    p {
      word-break: break-all;
    }

    p:first-child {
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      /* autoprefixer: ignore next */
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
    }

    p:last-child {
      color: var(--wl-text-color-secondary-2);
      font-size: 12px;
      margin-top: 5px;
      white-space: nowrap;
      text-overflow: ellipsis;
      /* autoprefixer: ignore next */
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 1;
      overflow: hidden;
    }
  }
}

.flex-row,
.flex-column {
  display: flex;
}

.flex-row {
  flex-direction: row;
}

.flex1 {
  flex: 1;
}

.flex-row > .flex1 {
  width: 0;
  min-width: 0;
}

.flex-column > .flex1 {
  height: 0;
  min-height: 0;
}

.cross-center {
  align-items: center;
}

.flex-column {
  flex-direction: column;
}

.main-center {
  justify-content: center;
}

.flex-center {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
