<template>
  <van-sticky>
    <van-nav-bar title="消息">
      <template #right>
        <div v-if="!isEdit" @click="editMes"><span style="color: #fff">编辑</span></div>
        <div v-else>
          <span class="del-btn" :class="m_list.length == 0?'disabled':''" @click="delNews">删除</span>
          &nbsp;
          <span @click="editCancel" style="color: #fff">取消</span>
        </div>
      </template>
    </van-nav-bar>
  </van-sticky>
  <van-list
      v-model:loading="loading"
      :finished="finished"
      :immediate-check="false"
      finished-text="没有更多了"
      @load="onload"
  >
    <ul v-for="(item,index) in list" class="message-list">
      <li @click="toDetail(item.id)">
        <!--                <span class="dot" v-if="index == 0 && !isEdit"></span>-->
        <div v-if="isEdit" class="message-check" @click.stop="manageList(item,index)">
          <van-checkbox v-model="item.isCheck" shape="square"></van-checkbox>
        </div>

        <h3 class="message-title">{{ item.name }}</h3>
        <h4 class="message-time">{{ item.datetime }}</h4>
        <h5 class="message-content line-two">{{ item.brief }}</h5>
        <div class="message-more">
          <span>查看详情</span>
          <van-icon name="arrow-down" size="16"/>
        </div>

      </li>
    </ul>
  </van-list>
  <van-tabbar route placeholder>
    <van-tabbar-item replace icon="home-o" to="/home">首页</van-tabbar-item>
    <van-tabbar-item replace icon="chat-o" to="/chat">消息</van-tabbar-item>
    <van-tabbar-item replace icon="user-o" to="/user">我的</van-tabbar-item>
  </van-tabbar>
</template>
<script>
import {news_list,news_del} from "@/pages/_chat/api.js";
import {showConfirmDialog,showSuccessToast, showFailToast} from 'vant';

export default {
  data() {
    return {
      isEdit: false,
      listCheck: [],
      list: [],
      loading: false,
      finished: false,
      cur_page: 1,
      total: '0',
      m_list: []
    }
  },
  methods: {
    getMessageList() {
      let data = {
        page: this.cur_page,
        limit: '10',
        ni_id: '138'
      }
      news_list(data).then(res => {
        if (res.code == 1) {
          this.list = res.data.page_data;
          this.cur_page = res.data.page_now;
          this.total = res.data.total_rows;
        }
      })
    },
    handleNavLeftClick() {
      this.$router.back();
    },
    editMes() {
      this.isEdit = true;
    },
    editCancel() {
      this.isEdit = false;
      this.list.filter(item => {
        return item.isCheck = false;
      });
      this.m_list = [];
    },
    onload() {
      setTimeout(() => {
        // 加载状态结束
        this.loading = false;

        // 数据全部加载完成
        if (this.list.length == this.total) {
          this.finished = true;
        } else {
          this.cur_page++;
          let data = {
            page: this.cur_page,
            limit: '10',
            ni_id: '138'
          }
          news_list(data).then(res => {
            if (res.code == 1) {
              this.list = this.list.concat(res.data.page_data);
              this.cur_page = res.data.page_now;
              for (let i in this.list) {
                this.list[i].isCheck = false;
              }
            }
          })
        }
      }, 1000);
    },
    toDetail(id) {
      this.$router.push({name: "chat_detail", params: {id: id}});
    },
    manageList(item, index) {
      if (this.list[index].isCheck) {
        this.m_list.push(item.id);
      } else {
        this.m_list = this.m_list.filter(items => {
          return items != item.id;
        })
      }
    },
    delNews() {
      showConfirmDialog({
        message: '确定要删除这些消息？',
      }).then(() => {
        let data = {
          ni_id: this.m_list
        }
        news_del(data).then(res=>{
          if(res.code == 1) {
            showSuccessToast('删除成功！');
            this.cur_page = 1;
            this.getMessageList();
          } else {
            showFailToast(res.msg);
          }
        })
      }).catch(() => {
            // on cancel
      });
    }
  },
  mounted() {
    this.getMessageList();
    // this.onload();
  }
}
</script>
<style lang="scss" scoped>
.message-list {
  width: 100%;
  padding: 15px;

  li {
    position: relative;
    width: 100%;
    background: #fff;
    border-radius: 5px;
    box-shadow: 0 1.5px 5px rgba(0, 0, 0, 0.16);
    padding: 10px;

    .dot {
      position: absolute;
      right: 0;
      top: -2px;
      width: 12px;
      height: 12px;
      border-radius: 50%;
      background: #FF0000;
    }

    & + li {
      margin-top: 10px;
    }

    .message-title {
      font-size: 16px;
      color: #222;
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
    }

    .message-time {
      margin-top: 5px;
      font-size: 14px;
      color: #818181;
    }

    .message-content {
      margin-top: 5px;
      font-size: 14px;
      color: #434343;

      &.line-two {
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        text-overflow: ellipsis;
        overflow: hidden;
      }
    }

    .message-more {
      margin-top: 5px;
      display: flex;
      justify-content: space-between;
      cursor: pointer;
      color: #8E8E8E;

      span {
        font-size: 12px;
        color: #8E8E8E;
      }
    }

    .message-check {
      position: absolute;
      right: 10px;
      top: 10px;
    }
  }
}

.del-btn {
  color: #fff;

  &.disabled {
    color: #ccc;
    pointer-events: none;
  }
}
</style>
