<template>
  <!-- #ifdef H5 -->
  <view class="message_view_page">
    <!-- 横幅 -->
    <view class="gonggao">
      <view>防骗手册，为您保驾护航</view>
      <text class="iconfont icon-jinru"></text>
    </view>
    <!-- 商品信息 -->
    <view class="goods_info">
      <!-- 商品图片 -->
      <view class="goods_img">
        <image style="width: 100%; height: 100%;" mode="aspectFill" :src="goodsDetail.goodsPicUrl"></image>
      </view>
      <!-- 商品描述 -->
      <view class="goods_text">
        <view class="goods_name">{{goodsDetail.title}}</view>
        <view class="goods_pon">
          <view>
            <view class="goods_jiag">{{goodsDetail.amount?"￥"+goodsDetail.amount:"面议"}}</view>
            <view class="goods_fuwu">卖家包运费</view>
          </view>
          <view class="go_goods_btn">立即购买</view>
        </view>
      </view>
    </view>
    <!-- 聊天内容 -->
    <view class="message_view">
      <scroll-view class="message_view_scroll" :scroll-y="true" :scroll-top="scrollTop" :enhanced="true"
        :bounces="false" :show-scrollbar="false" @scrolltoupper="upper" @scrolltolower="lower" @scroll="scroll">
        <view style="box-sizing: border-box;padding: 10rpx 0;" ref="scrollviewDiv" class="scrollviewDiv">
          <view v-for="(item,index) in list" :key="index">
            <!-- 右消息 -->
            <view class="msg_view" v-if="userId===item.userId">
              <!-- 占位 -->
              <view class="msg_user_img_kong">
              </view>
              <view class="msg_view_info msg_view_info_right">
                <view class="msg_view_conent">
                  {{item.message}}
                </view>
                <view class="msg_view_time">{{item.createTime}}</view>
              </view>
              <!-- 头像 -->
              <view class="msg_user_img">
                <image style="width: 100%; height: 100%;" mode="aspectFill" :src="item.userPic"></image>
              </view>
            </view>
            <!-- 左消息 -->
            <view class="msg_view" v-else>
              <!-- 头像 -->
              <view class="msg_user_img">
                <image style="width: 100%; height: 100%;" mode="aspectFill" :src="item.userPic"></image>
              </view>
              <view class="msg_view_info msg_view_info_left">
                <view class="msg_view_conent">{{item.message}}</view>
                <view class="msg_view_time">{{item.createTime}}</view>
              </view>
              <!-- 占位 -->
              <view class="msg_user_img_kong">
              </view>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
    <!-- 输入框 -->
    <view>
      <view class="text_input_view">
        <view class="text_input_lay">
          <view class="text_input_tengh">
            <input class="uni-input_chat" focus placeholder="请输入" v-model="message" />
          </view>
          <!-- 更多功能 -->
          <!-- <view class="text_input_mount" @click="gengduo = !gengduo">
            +
          </view> -->
          <!-- 发送消息 -->
          <view class="text_input_mount" @click="send">
            <image style="width: 60%; height: 60%;" mode="aspectFill" src="/static/tabBar/icon_issue01.png"></image>
          </view>
        </view>
      </view>
      <view v-if="gengduo" class="gengduo_view">
        <view class="gengduo_view_ay">
          <view class="gengduo_view_icon"></view>
          <view class="gengduo_view_name">相册</view>
        </view>
        <view class="gengduo_view_ay">
          <view class="gengduo_view_icon"></view>
          <view class="gengduo_view_name">拍照</view>
        </view>
        <view class="gengduo_view_ay">
          <view class="gengduo_view_icon"></view>
          <view class="gengduo_view_name">位置</view>
        </view>
        <view class="gengduo_view_ay">
          <view class="gengduo_view_icon"></view>
          <view class="gengduo_view_name">举报</view>
        </view>
      </view>
    </view>
  </view>
  <!-- #endif -->
  <!-- #ifndef H5 -->
  <web-view :src="webViewHref('/pages/message/msgView',pam)"></web-view>
  <!-- #endif -->
