<template>
  <view class="favorites-page">

    <!-- 列表 -->
    <view class="fav-list">

      <uv-swipe-action>
        <uv-swipe-action-item v-for="(item, idx) in list" :key="idx" :options="[{
          text: '取消收藏',
          style: {
            backgroundColor: '#f56c6c'
          }
        }]" @click="handleOption('cancelCollection', idx)">
          <view class="fav-card">
            <image @click="goPage('/pages/crowdFunding/crowdfundingDetails?id=' + item.id)" :src="item.main_image"
              class="fav-img"></image>

            <view class="fav-content">
              <view class="fav-header">
                <image :src="item.creator_avatar" class="avatar"></image>
                <text class="nickname" @click="goPage('/pages/crowdFunding/crowdfundingDetails?id=' + item.id)">{{
                  item.creator_username }}</text>
                <view class="tag" @click="goPage('/pages/crowdFunding/crowdfundingDetails?id=' + item.id)">
                  <image src="@/static/crowdFunding/authentication.png" mode="widthFix"></image> 发起人
                </view>
                <view style="position: relative;left: 0;top: 0;">
                  <!-- <image src="/static/crowdFunding/more.png" class="more-img" @click="toggleDropdown(idx)"></image>
                  <view class="dropdown-menu" v-if="currentDropdownIndex === idx">
                    <view class="dropdown-item" @tap="handleOption('cancelCollection', idx)">取消收藏</view>
                  </view> -->
                </view>
              </view>
              <view class="fav-title two-omit" @click="goPage('/pages/crowdFunding/crowdfundingDetails?id=' + item.id)">
                {{ item.title }}</view>
              <view class="fav-bottom">
                <text class="amount">已筹 ￥{{ item.current_amount }}</text>
                <!-- <text class="favorite-time">收藏于{{ item.favorite_time }}</text> -->
              </view>
            </view>
          </view>
        </uv-swipe-action-item>
      </uv-swipe-action>
    </view>
    <view v-if="!isLoading && list.length === 0" class="no-data">
      <text>暂无数据</text>
    </view>


  </view>
</template>

<script>

