<template>
  <div class="Page" v-if="isAuthed">
    <div class="top" v-if="detail.status == 2 && showPage">
      <div class="tops">{{ detail.auditOpinion }}</div>
    </div>

    <div class="middle" v-if="showPage">
      <div class="cardTop">
        <span v-if="tabIndex0 != 3">
          <img :src="avatarUrl" alt="" v-if="avatarUrl" class="img"/>
          <img src="https://cdn.cunwuyun.cn/shandong10086/avatar.png" alt="" class="img" v-else/>
        </span>

        <span v-if="tabIndex0 == 3">
          <img :src="user.avatarUrl" alt="" v-if="user.avatarUrl" class="img"/>
        </span>

        <div class="rightTop">
          <div class="leftStaus">
            <span class="names">{{ detail.createUserName || detail.replyUserName }}</span>

            <span class="titless">
              <span class="topics" v-if="detail.topic"> {{ $dict.getLabel('villagerCircleTopic', detail.topic) }} |</span>
              <span> {{ detail.createTime }}</span>
            </span>
          </div>

          <div class="statusDele">
            <div class="status" :class="detail.status == 0 ? 'status2' : detail.status == 1 ? 'status0' : 'status1'" v-if="tabIndex0 == 3">
              <span>{{ detail.status == 0 ? '审核中' : detail.status == 1 ? '已通过' : '未通过' }}</span>
              <!-- {{ $dict.getLabel('auditStatus', detail.status) }} -->
            </div>
          </div>
        </div>
      </div>

      <div class="cardConts">{{ tabCurrent1 == 0 ? detail.content : detail.villagerCircleInfo.content }}</div>

      <!-- 图片 -->
      <div class="cardImg" v-if="tabCurrent1 == 0">
        <img :src="items.url" alt="" v-for="(items, i) in detail.pictures" :key="i" @click.stop="previewImage(detail.pictures, items.url)"/>
      </div>

      <div class="cardImg" v-if="tabCurrent1 == 1">
        <img :src="items.url" alt="" v-for="(items, i) in detail.villagerCircleInfo.pictures" :key="i"
             @click.stop="previewImage(detail.villagerCircleInfo.pictures, items.url)"/>
      </div>

      <!-- 分享 留言 点赞 -->
      <div class="cardBot">
        <div class="icons">
          <u-icon name="map-fill"></u-icon>
          <span>{{ detail.gpsDesc || gpsDesc }}</span>
        </div>

        <div class="icones" v-if="tabIndex0 != 3">
          <div class="img1">
            <button class="buttons" open-type="share" :data-id="item.id">
              <img src="https://cdn.cunwuyun.cn/dvcp/AppVillager/1.png" alt="" class="img11"/>
            </button>
          </div>

          <div class="rightFlex">
            <img src="https://cdn.cunwuyun.cn/dvcp/AppVillager/2.png" alt="" class="img2" @click="poupShow = true"/>
            <img src="https://cdn.cunwuyun.cn/dvcp/AppVillager/3.png" alt="" class="img3" @click="like" v-if="detail.upFlag == 0"/>
            <img src="https://cdn.cunwuyun.cn/dvcp/AppVillager/33.png" alt="" class="img3" @click="like" v-if="detail.upFlag == 1"/>
          </div>
        </div>
      </div>

      <!-- 评论 -->
      <div class="reply" v-if="tabCurrent1 == 0 && (detail.upCount != 0 || commentsList.length)">
        <div class="replyTop" v-if="detail.upCount != 0">
          <img src="https://cdn.cunwuyun.cn/dvcp/AppVillager/3.png" alt=""/>
          <span class="people" v-if="detail.upCount != 0 && detail.upUserNames">
            <span v-if="detail.upUserNames"> {{ detail.upUserNames }}</span>
            <span v-if="detail.upUserNames && detail.upCount * 1 > 3">等</span>
            <span> {{ detail.upCount > 1 ? detail.upCount : '' }}</span>
            <span v-if="detail.upUserNames && detail.upCount > 1">人</span>
          </span>
        </div>

        <div class="replyCont" @click="deleShowBtn(item)" v-for="(item, i) in commentsList" :key="i">
          <span class="nameLeft">{{ item.replyUserName }} : </span>
          <span class="contRight"> {{ item.content }}</span>
        </div>
      </div>

      <div class="reply" v-if="tabCurrent1 == 1 && (detail.villagerCircleInfo.upCount != 0 || commentsInfoList.length)">
        <div class="replyTop">
          <img src="https://cdn.cunwuyun.cn/dvcp/AppVillager/3.png" alt=""/>
          <span class="people" v-if="detail.villagerCircleInfo.upCount != 0 && upUserNamesList">
            <span v-if="upUserNamesList"> {{ upUserNamesList }}</span>
            <span v-if="upUserNamesList && detail.villagerCircleInfo.upCount * 1 > 3">等</span>
            <span> {{ detail.villagerCircleInfo.upCount > 1 ? detail.villagerCircleInfo.upCount : '' }}</span>
            <span v-if="upUserNamesList && detail.villagerCircleInfo.upCount > 1">人</span>
          </span>
        </div>

        <div class="replyCont" @click="deleShowBtn(item)" v-for="(item, i) in commentsInfoList" :key="i">
          <span class="nameLeft">{{ item.replyUserName }} : </span>
          <span class="contRight"> {{ item.content }}</span>
        </div>
      </div>
    </div>

    <u-popup v-model="poupShow" height="auto" mode="bottom">
      <div class="comments-wrapper" :style="{ paddingBottom: heights + 'px' }">
        <div class="form-item form-item__textarea">
          <div class="form-item__wrapper">
            <div class="form-item__right">
              <textarea v-model="content" class="textareas" :maxlength="140" placeholder="请输入内容..." @keyboardheightchange="keyboard"></textarea>
            </div>
          </div>
        </div>

        <div class="words">{{ content.length }}/140</div>

        <div class="bottombtn">
          <div class="emptys" @click="content = ''">清空内容</div>

          <div class="publishs" @click="send">发送</div>
        </div>
      </div>
    </u-popup>
  </div>

  <AiEmpty :description="`请往'我的-个人身份认证'<br/>进行身份认证`" v-else/>