</template>

<script>
  import api from '@/common/http/'
  export default {
    data() {
      return {
        scrollTop: 0,
        gengduo: false,
        pam: {},
        list: [],
        message: "",
        userId: this.$store.state.user.userId,
        goodsDetail: {}
      }
    },
    onLoad(pam) {
      this.pam = pam
    },
    onShow() {
      // #ifdef H5
      this.getGoods()
      this.getNewList()
      // #endif
    },
    onHide() {
      // #ifdef H5
      if (this.timeId) {
        clearTimeout(this.timeId)
      }
      // #endif
    },
    methods: {
      // #ifdef H5
      getGoods() {
        if (!this.pam.goodsId) {
          return
        }
        api.postJson('/goods/detail', {
          id: this.pam.goodsId,
        }).then(res => {
          this.goodsDetail = res.data
          console.log(res)
        })
      },
      getNewList() {
        if (this.timeId) {
          clearTimeout(this.timeId)
        }
        this.getMsgList(0, () => {
          this.timeId = setTimeout(this.getNewList, 3000)
          uni.createSelectorQuery().select('.message_view_scroll').boundingClientRect(data => {
            uni.createSelectorQuery().select('.scrollviewDiv').boundingClientRect(data2 => {
              if ((this.scrollTop + data.height + 10) >= data2.height) {
                this.scrollTop = data2.height
              }
            }).exec()
          }).exec()
        })
      },
      getMsgList(flag, cb) {
        api.postJson('/user/talk/list', {
          userId: this.$store.state.user.userId,
          flag: flag,
          ...(() => {
            if (this.pam.goodsId) {
              return {
                goodsId: this.pam.goodsId,
              }
            }
            return {}
          })(),
          ...(() => {
            if (this.pam.groupId) {
              return {
                groupId: this.pam.groupId,
              }
            }
            return {}
          })(),
          ...(() => {
            if (this.list.length) {
              return {
                id: this.list[flag ? 0 : this.list.length - 1].id
              }
            }
            return {}
          })(),
        }, false).then(res => {
          if (flag) {
            this.list.unshift(...res.data)
          } else {
            this.list.push(...res.data)
          }
          cb && cb()
        })
      },
      upper() {
        // console.log("顶")
        uni.createSelectorQuery().select('.scrollviewDiv').boundingClientRect(data => {
          var oldHeight = data.height
          this.getMsgList(1, () => {
            this.$nextTick(() => {
              uni.createSelectorQuery().select('.scrollviewDiv').boundingClientRect(data2 => {
                this.scrollTop = data2.height - oldHeight
              }).exec()
            })
          })
        }).exec()
      },
      lower() {
        // console.log("底")
      },
      scroll(e) {
        this.scrollTop = e.detail.scrollTop
      },
      send() {
        if (this.pam.goodsId && this.$store.state.user.userId) {
          api.postJson('/user/talk/send', {
            message: this.message,
            userId: this.$store.state.user.userId,
            goodsId: this.pam.goodsId,
            ...(() => {
              if (this.pam.groupId) {
                return {
                  groupId: this.pam.groupId,
                }
              }
              return {}
            })(),
          }, false).then(res => {
            this.message = ""
            this.getNewList()
          })
        }
      },
      // #endif
      // #ifndef H5

      // #endif
    },
  }
</script>

