<template>
  <view class="page">
    <view class="topBody">
      <view class="header">
        <view class="card-box">
          <view class="card-top">
            <view class="top-box">
              <view class="hello-box" @click="goBack">
                <text class="fa fa-angle-left" style="color: #000; font-size: 55rpx;"></text>
              </view>
              <view class="settingBtn-box">
                <image @click="clickShare()" src="@/static/me/wd_icon_fenxian.png" mode=""></image>
              </view>
            </view>
            <view class="userinfo-box">
              <view class="userinfo-left">
                <CircleAvatar class="avator" :src="myinfo.avator"></CircleAvatar>
              </view>
              <view class="userinfo-right">
                <view class="nickname">
                  <text class="one-omit">{{ myinfo.nickname }}</text>
                  <image src="../../static/icon/wd_icon_nan.png" mode="widthFix" v-if="myinfo.sex_id == 1"></image>
                  <image src="../../static/icon/wd_icon_nv.png" mode="widthFix" v-else-if="myinfo.sex_id == 2"></image>
                  <view class="level">Lv{{ myinfo.my_level }}</view>
                </view>
                <view class="label">
                  <view v-for="(item, index) in aihao_tags" :key="index + item">
                    {{ item }}
                  </view>
                </view>
              </view>
            </view>
            <view class="intro_row">
              <block v-if="myinfo.content == ''">
                <text class="intro_text two-omit">简介</text>
              </block>
              <uv-text v-else class="intro_text two-omit">
                {{ myinfo.content }}
              </uv-text>
            </view>
            <view class="bom">
              <view class="follow_info">
                <view class="follow-box">
                  <view class="num">{{ myinfo.num_attention }}</view>
                  <view class="label">关注</view>
                </view>
                <view class="separator"></view>
                <view class="follow-box">
                  <view class="num">{{ myinfo.num_fans }}</view>
                  <view class="label">粉丝</view>
                </view>
                <view class="separator"></view>
                <view class="follow-box">
                  <view class="num">{{ myinfo.num_like }}</view>
                  <view class="label">获赞</view>
                </view>
              </view>
              <view class="points-box">
                <text class="followTheAuthor followTheAuthor1" v-if="!myinfo.is_attention" @click="followTheAuthor(1)">+关注</text>
                <text class="followTheAuthor followTheAuthor0" v-else @click="followTheAuthor(0)">已关注</text>
              </view>
            </view>
          </view>
        </view>
      </view>

      <view class="myinfo">
        <!-- 作品列表 -->
        <view class="numlist1"   style="margin-top: 30rpx">
          <WorkItem v-for="(item, index) in worksList" :subtitle="true" :key="index" :item="item"
            @click="goWork(item)" />
        </view>
      </view>
    </view>

    <!-- 确认框 -->
    <CustomConfirm ref="customConfirm"></CustomConfirm>
    <!-- 提示框 -->
    <DialogBox ref="DialogBox"></DialogBox>

    <!-- SharePopup组件 -->
    <SharePopup :visible="showShare" :share-url="shareUrl" :share-title="shareTitle" :share-desc="shareDesc"
      :share-img="shareImg" @close="showShare = false" />
  </view>
</template>

<script>
import tabbar from "@/mixins/tabbar";
import CustomConfirm from "@/components/custome-confirm/customeConfirm.vue";
import CircleAvatar from "@/components/CircleAvatar/CircleAvatar.vue";
import WorkItem from "@/components/WorkItem/WorkItem.vue";
import SharePopup from "@/components/SharePopup/SharePopup.vue";
import DialogBox from "@/components/DialogBox/DialogBox.vue";

