<template>
  <div class="conversationDetail">
    <AiTopFixed>
      <u-tabs :list="tabList" :is-scroll="false" :current="currentTabs" height="96" bg-color="#ffffff"
        inactive-color="#666666" :bar-style="barStyle" :active-item-style="activeStyle" active-color="#3975C6 " @change="change">
      </u-tabs>
      <div class="top-search">
        <div class="left" @click="isShowDate=true">
          <div class="color-999" v-if="!dateList.length">开始时间-结束时间</div>
          <div v-else>{{dateList[0]}}至{{dateList[1]}}</div>
          <img src="./img/del-icon.png" alt="" v-if="dateList.length" @click.stop="clearDate()">
        </div>
        <div class="right">
          <u-search v-model="keyword" :clearabled="true" placeholder="请输入关键词" :show-action="false" bg-color="#F5F5F5"
            search-icon-color="#999" color="#999" height="58" @search="getListInit" @clear="getListInit">
          </u-search>
        </div>
      </div>
    </AiTopFixed>
    <div class="conversation-list" v-if="list.length">
      <div v-for="(item, index) in list" :key="index">
        <div class="item item-right" v-if="item.userId == user.wxUserId">
          <div class="content">
            <span v-if="item.msgType == 'text'"></span>
            <p v-if="item.msgType == 'text'">{{item.content}}</p>
            <div class="img-list" v-if="item.msgType == 'image'">
              <img :src="item.sdkFileUrl" alt="" @click="previewImage(item.sdkFileUrl)">
            </div>
            <!-- <AiVideo :src="item.sdkFileUrl" autoplay v-if="item.msgType == 'voice'"></AiVideo> -->
            <audio :src="item.sdkFileUrl" ref="audio" :controls="true"  style="display: block;" v-if="item.msgType == 'voice'"></audio>
            <!-- <div @click="openRecord(item)" v-if="item.msgType == 'voice'">123</div> -->
          </div>
          <img :src="item.userAvatar" alt="" class="user-img">
        </div>
        <div class="item item-left" v-else>
          <img :src="item.userAvatar" alt="" class="user-img">
          <div class="content">
            <span></span>
            <p v-if="item.msgType == 'text'">{{item.content}}</p>
          </div>
          <!-- <img src="./img/fail-icon.png" alt="" class="fail-img"> -->
        </div>
      </div>
    </div>
    <AiEmpty v-else/>
    <u-calendar v-model="isShowDate" @change="onDateChange" mode="range"></u-calendar>
    <div class="footer" @click="toGroup">{{type == 1 ? '群详情' : '个人信息'}}</div>
  </div>
</template>

