<template lang="html">
  <div class="checkInfoVue">
    <div class="title">
      <input type="text" name="" value="" class="searchInput" spellcheck="false" placeholder="模板名" v-model="tempName" @keydown.enter="findAllMsg()">
      <div class="right">
        <button type="button" class="btn1" @click="del()" v-if="userInfo.lv>=2">删除信息</button>
      </div>
    </div>
    <div class="table">
      <table>
        <tr>
          <td>编号</td>
          <td>模板名</td>
          <td>渠道</td>
          <td>产生时间</td>
          <td>检测信息</td>
          <td v-if="userInfo.lv>=2">详细信息</td>
          <td v-if="userInfo.lv>=2">标记</td>
        </tr>
        <tr v-for="n,i in MsgData.data" :class="'lv-' + n.templateLv">
          <td>{{ i + 1 + (page - 1) * 10 }}.</td>
          <td>
            <p class="lengthLimit-new">{{ n.templateName }}</p>
          </td>
          <td>网媒</td>
          <td>{{ n.handleDate | format1 }}</td>
          <td><span class="maxLen" :title="n.errorMsg">{{ n.errorMsg }}</span></td>
          <td v-if="userInfo.lv>=2">
            <button type="button" class="btn2" @click="popup.checkInfoDetails = true, childrenData = n">详情</button>
          </td>
          <td v-if="userInfo.lv>=2">
            <label :for="'checkbox' + i" class="checkbox">
              <input type="checkbox" name="" value="" :id="'checkbox' + i" v-model="checkbox[i]">
              <span></span>
            </label>
          </td>
        </tr>
        <tr class="empty" v-for="n,i in freeList">
            <td><!-- 编号 --></td>
            <td><!-- 模板名 --></td>
            <td><!-- 渠道 --></td>
            <td><!-- 产生时间 --></td>
            <td><!-- 检测信息 --></td>
            <td v-if="userInfo.lv>=2"><!-- 详细信息 --></td>
            <td v-if="userInfo.lv>=2"><!-- 标记 --></td>
        </tr>
      </table>
      <div class="pageList">
        <div @click="page = page - 1 < 1 ? 1 : page - 1">Provious</div>
        <div @click="page = n" :class="{ 'active': page === n }" v-for="n,i in pageList">{{ n }}</div>
        <div @click="page = page + 1 > pages ? pages : page + 1">Next</div> / 总共 {{ MsgData.pageCount || 1 }} 页
      </div>
    </div>

    <!-- 检测信息详情 -->
    <div class="mask" v-if="popup.checkInfoDetails">
      <check-info-details @close="popup.checkInfoDetails = false" :children-data="childrenData" @update="findAllMsg()" :userInfo="userInfo" :pt-selected-data="ptSelectedData" />
    </div>

    <!-- 删除信息 -->
    <div class="mask" v-if="popup.checkInfoDelete">
      <check-info-delete @close="popup.checkInfoDelete = false" :checked-message="checkedMessage" @update="findAllMsg()" @clear-checkbox="clearCheckbox()" :pt-selected-data="ptSelectedData" />
    </div>

  </div>
</template>

<script>
import checkInfoDetails from '@/components/module/checkInfoDetails'
import checkInfoDelete from '@/components/module/checkInfoDelete'
import { bus, requestFindAllMsg } from '@/vuex/request.js'
import { getPageList } from '@/assets/js/get-page-list.js'
import moment from 'moment'

export default {
  components: {
    checkInfoDetails,
    checkInfoDelete
  },
  data () {
    return {
      page: 0,
      pages: 7,
      pageList: [1],
      popup: {
        checkInfoDetails: false,
        checkInfoDelete: false
      },
      checkbox: [false, false, false, false, false, false, false, false, false, false],
      tempName: '',
      MsgData: {},
      freeList: 10,
      pageSize: 10,
      childrenData: {},
      checkedMessage: []
    }
  },
  props: ['userInfo', 'ptSelectedData'],
  mounted () {
    this.page = 1
  },
  methods: {
    clearCheckbox () {
      this.checkbox = [false, false, false, false, false, false, false, false, false, false]
    },
    del () {
      if (!this.checkbox.some((n, i) => { return n === true })) {
        bus.$emit('alert', '您没有选择任何选项！')
      } else {
        this.popup.checkInfoDelete = true
        this.checkedMessage = []
        this.checkbox.forEach((n, i) => {
          if (n) {
            this.checkedMessage.push(this.MsgData.data[i])
          }
        })
      }
    },
    findAllMsg () {
      let pageSize = this.pageSize
      let data = {
        pageNo: this.page,
        pageSize: pageSize,
        tempName: this.tempName,
        pt: this.ptSelectedData.pt
      }
      requestFindAllMsg({}, data, (response) => {
        if (response.data) {
          if (response.data.state) {
            this.MsgData = response.data.data
            this.pages = response.data.data.pageCount
            this.pageList = getPageList({
              displayCount: 7,  // 显示的页面总数（最大值）
              page: Math.max(this.page, 1),    // 当前页面
              pages: Math.max(response.data.data.pageCount, 1)    // 总页数
            })
            this.freeList = this.page === response.data.data.pageCount ? pageSize - (response.data.data.dataCount % pageSize === 0 ? pageSize : response.data.data.dataCount % pageSize) : 0
            if (!response.data.data.data.length) {
              this.freeList = 10
            }
          } else {
            bus.$emit('alert', response.data.message)
          }
        } else {
          bus.$emit('alert', response + '.')
        }
      })
    }
  },
  watch: {
    page (val, oldVal) {
      this.findAllMsg()
    },
    ptSelectedData (val, oldVal) {
      this.findAllMsg()
    }
  },
  filters: {
    format1 (timestemp) {
      return moment(timestemp).format('YYYY-MM-DD hh:mm')
    }
  }
}
</script>

<style lang="scss" scoped>
.checkInfoVue{
  padding: 0 50px;
  max-width: 1600px;
  margin: auto;
  .title{
    padding: 15px 0;
    .right{
      float: right;
      height: 100%;
      vertical-align: middle;
      text-align: right;
      line-height: 36px;
    }
  }
}
.maxLen{
  display: inline-block;
  max-width: 410px;
  line-height: 1;
  overflow: hidden;
  text-overflow:ellipsis;
  white-space: nowrap;
}
.pageList{
  margin-top: 30px;
  margin-bottom: 10px;
}
tr:not(:first-child){
  td:first-child:before{
    display: none;
    content: "";
    position: absolute;
    left: 0;
    top: 50%;
    line-height: 1;
    font-size: 12px;
    padding: 4px;
    border-radius: 10px;
    background-color: #000;
    color: #fff;
    transform: translate(-70%, -50%);
    box-shadow: 0 0 10px rgba(0, 0, 0, .7), -2px 3px 5px rgba(255, 255, 255, .5) inset;
  }
  &.lv-1 td:first-child:before{
    display: block;
    content: "紧急";
    background-color: red;
  }
  &.lv-2 td:first-child:before{
    display: block;
    content: "略急";
    background-color: yellow;
  }
  &.lv-3 td:first-child:before{
    display: block;
    content: "不急";
    background-color: green;
  }
}
</style>
