<template lang="html">
  <div class="fieldTableInfoVue">
    <div class="title">
      <input type="text" name="" value="" class="searchInput" spellcheck="false" placeholder="渠道" v-model="pt" @keydown.enter="getFields()">
      <div class="right">
        <button type="button" class="btn1" @click="popup.fieldAdd = true">新建</button>
        <button type="button" class="btn1" @click="del()">删除</button>
      </div>
    </div>
    <div class="table">
      <table>
        <tr>
          <td>编号</td>
          <td>数据库名</td>
          <td>表名</td>
          <td>字段长度</td>
          <td>所属渠道</td>
          <td>详细信息</td>
          <td>标记</td>
        </tr>
        <tr v-for="n,i in fieldData.data">
          <td>{{ i + 1 + (page - 1) * 10 }}.</td>
          <td>
            <p class="lengthLimit" :title="n.dbName">{{ n.dbName }}</p>
          </td>
          <td>{{ n.collection }}</td>
          <td>{{ n.fieldSize }}</td>
          <td>{{ n.pt }}</td>
          <td>
            <button type="button" class="btn2" @click="popup.fieldDetails = true, childrenData = n">详情</button>
          </td>
          <td>
            <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><!-- 详细信息 --></td>
          <td><!-- 标记 --></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> / 总共 {{ fieldData.pageCount || 1 }} 页
      </div>
    </div>

    <!-- 检测模板详情 -->
    <div class="mask" v-if="popup.fieldDetails">
      <field-details @close="popup.fieldDetails = false" :children-data="childrenData"  @update="getFields()" :channel-msg-data="channelMsgData" />
    </div>

    <!-- 删除模板 -->
    <div class="mask" v-if="popup.fieldDelete">
      <field-delete @close="popup.fieldDelete = false" :children-data="childrenData"  @update="getFields()" :checked-field="checkedField" @clear-checkbox="clearCheckbox()" />
    </div>

    <!-- 新建模板 -->
    <div class="mask" v-if="popup.fieldAdd">
      <field-add @close="popup.fieldAdd = false" :userInfo="userInfo" @update="getFields()" :channel-msg-data="channelMsgData" />
    </div>

  </div>
</template>

<script>
import fieldDetails from '@/components/module/fieldDetails'
import fieldDelete from '@/components/module/fieldDelete'
import fieldAdd from '@/components/module/fieldAdd'
import { bus, requestGetFields, requestGetDisChannel } from '@/vuex/request.js'
import { getPageList } from '@/assets/js/get-page-list.js'

export default {
  components: {
    fieldDetails,
    fieldDelete,
    fieldAdd
  },
  data () {
    return {
      page: 0,
      pages: 7,
      pageList: [1],
      popup: {
        fieldDetails: false,
        fieldDelete: false,
        fieldAdd: false
      },
      checkbox: [false, false, false, false, false, false, false, false, false, false],
      pt: '',
      fieldData: {},
      freeList: 10,
      pageSize: 10,
      childrenData: {},
      checkedField: [],
      channelMsgData: []
    }
  },
  props: ['userInfo'],
  mounted () {
    this.page = 1
    this.getDisChannel()
  },
  methods: {
    getDisChannel () {
      requestGetDisChannel((response) => {
        if (response.data) {
          if (response.data.state) {
            this.channelMsgData = response.data.data
          } else {
            bus.$emit('alert', response.data.message)
          }
        } else {
          bus.$emit('alert', response + '.')
        }
      })
    },
    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.fieldDelete = true
        this.checkedField = []
        this.checkbox.forEach((n, i) => {
          if (n) {
            this.checkedField.push(this.fieldData.data[i])
          }
        })
      }
    },
    getFields () {
      let pageSize = this.pageSize
      let data = {
        pageNo: this.page,
        pageSize: pageSize,
        pt: this.pt
      }
      requestGetFields({}, data, (response) => {
        if (response.data) {
          if (response.data.state) {
            this.fieldData = 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.getFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.fieldTableInfoVue{
  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;
      button:nth-of-type(n + 2){
        margin-left: 10px;
      }
    }
  }
}
.pageList{
  margin-top: 30px;
  margin-bottom: 10px;
}
</style>