</template>

<script>
import {mapState} from 'vuex'

export default {
  name: 'detail',
  components: {},
  props: {},
  data() {
    return {
      showPage: false,
      id: '',
      avatarUrl: '',
      detail: {},
      poupShow: false,
      content: '',
      flag: false,
      pageShow: false,
      flagLike: false,
      tabCurrent1: '',
      commentsList: [],
      commentsInfoList: [],
      nameList: [],
      nameLists: [],
      tabIndex0: '',
      gpsDesc: '',
      upUserNamesList: '',
      titleType: '',
      heights: '',
    }
  },
  computed: {
    ...mapState(['user']),

    isAuthed() {
      return this.user.status == 2
    },
  },
  onLoad(o) {
    this.id = o.id
    this.avatarUrl = o.avatarUrl
    this.tabCurrent1 = o.tabCurrent1 || 0
    this.tabIndex0 = o.tabIndex0
    this.gpsDesc = o.gpsDesc
    this.titleType = o.titleType
    this.$dict.load('villagerCircleTopic')
  },
  onShow() {
    uni.setNavigationBarTitle({
      title: this.titleType == 0 ? '话题详情' : this.titleType == 1 ? '话题详情' : this.titleType == 2 ? '评论详情' : '',
    })
    this.getDetail()
  },
  methods: {
    getDetail() {
      this.$instance
      .post(`/app/${this.tabCurrent1 != 0 ? 'appvillagercirclecomment' : 'appvillagercircleinfo'}/queryDetailById`, null, {
        params: {
          id: this.id,
        },
      })
      .then((res) => {
        if (res?.data) {
          this.detail = res.data
          // 我的话题通过的 不重复的评论
          if (this.detail.comments && this.detail.comments) {
            let lists = []
            this.detail.comments.map((e) => {
              if (e.status == 1) {
                lists.push(e)
              }
            })
            this.commentsList = lists

            if (this.commentsList.length) {
              var arr = this.commentsList
              var obj = {}
              this.nameList = arr.reduce(function (item, next) {
                obj[next.replyUserName] ? '' : (obj[next.replyUserName] = true && item.push(next))
                return item
              }, [])
            }
          }
          // 我的话题点赞人数
          if (this.detail.upUserNames) {
            this.detail.upUserNames = this.detail && this.detail.upUserNames.match(/[^\u3001]+(?:\u3001[^\u3001]+){2,2}/) == null ? this.detail.upUserNames : this.detail && this.detail.upUserNames.match(/[^\u3001]+(?:\u3001[^\u3001]+){2,2}/)[0]
          }
          // 我的评论 通过不重复的的评论
          if (this.detail.villagerCircleInfo && this.detail.villagerCircleInfo.comments) {
            let listss = []
            this.detail.villagerCircleInfo &&
            this.detail.villagerCircleInfo.comments.map((item) => {
              if (item.status == 1) {
                listss.push(item)
              }
            })
            this.commentsInfoList = listss

            if (this.commentsInfoList.length) {
              arr = this.commentsInfoList
              obj = {}
              this.nameLists = arr.reduce(function (item, next) {
                obj[next.replyUserName] ? '' : (obj[next.replyUserName] = true && item.push(next))
                return item
              }, [])
            }
          }
          // 我的评论 点赞人数
          if (this.detail.villagerCircleInfo) {
            const upUserName = this.detail.villagerCircleInfo.upUserNames
            this.upUserNamesList = upUserName.match(/[^\u3001]+(?:\u3001[^\u3001]+){2,2}/) == null ? upUserName : upUserName.match(/[^\u3001]+(?:\u3001[^\u3001]+){2,2}/)[0]
          }
        }
      })
      .finally(() => {
        this.showPage = true
      })
    },

    send() {
      if (this.flag) return
      if (!this.content) {
        return this.$u.toast('请输入内容')
      }
      this.flag = true
      this.$instance
      .post(`/app/appvillagercirclecomment/addOrUpdate`, {
        content: this.content,
        replyUserResidentId: this.user.id,
        replyUserName: this.user.realName,
        areaId: this.user.areaId,
        vcId: this.detail.id,
      })
      .then((res) => {
        if (res.code == 0) {
          this.$u.toast('发送成功')
          this.content = ''
          this.poupShow = false
          this.getDetail()
          this.flag = false
        }
      })
      .catch(() => {
        this.flag = false
        this.$u.toast('发送失败')
      })
    },

    like() {
      if (this.flagLike) return

      this.flagLike = true
      this.$instance
      .post(`/app/appvillagercircleinfo/up?id=${this.detail.id}&userId=${this.user.id}`)
      .then((res) => {
        this.$hideLoading()
        if (res.code === 0) {
          this.$u.toast(this.detail.upFlag == 0 ? '点赞成功' : '取消点赞成功')
          this.getDetail()
          setTimeout(() => {
            this.flagLike = false
          }, 600)
        }
      })
      .catch((err) => {
        this.$u.toast(err)
        this.flagLike = false
      })
    },

    previewImage(images, img) {
      uni.previewImage({
        urls: images.map((v) => v.url),
        current: img,
      })
    },

    keyboard(e) {
      const info = wx.getSystemInfoSync()
      if (info.platform === 'android') {
        this.heights = e.detail.height + 10
      } else {
        this.heights = e.detail.height - 250
      }
    },
  },
  onShareAppMessage() {
    let {
      detail: {id},
      tabCurrent1,
    } = this
    return {
      title: this.detail.content,
      path: this.$qs.stringifyUrl({
        url: '/pages/AppVillager/detail',
        query: {id, tabCurrent1},
      }),
    }
  },
}
</script>