export default {
  data() {
    return {
      list: [],
      page: 1,
      pageSize: 20,
      total: 0,
      isLoading: false,
      hasMore: true,
      currentDropdownIndex: null,
      show1: false,
    };
  },
  onLoad() {
    this.page = 1;
    this.list = [];
    this.hasMore = true;
    this.getList();
  },
  onPullDownRefresh() {
    this.page = 1;
    this.list = [];
    this.hasMore = true;
    this.getList(() => uni.stopPullDownRefresh());
  },
  onReachBottom() {
    if (this.hasMore && !this.isLoading) {
      this.getList();
    }
  },
  methods: {
    getList(cb) {
      if (this.isLoading) return;
      this.isLoading = true;
      uni.request({
        url: this.$apiHost + '/crowdfund/favorites',
        method: 'GET',
        data: {
          page: this.page,
          pageSize: this.pageSize,
          uuid: getApp().globalData.uuid,
          skey: getApp().globalData.skey,
        },
        success: (res) => {
          if (res.data && res.data.success === 'yes' && res.data.data) {
            var { list, total } = res.data.data;
            if (!Array.isArray(list)) {
              list = [];
            }
            if (this.page === 1) {
              this.list = list;
            } else {
              this.list = [...this.list, ...list];
            }
            this.total = total;
            this.hasMore = this.list && this.list.length < total;
            if (this.hasMore) this.page++;
          }
        },
        complete: () => {
          this.isLoading = false;
          cb && cb();
        }
      });
    },
    goBack() {
      uni.navigateBack();
    },
    toggleDropdown(idx) {
      this.currentDropdownIndex = this.currentDropdownIndex === idx ? null : idx;

    },
    goPage(url) {
      console.log(url, 888);

      uni.navigateTo({
        url,
      });
    },
    // 处理下拉菜单选项点击
    handleOption(type, idx) {

      this.currentDropdownIndex = null;
      if (type === "cancelCollection") {
        const item = this.list[idx];
        uni.request({
          url: this.$apiHost + '/crowdfund/favorite',
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          data: {
            crowdfund_id: item.id,
            action: 'remove',
            uuid: getApp().globalData.uuid,
            skey: getApp().globalData.skey,
          },
          success: (res) => {
            if (res.data && res.data.success && res.data.success === 'yes') {
              this.list.splice(idx, 1);
              uni.showToast({
                title: '取消收藏成功',
                icon: 'none',
                duration: 2000
              });
              this.show1 = !this.show1;
            } else {
              uni.showToast({
                title: res.data.msg || '取消失败',
                icon: 'none',
                duration: 2000
              });
            }
          },
          fail: () => {
            uni.showToast({
              title: '网络错误',
              icon: 'none',
              duration: 2000
            });
          }
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.favorites-page {
  min-height: 100vh;
  background: #f2f6f2;

  .fav-list {
    padding: 24rpx 0;

    .fav-card {
      display: flex;
      background: #fff;
      // border-radius: 18rpx; 
      box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
      padding: 20rpx;

      .fav-img {
        width: 140rpx;
        height: 140rpx;
        border-radius: 12rpx;
        object-fit: cover;
        flex-shrink: 0;
      }

      .fav-content {
        flex: 1;
        margin-left: 20rpx;
        display: flex;
        flex-direction: column;
        justify-content: space-between;

        .fav-header {
          display: flex;
          align-items: center;

          .avatar {
            width: 44rpx;
            height: 44rpx;
            border-radius: 50%;
            margin-right: 12rpx;
          }

          .nickname {
            font-weight: 400;
            font-size: 24rpx;
            color: #1f1f1f;
            font-family: "PingFang SC-Bold";
            max-width: 250rpx;
            margin-right: 12rpx;
          }

          .tag {
            font-size: 20rpx;
            color: #acf934;
            background: #1f1f1f;
            border-radius: 8rpx;
            padding: 2rpx 12rpx;
            margin-right: auto;
            margin-left: 4rpx;
            display: inline-flex;
            align-items: center;

            image {
              width: 28rpx;
              height: 28rpx;
            }
          }

          .more-img {
            width: 36rpx;
            height: 36rpx;
            margin-left: 12rpx;
          }
        }

        .fav-title {
          font-size: 26rpx;
          color: #222;
          font-weight: 500;
          margin: 10rpx 0 0 0;
          overflow: hidden;
          text-overflow: ellipsis;
          width: 100%;
        }

        .fav-bottom {
          margin-top: 10rpx;
          display: flex;
          justify-content: flex-end;
          align-items: center;

          .amount {
            font-size: 22rpx;
            color: #bdbdbd;
          }

          .favorite-time {
            font-size: 22rpx;
            color: #bdbdbd;
            margin-left: 10rpx;
          }
        }
      }
    }
  }

  .dropdown-menu {
    position: absolute;
    top: calc(100% + 10rpx);
    right: 20rpx;
    background-color: #ffffff;
    border-radius: 20rpx;
    padding: 0;
    width: 150rpx;
    box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
    z-index: 100;
    transform-origin: top right;
    animation: dropdownAnimation 0.2s ease-out;
    overflow: hidden;

    .dropdown-item {
      padding: 15rpx 0;
      color: #333333;
      font-size: 24rpx;
      position: relative;
      text-align: center;

      &:not(:last-child)::after {
        content: "";
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        height: 1rpx;
        background-color: #eeeeee;
      }

      &:active {
        background-color: #f8f8f8;
      }
    }
  }

  @keyframes dropdownAnimation {
    0% {
      opacity: 0;
      transform: scale(0.95) translateY(-5rpx);
    }

    100% {
      opacity: 1;
      transform: scale(1) translateY(0);
    }
  }

  .no-data {
    text-align: center;
    color: #bbb;
    font-size: 28rpx;
    padding: 80rpx 0 40rpx 0;
  }
}

::v-deep .uv-swipe-action {
  margin: 0 24rpx 24rpx 24rpx;
  border-radius: 18rpx;
  overflow: hidden;

  .uv-swipe-action-item__content {
    background-color: #f2f6f2;
  }

  .uv-swipe-action-item__right {
    border-top-right-radius: 32rpx;
    border-bottom-right-radius: 32rpx;
    top: 2rpx;
    bottom: 2rpx;
    right: 2rpx;
  }
}

.uv-swipe-action-item {
  border-radius: 18rpx;
  overflow: hidden;
  margin-bottom: 24rpx;
}
</style>
