<template>
  <page-meta :page-style="themeColor"></page-meta>
  <view class="message-list-content">
    <!-- 列表内容 -->
    <mescroll-uni ref="mescroll" @getData="getList" top="0">
      <block slot="list">
        <view class="message-list" v-if="Object.keys(list).length > 0">
          <view class="message-block" v-for="(item,index) in list" :key="index">
            <view class="message-block-top">
              <view class="message-block-top-image">
                <image class="top-image" :src="$util.img(type_info.icon)" mode="widthFix"></image>
              </view>
              <view class="message-block-top-title">{{ type_info.title }}</view>
            </view>
            <view class="message-block-message">{{ item.message_content }}</view>
            <view class="message-block-message message-block-reply" v-if="item.reply_content">回复内容：{{ item.reply_content }}</view>
            <view class="message-block-bottom">{{ $util.timeStampTurnTime(item.created_time) }}</view>
          </view>
        </view>
        <view v-else>
          <ns-empty :isIndex="false" text="暂无消息通知"></ns-empty>
        </view>
      </block>
    </mescroll-uni>
    <!--登录弹框-->
    <ns-login ref="login"></ns-login>
    <!--加载动画-->
    <loading-cover ref="loadingCover"></loading-cover>
  </view>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      // 类型信息
      message_type: 'business',
      type_info: {},
      // 消息类型：1=交易信息，2=系统消息，3=通知消息，4=留言反馈
      types: {
        business: {title: '交易信息', type: 'business', value: 1, icon: 'public/static/img/message/business.png'},
        system: {title: '系统消息', type: 'system', value: 2, icon: 'public/static/img/message/system.png'},
        notice: {title: '通知消息', type: 'notice', value: 3, icon: 'public/static/img/message/notice.png'},
        words: {title: '留言反馈', type: 'words', value: 4, icon: 'public/static/img/message/words.png'},
      },

    };
  },
  components: {},
  mixins: [],
  onLoad(option) {
    this.message_type = option.message_type || 'business';
    // 获取页面标题
    this.type_info = this.types[this.message_type];
    uni.setNavigationBarTitle({
      title: this.type_info.title
    });
  },
  onShow() {},
  onReady(){
    // 判断是否登录
    if (!uni.getStorageSync('token')) this.$refs.login.open('/pages_promotion/futures/list');
  },
  methods: {
    // 获取消息列表
    getList(mescroll) {
      this.$api.sendRequest({
        url:  '/message/api/message/messageList',
        data: {
          page: mescroll.num || 1,
          page_size: mescroll.size || 10,
          message_type: this.types[this.message_type].value,
        },
        success: res => {
          if(parseInt(res.code) === 0){
            mescroll.endSuccess(Object.keys(res.data.list).length);

            //设置列表数据
            if (parseInt(mescroll.num) === 1) this.list = []; //如果是第一页需手动制空列表
            this.list = this.list.concat(res.data.list); //追加新数据
          }

          if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
        },
        fail: res => {
          mescroll.endErr();
          if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
        }
      });
    },
  },
  onBackPress(options) {
    if (options.from === 'navigateBack') return false;
    this.$util.redirectTo('/pages/member/index');
    return true;
  }
};
</script>

<style lang="scss" scoped>
.message-list-content{
  background-color: #f5f5f5;
  min-height: 100vh;

  .message-list{
    padding:30rpx;
    .message-block{
      background: #fff;
      border-radius: 20rpx;
      padding: 30rpx 30rpx 20rpx 30rpx;
      margin-bottom: 30rpx;
      .message-block-top{
        --top-height--: 60rpx;
        height: var(--top-height--);
        width: 100%;
        display: inline-flex;
        flex-direction: row;
        flex-wrap: nowrap;
        justify-content: flex-start;
        align-items: center;
        .message-block-top-image{
          height: var(--top-height--);
          width: var(--top-height--);
          position: relative;

          .top-image{
            width: 100% !important;
            height: 100% !important;
          }
        }
        .message-block-top-title{
          font-size: 32rpx;
          font-weight: bold;
          color: #353535;
          margin-left: 20rpx;
        }
      }
      .message-block-message{
        line-height: 40rpx;
        font-size: 28rpx;
        margin: 20rpx 0;
        color: #828282;
      }
      .message-block-reply{
        color: #c0c0c0;
      }
      .message-block-bottom{
        height: 60rpx;
        line-height: 60rpx;
        font-size: 26rpx;
        color: #bababa;
        display: inline-flex;
        flex-direction: row;
        flex-wrap: nowrap;
        justify-content: flex-end;
        align-items: baseline;
      }
    }
  }
}
</style>