export default {
  components: {
    CustomConfirm,
    CircleAvatar,
    WorkItem,
    SharePopup,
    DialogBox
  },
  mixins: [tabbar],
  data() {
    return {
      firstLevelNavActive: 0,
      myinfo: {
        avator: "../../static/logo.png",
        nickname: "王思思",
        content: "",
        sex_id: 1,
        my_level: 1,
        num_attention: 0,
        num_fans: 0,
        num_like: 0,
        wxkf: ""
      },
      aihao_tags: [],
      activeTab: 0,
      offset: 0,
      hasMore: true,
      isLoading: false,
      worksList: [],
      showShare: false,
      shareUrl: "https://your-share-url.com",
      shareTitle: "分享标题",
      shareDesc: "分享描述",
      shareImg: "https://your-share-image.com/image.jpg",
      id: 0,
    };
  },
  onLoad(e) {
    if (e.id) {
      this.id = e.id
    }
  },
  onShow() {
    uni.$emit("check_login", () => { });
    this.offset = 0;
    this.hasMore = true;
    this.worksList = [];
    this.loadInfo();
    this.loadWorksList();
  },
  onReachBottom() {
    if (this.hasMore && !this.isLoading) {
      this.loadMoreWorks();
    }
  },
  methods: {
    // 关注作者
    followTheAuthor(n) { 
      uni.request({
        url: this.$apiHost + "/Member/attention",
        data: {
          uuid: getApp().globalData.uuid,
          id: this.id,
        },
        header: {
          "content-type": "application/json",
          sign: getApp().globalData.headerSign,
        },
        success: (res) => {
          console.log("点赞结果:", res.data);
          uni.showToast({
            title: res.data.str,
            icon: "none",
          });
          this.loadInfo();
        },
        fail: (e) => {
          console.log("关注失败:", e);
          uni.showToast({
            title: "网络请求失败",
            icon: "none",
          });
        },
      });
    },
    goBack() {
      uni.navigateBack({
        delta: 1,
      });
    },
    clickShare(item) {
      this.showShare = true;
    },
    goPage(page) {
      if (page == "kefu") {
        let that = this;
        // #ifdef APP-PLUS
        plus.share.getServices((res) => {
          const wechat = res.find((i) => i.id === "weixin");
          if (wechat) {
            wechat.openCustomerServiceChat(
              {
                corpid: "wwbc06aa8311b6ac08",
                url: that.myinfo.wxkf,
              },
              (src) => {
                console.log("success:");
              },
              (err) => {
                console.log("error:");
              }
            );
          } else {
            uni.showToast({
              title: "没有检测到微信，请先安装",
              icon: "error",
            });
          }
        });
        // #endif
      } else if (page != "") {
        uni.navigateTo({
          url: page,
        });
      }
    },
    loadInfo() {
      console.log({
        uuid: getApp().globalData.uuid,
        skey: getApp().globalData.skey,
      });
      uni.request({
        url: this.$apiHost + "/Member/getHomeInfo",
       
        data: {
          uuid: getApp().globalData.uuid,
          skey: getApp().globalData.skey,
          user_id: this.id,
        },
        header: {
          "content-type": "application/json",
          sign: getApp().globalData.headerSign,
        },
        success: (res) => {
          console.log("----:", JSON.parse(JSON.stringify(res.data)));
          if (res.data.need_login == "yes") {
            uni.removeStorageSync("wapptoken");
            uni.redirectTo({
              url: "/pages/login/login",
            });
            return;
          }
          if (res.data.aihao) {
            this.aihao_tags = res.data.aihao.split(",");
          }
          this.myinfo = res.data;
        },
        complete: (com) => {
          // uni.hideLoading();
        },
        fail: (e) => {
          console.log("----e:", e);
        },
      });
    },
    switchTab(index) {
      this.activeTab = index;
      this.offset = 0;
      this.hasMore = true;
      this.worksList = [];
      this.loadWorksList();
    },
    loadWorksList() {
      if (this.isLoading) return;
      this.isLoading = true;

      // 根据activeTab选择不同的API
      
      uni.request({
        url: this.$apiHost + '/Work/getlist',
        data: {
          uuid: getApp().globalData.uuid,
          skey: getApp().globalData.skey,
          type: "other", // 固定为my，表示获取自己的作品
          offset: this.offset,
          user_id:this.id
        },
        header: {
          "content-type": "application/json",
          sign: getApp().globalData.headerSign,
        },
        success: (res) => {
          console.log("列表数据:", JSON.parse(JSON.stringify(res.data)));
          if (res.data.success == "yes" && res.data.list) {
            if (res.data.list.length > 0) {
              this.worksList = [...this.worksList, ...res.data.list];
              this.offset += res.data.list.length;
            }

            if (res.data.list.length < 20) {
              this.hasMore = false;
            }
          } else {
            this.hasMore = false;
          }
 
         
        },
        complete: () => {
          this.isLoading = false;
        },
        fail: (e) => {
          console.log("请求列表失败:", e);
          this.isLoading = false;
        },
      });
    },
    firstLevelNavActiveSwitch(n) {
      this.firstLevelNavActive = n;
      this.offset = 0;
      this.hasMore = true;
      this.worksList = [];
      if (this.firstLevelNavActive == 0) {
        this.activeTab = 0;
      }
      this.loadWorksList();
    },

    loadMoreWorks() {
      if (this.hasMore && !this.isLoading) {
        this.loadWorksList();
      }
    },
    updateDataList() {
      this.data_list = this.worksList.map((item) => {
        return {
          url:
            item.images || item.img_url || item.url || "../../static/logo.png",
          title: item.title || item.description || "作品",
          id: item.id,
        };
      });
    },
    goWork(item) {
      uni.$emit("check_login", () => { });
      
      uni.navigateTo({
            url: "/pages/index/workDetail?id=" + item.id,
          });
    },
    navigateToFollow() {
      uni.navigateTo({
        url: "/pages/my/follow",
      });
    },
  },
};
</script>

