<template>
  <view class="message-wrapper">
    <u-swipe-action
      :index="itemIndex"
      :show="item.show"
      :options="swipeOptions"
      @click="handleSwipeClick"
      @open="handleSwipeOpen"
      @close="handleSwipeClose"
      @content-click="closeSwipe"
      :btn-width="80"
      :disabled="false"
    >
      <view class="message-item">
        <view class="message-header">
          <view class="user-info">
            <image class="avatar" :src="item.avatar || defaultAvatar"></image>
            <text class="username">{{ item.username }}</text>
          </view>
          <view class="message-time">{{ item.time }}</view>
        </view>

        <view class="message-content">
          <view class="question">
            <view class="question-icon">问</view>
            <view class="question-text">{{ item.question }}</view>
          </view>

          <view class="reply-button" @click.stop="onReply" v-if="!item.reply">
            <u-icon name="chat" color="#4a6cf7" size="16"></u-icon>
            <text>回复</text>
          </view>

          <view class="reply-content" v-if="item.reply">
            <view class="reply-header">
              <view class="reply-icon">答</view>
              <view class="reply-info">
                <text>{{ item.replyUser }}</text>
              </view>
            </view>
            <view class="reply-text" :class="{ expanded: item.expanded }">{{
              item.reply
            }}</view>
            <view
              class="expand-button"
              v-if="item.reply.length > 100"
              @click.stop="toggleExpand"
            >
              {{ item.expanded ? "收起" : "展开" }}
              <u-icon
                :name="item.expanded ? 'arrow-up' : 'arrow-down'"
                size="12"
              ></u-icon>
            </view>
          </view>

          <view class="reply-button" @click.stop="onReply" v-if="item.reply">
            <u-icon name="chat" color="#4a6cf7" size="16"></u-icon>
            <text>回复</text>
          </view>
        </view>
      </view>
    </u-swipe-action>
  </view>
</template>

<script>
export default {
  name: "MessageItem",
  props: {
    item: {
      type: Object,
      required: true,
    },
    itemIndex: {
      type: Number,
      required: true,
    },
    defaultAvatar: {
      type: String,
      default: "/static/avatar/default-avatar.png",
    },
  },
  data() {
    return {
      swipeOptions: [
        {
          text: "删除",
          style: {
            backgroundColor: "#fa3534",
            color: "#ffffff",
            fontSize: "14px",
          },
        },
      ],
    };
  },
  methods: {
    handleSwipeClick(e) {
      const { index } = e; // 点击的按钮索引

      if (index === 0) {
        // 删除按钮
        this.$emit("delete", this.itemIndex);
      }
    },
    handleSwipeOpen(e) {
      // uView 1.x的事件参数格式与2.x不同
      this.$emit("open-swipe", this.itemIndex);
    },
    handleSwipeClose(e) {
      // uView 1.x的事件参数格式与2.x不同
      this.item.show = false;
    },
    closeSwipe() {
      this.item.show = false;
    },
    onReply() {
      this.$emit("reply", this.item);
    },
    toggleExpand() {
      this.$emit("toggle-expand", this.item);
    },
  },
};
</script>

<style scoped>
.message-wrapper {
  position: relative;
  margin-bottom: 10px;
}

.message-item {
  background-color: #fff;
  padding: 15px;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.message-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.user-info {
  display: flex;
  align-items: center;
}

.avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  margin-right: 8px;
  background-color: #eee;
}

.username {
  font-size: 12px;
  color: #666;
}

.message-time {
  font-size: 12px;
  color: #999;
}

.message-content {
  position: relative;
}

.question {
  display: flex;
  margin-bottom: 10px;
}

.question-icon {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background-color: #4a6cf7;
  color: #fff;
  font-size: 12px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-right: 8px;
  flex-shrink: 0;
}

.question-text {
  font-size: 14px;
  line-height: 1.5;
  flex: 1;
}

.reply-button {
  height: 32px;
  background-color: #f0f2fd;
  color: #4a6cf7;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  margin-top: 10px;
  cursor: pointer;
}

.reply-button text {
  margin-left: 5px;
}

.reply-content {
  background-color: #f8f8f8;
  border-radius: 4px;
  padding: 10px;
  margin-top: 10px;
}

.reply-header {
  display: flex;
  align-items: center;
  margin-bottom: 5px;
}

.reply-icon {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background-color: #ff9500;
  color: #fff;
  font-size: 12px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-right: 8px;
}

.reply-info {
  font-size: 12px;
  color: #666;
}

.reply-text {
  font-size: 14px;
  line-height: 1.5;
  color: #333;
  margin-left: 28px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

.reply-text.expanded {
  -webkit-line-clamp: unset;
}

.expand-button {
  text-align: right;
  font-size: 12px;
  color: #4a6cf7;
  margin-top: 5px;
  cursor: pointer;
}

/* 修改删除按钮样式 */
::v-deep .u-btn-text {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
}

::v-deep .u-btn-text::before {
  content: "";
  display: block;
  width: 20px;
  height: 20px;
  background-image: url("/static/common/img/trash.png");
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  margin-bottom: 5px;
  z-index: 1;
}
</style>
