<template>
  <view class="msg-box">
    <Nav title="留言反馈" :left-show="false"  titleColor="#fff"></Nav>
    <view class="mui-content">
      <view class="ln-public">
        留言内容
      </view>
      <view class="msg-text" contenteditable="true" placeholder="在此输入留言内容，限200字。"></view>
      <view class="msg-btn" @click="sendMsg">提交</view>
      <view class="msg-list">
        <view class="msg-item" v-for="(msgItem,index) in list">
          <view class="msg-list-con">
            <view class="msg-list-tit">
              <view class="msg-list-name">
               {{msgItem.username}}
              </view>
              <view class="msg-list-time">{{msgItem.created_at}}</view>
            </view>
            <view class="msg-list-intro">{{msgItem.content}}</view>
          </view>
          <view class="msg-list-con" v-if="msgItem.reply!==''">
            <view class="msg-list-tit">
              <view class="msg-list-name">
                回复
              </view>
              <view class="msg-list-time">{{msgItem.updated_at}}</view>
            </view>
            <view class="msg-list-intro">{{msgItem.reply}}</view>
          </view>
        </view>
      </view>
      <en-blank v-if="list.length<=0"></en-blank>
    </view>
  </view>
</template>

<script>
import $ from 'jquery/src/jquery'
import {addMsg, getMsgList} from "@/api/news";
import tools from "@/common/js/tools";
import EnBlank from "@/components/en-utils/en-blank/en-blank";
export default {
  name: "index",
  components: {EnBlank},
  props: {},
  data() {
    return {
      isAjax:false,
      page:1,
      list:[],
      total:null
    }
  },
  watch: {},
  mounted() {
    this.startList()
  },
  onReachBottom() {
    this.getMsgList()
  },
  methods: {
    startList(){
      this.page=1;
      this.list=[];
      this.total=null;
      this.getMsgList()
    },
    getMsgList(){
      if(this.isAjax){
        return;
      }
      if(this.total!==null && this.total>=this.list.length){
        return;
      }
      getMsgList({'page':this.page}).then((res)=>{
        if(res.code===1){
          this.total=res.data.total
          this.list.push(...res.data.items)
          ++this.page
        }
      })
    },
    sendMsg(){
      let msg = $('.msg-text').text();
      if(msg===''){
        tools.error('请输入留言内容')
        return;
      }
      if(this.isAjax){
        return;
      }
      this.isAjax=true
      addMsg({'content':msg}).then((res)=>{
        this.isAjax=false
        if(res.code===1){
            this.list.unshift(res.data)
          $('.msg-text').text('');
            tools.success(res.msg)
        }else {
          tools.error(res.msg)
        }
      }).catch((e)=>{
        this.isAjax=false
      })
    }
  }
}
</script>

<style scoped lang="scss">
.msg-box{
  background: #131E30;
  min-height: calc(100vh - 50px);
  .mui-content{
    padding:0 10px;
    .ln-public {
      width: 100%;
      padding: 0 10px;
      font-size: 20px;
      line-height: 28px;
      font-weight: 600;
      margin: 20px auto;
      color: #D9E4F6;
      word-break: break-all;
    }
    .msg-text {
      width: calc(100% - 20px);
      min-height: 98px;
      line-height: 24px;
      color: #D9E4F6;
      padding: 10px 0;
      background: #061623;
      border: 1px solid rgba(39,60,83,0.5);
      margin: 10px;
    }
    .msg-btn {
      display: block;
      width: 82%;
      height: 42px;
      line-height: 42px;
      border: 0;
      border-radius: 3px;
      text-align: center;
      padding: 0;
      font-size: 16px;
      background: #007AFF;
      color: #FFFFFF;
      margin: 15px auto;
      margin-top: 60px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .msg-list {
      width: calc(100% - 20px);
      line-height: 20px;
      padding: 0 10px;
      border-top: 1px solid rgba(39,60,83,0.5);
      list-style-type: none;
      margin: 0;
      -webkit-padding-start: 0;
      .msg-item{
        padding: 2px 0;
        border-bottom: 1px solid rgba(39,60,83,0.5);
        color: #D9E4F6;
        .msg-list-con {
          width: 100%;
          overflow: auto;
          margin: 15px 0;
          .msg-list-tit {
            width: 100%;
            height: 30px;
            line-height: 30px;
            .msg-list-name {
              max-width: calc(100% - 120px);
              height: 30px;
              float: left;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
              color: #D9E4F6;
            }
            .msg-list-time {
              max-width: 130px;
              height: 30px;
              font-size: 12px;
              color: #495B73;
              text-align: right;
              float: right;
              overflow: hidden;
            }
          }
          .msg-list-intro {
            width: 100%;
            line-height: 20px;
            color: #495B73;
            text-align: justify;
          }
        }
      }
    }

  }
}
</style>