<template>
  <div class="TopicDetail">
    <h2>#{{ name }}</h2>
    <p>贴子：{{ total }}</p>
    <div class="tab">
      <span @click="changeTab(0)" :class="[currIndex === 0 ? 'active' : '']">广场</span>
      <span @click="changeTab(1)" :class="[currIndex === 1 ? 'active' : '']">社区</span>
    </div>
    <div class="AppCircle-list">
      <div
        class="item"
        v-for="(item, index) in list"
        :key="index"
        @click="$linkTo('./Detail?isFrom=topic&id=' + item.id + '&name=' + name + '&themeId=' + themeId)">
        <div class="item-top">
          <image :src="item.createUserAvatar || 'https://cdn.cunwuyun.cn/wxmp/fengdu/avatar.png'" />
          <div class="right">
            <h3>{{ item.createUserName }}</h3>
            <span v-if="item.publishDepartName">{{ item.publishDepartName }}</span>
          </div>
        </div>
        <div class="item-content">
          <text>{{ item.content }}</text>
        </div>
        <div class="item-imgs" v-if="item.files.length">
          <image mode="aspectFill" hover-stop-propagation @click.stop="previewImage(e.url, item.files)" v-for="(e, i) in item.files" :key="i" :src="e.url" />
        </div>
        <p>{{ item.createTime }}</p>
        <div class="item-bottom">
          <button
            hover-stop-propagation
            @click.stop=""
            open-type="share"
            :data-content="item.content"
            :data-themeid="item.themeId"
            :data-id="item.id"
            :data-name="item.topicName">
            <image src="https://cdn.cunwuyun.cn/wxmp/fengdu/ic-zhuanfa.png" />
            <i>{{ item.sharedCount }}</i>
          </button>
          <div hover-stop-propagation @click.stop="reciate(item.id, item.appreciateStatus)">
            <image :src="item.appreciateStatus ? 'https://cdn.cunwuyun.cn/wxmp/fengdu/zan-active.png' : 'https://cdn.cunwuyun.cn/wxmp/fengdu/zan.png'" />
            <i>{{ item.appreciateCount }}</i>
          </div>
          <div>
            <image src="https://cdn.cunwuyun.cn/wxmp/fengdu/ic-pinglun.png" />
            <i>{{ item.commentCount }}</i>
          </div>
        </div>
      </div>
      <AiEmpty v-if="!list.length"></AiEmpty>
    </div>
    <AiLogin ref="login"/>
  </div>
</template>
<script>
  import { mapActions, mapState } from 'vuex'

  export default {
    name: 'TopicDetail',
    appName: '话题详情',

    data () {
      return {
        currIndex: 0,
        name: '',
        themeId: '',
        list: [],
        isMore: false,
        current: 1,
        total: 0
      }
    },

    computed: {
      ...mapState(['user', 'token'])
    },

    onLoad (query) {
      this.themeId = query.themeId
      this.name = query.name

      this.$nextTick(() => {
        this.getList()
      })
    },

    methods: {
      ...mapActions(['autoLogin', 'authCheck']),

      changeTab (index) {
        this.currIndex = index
        this.isMore = false
        this.current = 1

        this.$nextTick(() => {
          this.getList()
        })
      },

      reciate (id, appreciateStatus) {
        this.$instance.post(`/app/appneighborhoodassistance/appreciate?id=${id}`).then(res => {
          if (res.code === 0) {
            this.$toast(appreciateStatus ? '取消点赞' : '点赞成功')

            this.changeTab(this.currIndex)
          }
        })
      },

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

      getList () {
        if (this.isMore) return

        this.$loading()
        this.$instance.post(`/app/appneighborhoodassistance/list`, null, {
          params: {
            current: this.current,
            size: 10,
            themeId: this.themeId,
            visibleRange: this.currIndex === 0 ? 1 : 0
          }
        }).then(res => {
          if (res.code === 0) {
            this.$hideLoading()
            if (this.current > 1) {
              this.list = [...this.list, ...res.data.records]
            } else {
              this.list = res.data.records
            }

            this.total = res.data.total

            if (res.data.records.length < 10) {
              this.isMore = true

              return false
            }

            this.current = this.current + 1
          } else {
            this.isMore = true
          }
        }).catch(() => {
          this.$hideLoading()
        })
      }
    },

    onReachBottom () {
      this.getList()
    },

    onShareAppMessage (e) {
      this.$instance.post(`/app/appneighborhoodassistance/share?id=${e.target.dataset.id}`).then(res => {
        if (res.code === 0) {
          this.changeTab(this.currIndex)
        }
      })

      return {
        title: e.target.dataset.content,
        path: `/pages/AppCircle/Detail?id=${e.target.dataset.id}&themeId=${e.target.dataset.themeid}&name=${e.target.dataset.name}`
      }
    }
  }
</script>

<style scoped lang="scss">
  .TopicDetail {
    padding-top: 32px;
    padding-bottom: 40px;

    div {
      box-sizing: border-box;
    }

    & > h2 {
      margin: 0 32px 24px;
      font-size: 46px;
      color: #3d3d3d;
    }

    .tab {
      margin: 0 32px 24px;
    }

    & > p {
      margin: 0 32px 12px;
      color: #999999;
      font-size: 28px;
    }

    .AppCircle-list {
      .item {
        margin: 0 24px 24px;
        padding: 24px 24px 0;
        background: #ffffff;
        border-radius: 16px;
        box-shadow: inset 0 -1px 0 0 #eeeeee;

        .item-top {
          display: flex;
          align-items: center;

          image {
            width: 96px;
            height: 96px;
            margin-right: 16px;
            border-radius: 50%;
          }

          h3 {
            color: #333333;
            text-align: left;
            font-size: 34px;
            font-weight: 500;
          }

          span {
            margin-top: 12px;
            color: #999999;
            font-size: 28px;
          }
        }

        .item-imgs {
          display: flex;
          align-items: center;
          flex-wrap: wrap;

          image {
            height: 208px;
            width: 33.33%;
            padding-right: 12px;
            margin-bottom: 12px;
            box-sizing: border-box;

            &:nth-of-type(3n) {
              padding-right: 0;
            }
          }
        }

        & > p {
          margin: 12px 0;
          font-size: 28px;
          color: #333333;
        }

        .item-bottom {
          display: flex;
          align-items: center;
          height: 88px;
          border-top: 1px solid #eeeeee;

          div, button {
            display: flex;
            align-items: center;
            justify-content: center;
            flex: 1;

            image {
              width: 40px;
              height: 40px;
              margin: 16px;
            }

            i {
              color: #687DA6;
              font-size: 28px;
            }
          }
        }

        .item-content {
          margin: 16px 0;
          line-height: 1.3;
          text-align: justify;

          span {
            font-size: 28px;
            color: #4181FF;
          }

          text {
            font-size: 28px;
            color: #333333;
          }
        }
      }
    }

    .tab {
      display: flex;
      align-items: center;
      height: 100px;

      span {
        position: relative;
        height: 100px;
        line-height: 100px;
        color: #222;
        font-size: 30px;

        &:first-child {
          margin-right: 64px;
        }

        &.active {
          font-weight: 600;
          font-size: 32px;

          &::after {
            position: absolute;
            bottom: 12px;
            left: 50%;
            width: 40px;
            height: 6px;
            border-radius: 3px;
            background: #2d7dffff;
            transform: translateX(-50%);
            content: ' ';
          }
        }
      }
    }
  }
</style>