<style scoped lang="scss">
.Page {
  background-color: #fff;
  height: 100%;
  height: 100vh;

  .top {
    padding-top: 32px;

    .tops {
      padding: 24px;
      font-size: 26px;
      color: #ff4466;
      background: #fff5f7;
      border-radius: 16px;
    }
  }

  .middle {
    padding: 32px 32px 48px 32px;
    background: #fff;

    .cardTop {
      display: flex;

      .img {
        width: 96px;
        height: 96px;
        border-radius: 50%;
      }

      .rightTop {
        display: flex;
        justify-content: space-between;
        align-items: center;
        width: calc(100% - 96px);

        .leftStaus {
          display: flex;
          flex-direction: column;
          margin-left: 16px;

          .names {
            font-size: 32px;
            font-weight: 500;
          }

          .titless {
            font-size: 26px;
            color: #6e727a;
            margin-top: 6px;

            .topics {
              margin-right: 8px;
            }
          }
        }

        .statusDele {
          display: flex;
          align-items: center;

          .status {
            font-size: 28px;
          }

          .status0 {
            color: #42d784;
          }

          .status1 {
            color: #ff4466;
          }

          .status2 {
            color: #ff883c;
          }
        }
      }
    }

    .cardConts {
      margin-top: 24px;
      font-size: 32px;
      line-height: 48px;
    }

    .cardImg {
      margin-top: 24px;

      img {
        width: 226px;
        height: 226px;
        margin-right: 4px;
      }

      img:nth-child(3n + 0) {
        margin-right: 0;
      }
    }

    .cardBot {
      margin-top: 24px;

      .icons {
        display: inline-block;
        padding: 8px 32px 8px 16px;
        background: #eff1f3;
        border-radius: 28px;
      }

      .icones {
        display: flex;
        justify-content: space-between;
        margin-top: 24px;

        .img1 {
          .buttons {
            justify-content: flex-start !important;

            .img11 {
              width: 48px;
              height: 48px;
            }
          }
        }

        .rightFlex {
          display: flex;

          .img2,
          .img3 {
            width: 48px;
            height: 48px;
          }

          .img3 {
            margin-left: 48px;
          }
        }
      }
    }

    .reply {
      margin-top: 32px;
      padding: 0 24px 24px 24px;
      background: #f7f8f9;
      border-radius: 16px;

      .replyTop {
        padding: 20px 0;
        box-shadow: inset 0px -1px 0px 0px #eee;

        img {
          width: 40px;
          height: 40px;
          vertical-align: bottom;
        }

        .people {
          font-size: 28px;
          font-weight: 500;
          color: #485d87;
          margin-left: 8px;
        }
      }

      .replyCont {
        padding-top: 24px;
        font-size: 28px;
        font-weight: 500;

        .nameLeft {
          color: #485d87;
          margin-right: 12px;
        }

        .contRight {
          color: #333;
        }
      }
    }
  }

  .comments-wrapper {
    padding: 16px 0;
    background: #f7f7f7;

    .form-item {
      background: #fff;
      margin: 0 16px;
      border-radius: 16px 16px 0 0;
      padding: 10px !important;

      .form-item__wrapper {
        display: flex;
        align-items: center;
        justify-content: space-between;
        height: 128px;
        padding-right: 28px;

        input {
          flex: 1;
          height: 100%;
          text-align: right;
          padding-right: 10px;
        }

        .form-item__right {
          display: flex;
          align-items: center;
          font-size: 32px;

          span {
            max-width: 400px;
            margin-right: 8px;
            color: #333333;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
          }

          i {
            margin-right: 8px;
            color: #999999;
          }
        }
      }

      &:last-child {
        .form-item__wrapper {
          border-bottom: none;
        }
      }

      &.form-item__imgs,
      &.form-item__textarea {
        .form-item__wrapper {
          display: block;
          height: auto;
          padding-bottom: 32px;
        }

        textarea {
          width: 100%;
          height: 160px;
        }

        .form-item__right {
          padding-left: 18px;

          .textareas {
            color: #303133;
          }
        }
      }
    }

    .words {
      background: #fff;
      border-radius: 0 0 16px 16px;
      margin: 0 16px;
      padding: 10px;
      text-align: right;
      font-size: 26px;
      color: #999999;
    }

    .bottombtn {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin: 0 30px;
      padding: 20px 0 0;

      .emptys {
        font-size: 26px;
        color: #666666;
      }

      .publishs {
        width: 144px;
        height: 64px;
        line-height: 64px;
        text-align: center;
        background: #1365dd;
        border-radius: 32px;
        color: #fff;
      }
    }
  }
}
</style>