<script>
import {mapState} from "vuex";
import BenzAMRRecoder from 'benz-amr-recorder';
export default {
  name: "conversationDetail",
  data() {
    return {
      tabList: [
        {name: '全部', value: ''},
        {name: '文本', value: 'text'},
        {name: '图片', value: 'image'},
        {name: '语音', value: 'voice'},
      ],
      currentTabs: 0,
      barStyle: {
        'width': '24px',
        'height': '2px',
        'border-radius': '0',
        'bottom': '5px'
      },
      activeStyle: {
        'font-weight' : '400',
        'color': '#000000'
      },
      keyword: '',
      toUserId: '',
      roomId: '',
      current: 1,
      pages: 2,
      list: [],
      type: '',
      isShowDate: false,
      dateList: [],

      amrPlay: '',
      amrPlayStatus: false,
    };
  },
  computed: {
    ...mapState(['user']),
  },
  onLoad(option) {
    if(option.toUserId) {
      this.toUserId = option.toUserId
    }else {
      this.roomId = option.roomId
    }
    this.type = option.type
    this.getList()
  },
  onShow() {
    document.title = '会话详情'
  },
  methods: {
    toGroup() {
      if(this.type == 1) {
        uni.navigateTo({url: `./groupDetail`})
      }else {
        uni.navigateTo({url: `./userInfo`})
      }
    },
    change(index) {
      this.keyword = ''
      this.currentTabs = index
      this.getListInit()
    },
    onDateChange (e) {
      console.log(e)
      this.dateList = [e.startDate, e.endDate]
      this.getListInit()
    },
    clearDate() {
      this.dateList = []
      this.getListInit()
    },
    getListInit() {
      this.current = 1
      this.pages = 2
      this.list = []
      this.getList()
    },
    getList() {
      if(this.current > this.pages)  return
      var msgTypeList = ['','image', 'voice', 'text']
      this.$http.post(`/app/appsessionarchiveinfo/listByUser`, null, {
        params: {
          current: this.current,
          size: 10,
          msgType: msgTypeList[this.currentTabs],
          toUserId: this.toUserId,
          roomId: this.roomId,
          type: this.type,
          content: this.keyword,
          startTime: this.dateList.length ? this.dateList[0] : '',
          endTime: this.dateList.length ? '' : this.dateList[1]
        }
      }).then(res=> {
        if(res?.data) {
          // res.data.records.map((item) => {
          //   if(item.msgType == 'voice'){
          //     //amr对象不能共用，所以要为每条语音消息初始化一个amr对象
          //     var amr = new BenzAMRRecoder();
          //     amr.initWithUrl(`请求的amr地址`).then(() => {
          //       item.amrTime = Math.ceil(amr.getDuration());    //获取音频总时长
          //     });
          //     item.amr = amr;  //将初始化好的amr赋值到当前消息对象data中
          //   }
          // })
          this.list = this.current > 1 ? [...this.list, ...res.data.records]: res.data.records
          this.pages = res.data.pages
        }
      })
    },
    previewImage(img) {
      uni.previewImage({
        urls: [img],
        current: img
      })
    },
    openRecord (row) {
      // this.amrPlayStatus = false
      // this.$nextTick(() => {
      //   this.amrPlay = new BenzAMRRecoder()
      //   this.amrPlay.onPlay(() => {
      //     this.amrPlayStatus = true
      //   })
      //   this.amrPlay.onStop(() => {
      //     this.amrPlayStatus = false
      //   })
      //   this.amrPlay.onPause(() => {
      //     this.amrPlayStatus = false
      //   })
      //   this.amrPlay.onResume(() => {
      //     this.amrPlayStatus = true
      //   })
      //   this.amrPlay.initWithUrl(row.sdkFileUrl).then(() => {
      //     console.info('armPlay:', this.amrPlay)
      //     this.amrPlay.play()
      //   })
      // })

      //amr.isPlaying()，音频播放状态，正在播放为true，否则为false；
      if (row.amr.isPlaying()) {
        row.amr.pause();  //暂停
      } else {
        row.amr.play();  //播放
      }


    },
    handleClose () {
      if (this.amrPlay) {
        this.amrPlay.stop()
      }
    },
    pauseAmrPlay () {
      if (this.amrPlay) {
        this.amrPlay.pause()
      }
    },
    resumeAmrPlay () {
      if (this.amrPlay) {
        if (this.amrPlay.isPaused()) {
          this.amrPlay.resume()
        } else if (!this.amrPlay.isPlaying()) {
          this.amrPlay.play()
        }
      }
    },
  },
  onReachBottom() {
    this.current ++
    this.getList()
  }
};
</script>

<style lang="scss" scoped>
.conversationDetail {
  height: 100%;
  ::v-deep .AiTopFixed {
    .placeholder {
      .content {
        padding: 0 !important;
      }
    }
    .fixed {
      margin: 0 !important;
      .content {
        padding: 0 !important;
      }
    }
  }
  .top-search {
    padding: 20px 32px;
    display: flex;
    .left {
      width: 400px;
      div {
        display: inline-block;
        line-height: 64px;
      }
      img {
        display: inline-block;
        width: 32px;
        height: 32px;
        margin-left: 8px;
        vertical-align: middle;
      }
    }
    .right {
      width: calc(100% - 400px);
    }
  }
  .conversation-list {
    padding: 48px 32px 160px;
    // background-color: #EBECF0;
    .item {
      margin-bottom: 48px;
      display: flex;
      .user-img {
        width: 72px;
        height: 72px;
        margin-right: 20px;
        border-radius: 50%;
      }
      .content {
        max-width: calc(100% - 144px);
        position: relative;
        span {
          width: 0px;
          height: 0px;
          border: 10px solid transparent;
          position: absolute;
          top: 20px;
        }
        p {
          display: inline-block;
          padding: 14px 54px 14px 32px;
          background-color: #fff;
          font-family: PingFangSC-Regular;
          font-size: 32px;
          color: #333;
          word-break: break-all;
        }
      }
      .fail-img {
        width: 32px;
        height: 32px;
        margin: auto 0 auto 24px;
      }
      .img-list {
        img {
          width: 100px;
          height: 100px;
          margin: 0 16px 16px 0;
        }
      }
    }
    .item-left {
      .content {
        span {
          left: -18px;
          border-right-color: #fff;
        }
      }
    }
    .item-right {
      width: 100%;
      justify-content: right;
      .user-img {
        margin: 0 0 0 20px;
      }
      .content {
        span {
          border-left-color: #C7E7FE;
          right: -18px;
        }
        p {
          background-color: #C7E7FE;
          padding: 14px 32px 14px 54px;
        }
      }
    }
  }
  .footer{
    width: 100%;
    height: 112px;
    line-height: 112px;
    background: #1365DD;
    box-shadow: inset 0px 1px 0px 0px #EEEEEE;
    font-size: 32px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #FFF;
    text-align: center;
    position: fixed;
    bottom: 0;
    left: 0;
  }
}
</style>