<style scoped lang="scss">
// 导入FontAwesome
@import url("https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css");

page {
  background-color: #fff;
  padding-top: var(--status-bar-height);
  padding-bottom: 144rpx;
}

.page {
  background-color: #fff;
  width: 100%;
  min-height: 100vh;
}

.topBody {
  width: 750rpx;
}

.header {
  padding: 20rpx;
  padding-top: 48rpx;
  background: linear-gradient(225deg, #cdff9f 0%, #acff5f 30%, #d0ffa5 100%);
  min-height: 720rpx;
  margin-bottom: -210rpx;

  .card-box {
    width: 100%;
    min-height: 440rpx;
    position: relative;
    left: 0;
    top: 0%;
    overflow: hidden;

    .card-top {
      height: 435rpx;
      width: 100%;
      background: url("../../static/me/my-card-bg.png") top center / 100% auto,
        #fff;
      position: absolute;
      top: 0%;
      left: 0;
      z-index: 5;
      padding: 24rpx;
      padding-top: 16rpx;
      box-sizing: border-box;
      border-radius: 25rpx;

      .top-box {
        display: flex;
        justify-content: space-between;
        padding-top: 8rpx;
        background: url("../../static/me/car-top-bg-center.png") top center/ 146rpx 50rpx no-repeat;

        .hello-box {
          font-family: "CustomFont" !important;
          font-size: 36rpx;
          font-weight: 700;
        }

        .settingBtn-box {
          width: 74rpx;
          display: flex;
          align-items: center;
          justify-content: space-between;

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

      .userinfo-box {
        min-height: 120rpx;
        width: 100%;
        display: flex;

        .avator {
          width: 120rpx;
          height: 120rpx;
          margin-right: 16rpx;
        }

        .userinfo-right {
          .nickname {
            font-weight: bold;
            margin-bottom: 0rpx;
            display: flex;
            flex-direction: row;
            justify-content: flex-start;
            align-items: center;
            display: flex;

            >text {
              max-width: 380rpx;
              font-family: "PingFang SC-Bold";
              font-weight: 400;
              font-size: 36rpx;
            }

            image {
              width: 36rpx;
              margin-left: 8rpx;
              margin-right: 10rpx;
            }

            .level {
              font-weight: 400;
              font-size: 20rpx;
              font-family: "PingFang SC-Bold";
              background: linear-gradient(360deg, #acf934 0%, #ffe439 100%);
              border-radius: 8rpx;
              padding: 2rpx 8rpx;
            }
          }

          .label {
            height: 55rpx;
            height: 110rpx;
            overflow: hidden;

            >view {
              color: #acf934;
              font-family: "PingFang SC-Medium";
              font-weight: 400;
              font-size: 20rpx;
              background: #1f1f1f;
              border-radius: 6px 6px 6px 6px;
              display: inline-block;
              margin-left: 10rpx;
              margin-bottom: 10rpx;
              padding: 6rpx 16rpx;
            }
          }
        }
      }

      .intro_row {
        width: 100%;
        margin-bottom: 20rpx;
        display: flex;
        align-items: center;

        .intro_text {
          color: #1f1f1f;
          font-size: 28rpx;
          font-family: "PingFang SC-Bold";
          font-weight: 400;
          padding-right: 0rpx;
        }

        .add_icon {
          width: 28rpx;
          margin-left: 10rpx;
        }
      }

      .bom {
        display: flex;
        align-items: center;
        justify-content: space-between;

        .follow_info {
          display: flex;
          align-items: center;
          justify-content: space-between;
          width: 340rpx;

          .follow-box {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            text-align: center;
          }

          .num {
            width: 100%;
            font-size: 36rpx;
            font-weight: bold;
            color: #333;
          }

          .label {
            width: 100%;
            font-size: 28rpx;
            color: #999;
          }

          .separator {
            width: 2rpx;
            height: 24rpx;
            background-color: #e5e5e5;
            margin: 0 30rpx;
          }
        }

        .points-box {
          display: flex;
          justify-content: space-between;

          .followTheAuthor {
            padding: 6rpx 40rpx 8rpx 35rpx;
            border-radius: 26rpx;
            margin-right: 16rpx;
            transition: all 0.6s;
            border: 2rpx solid transparent;

            &.followTheAuthor1 {
              color: #acf934;
              background: #1f1f1f;
            }

            &.followTheAuthor0 {
              border: 2rpx solid #1f1f1f;
              background: #fff;
            }
          }
        }
      }
    }
  }
}

.myinfo {
  width: 100%;
  display: flex;
  flex-direction: column;
  border-radius: 28rpx 28rpx 0 0;
  padding: 24rpx 20rpx;
  justify-content: flex-start;
  box-sizing: border-box;
  background: #fff;

  .numlist1 {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
  }
}
</style>