<style lang="scss" scoped>
  page {
    height: 100%;
    overflow: hidden;
  }

  .message_view_page {
    overflow: hidden;
    width: 100%;
    /* #ifdef H5 */
    height: 100%;
    /* #endif */
    /* #ifndef H5 */
    height: 100vh;
    /* #endif */
    display: flex;
    flex-direction: column;

    .msg_view {
      width: 100%;
      height: auto;
      box-sizing: border-box;
      padding: 10rpx 20rpx;
      display: flex;

      .msg_view_info {
        flex: 1;
        min-height: 80rpx;

        .msg_view_conent {
          display: inline-block;
          max-width: 100%;
          height: auto;
          background: #fff;
          border-radius: 10rpx;
          box-sizing: border-box;
          padding: 18rpx;
          word-break: break-all;
        }

        .msg_view_time {
          margin-top: 8rpx;
          font-size: 22rpx;
          color: #aaa;
        }
      }

      .msg_view_info_left {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        box-sizing: border-box;
        padding-left: 30rpx;
      }

      .msg_view_info_right {
        display: flex;
        flex-direction: column;
        align-items: flex-end;
        padding-right: 30rpx;
      }

      .msg_user_img_kong {
        width: 80rpx;
        height: 80rpx;
      }

      .msg_user_img {
        background: #fff;
        width: 80rpx;
        height: 80rpx;
        border-radius: 40rpx;
      }
    }

    .gengduo_view {
      width: 100%;
      height: auto;
      box-sizing: border-box;
      padding: 8rpx 20rpx 18rpx 20rpx;
      background: #fff;
      display: flex;
      justify-content: space-around;

      .gengduo_view_ay {
        width: 110rpx;
      }

      .gengduo_view_icon {
        width: 110rpx;
        height: 110rpx;
        border-radius: 16rpx;
        background: #eee;
        margin-bottom: 16rpx;
      }

      .gengduo_view_name {
        font-size: 22rpx;
        text-align: center;
        color: #aaa;
      }
    }

    .text_input_view {
      width: 100%;
      height: 100rpx;
      box-sizing: border-box;
      padding: 18rpx 20rpx;
      background: #fff;

      .text_input_lay {
        width: 100%;
        height: 100%;
        display: flex;

        .text_input_tengh {
          flex: 1;
          height: 100%;
          box-sizing: border-box;
          padding-right: 18rpx;

          .uni-input_chat {
            border-radius: 34rpx;
            width: 100%;
            height: 100%;
            box-sizing: border-box;
            border: 1rpx solid #eee;
            padding: 0 10rpx;
          }
        }

        .text_input_mount {
          width: 64rpx;
          height: 64rpx;
          font-size: 52rpx;
          font-weight: bold;
          color: #999;
          display: flex;
          justify-content: center;
          align-items: center;
        }
      }
    }

    .goods_info {
      width: 100%;
      height: auto;
      padding: 20rpx;
      box-sizing: border-box;
      background: #fff;
      display: flex;

      .goods_img {
        width: 100rpx;
        height: 100rpx;
      }

      .goods_text {
        flex: 1;
        min-height: 100rpx;
        height: auto;
        box-sizing: border-box;
        padding-left: 16rpx;

        .goods_name {
          width: 100%;
          margin-bottom: 10rpx;
          font-size: 28rpx;
          font-weight: bold;
          overflow: hidden;
          /*超出部分隐藏*/
          white-space: nowrap;
          /*禁止换行*/
          text-overflow: ellipsis;
          /*省略号*/
        }

        .goods_jiag {
          font-size: 26rpx;
          color: red;
        }

        .goods_fuwu {
          font-size: 24rpx;
          color: #aaa;
        }

        .goods_pon {
          width: 100%;
          display: flex;
          justify-content: space-between;
          align-items: center;

          .go_goods_btn {
            width: auto;
            height: auto;
            box-sizing: border-box;
            padding: 14rpx 28rpx;
            border-radius: 10rpx;
            font-size: 24rpx;
            font-weight: bold;
            color: #fff;
            background: rgb(250, 42, 42);
          }
        }
      }
    }

    .message_view {
      width: 100%;
      flex: 1;
      position: relative;

      .message_view_scroll {
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
      }
    }

    .gonggao {
      width: 100%;
      height: 80rpx;
      background: #fff2d4;
      box-sizing: border-box;
      padding: 0 20rpx;
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size: 24rpx;
      color: rgb(230, 138, 19);

      view {
        color: rgb(230, 138, 19);
      }
    }
  }
</style>