<template lang="html">
  <div class="userManageVue">
    <div class="title">
      <input type="text" name="" value="" class="searchInput" spellcheck="false" placeholder="搜索用户" v-model="username" @keydown.enter="getUserList()">
      <div class="right">
        <button type="button" class="btn1" @click="popup.userAdd = true" v-if="userInfo.lv>=3">新建用户</button>
        <button type="button" class="btn1" @click="del()" v-if="userInfo.lv>=3">删除用户</button>
      </div>
    </div>
    <div class="table">
      <table>
        <tr>
          <td>编号</td>
          <td>用户名</td>
          <td>密码</td>
          <td>等级</td>
          <td>最后一次登录</td>
          <td>创建时间</td>
          <td>创建者</td>
          <td v-if="userInfo.lv>=3">修改用户</td>
          <td v-if="userInfo.lv>=3">标记</td>
        </tr>
        <tr v-for="n,i in userData.data">
          <td>{{ i + 1 + (page - 1) * 10 }}.</td>
          <td>
            <p class="lengthLimit" :title="n.username">{{ n.username }}</p>
          </td>
          <td>{{ n.password }}</td>
          <td>{{ n.lv }}</td>
          <td>{{ n.lastLogin | format1 }}</td>
          <td>{{ n.createDate | format1 }}</td>
          <td>{{ n.creator }}</td>
          <td v-if="userInfo.lv>=3">
            <button type="button" class="btn2" @click="popup.userEdit = true, childrenData = n" v-if="n.personId !== userInfo.personId">修改</button>
          </td>
          <td v-if="userInfo.lv>=3">
            <label :for="'checkbox' + i" class="checkbox" v-if="n.personId !== userInfo.personId">
              <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>
          <td v-if="userInfo.lv>=3"><!-- 修改用户 --></td>
          <td v-if="userInfo.lv>=3"><!-- 标记 --></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> / 总共 {{ userData.pageCount || 1 }} 页
      </div>
    </div>

    <!-- 删除用户 -->
    <div class="mask" v-if="popup.userDelete">
      <user-delete @close="popup.userDelete = false" @update="getUserList()" :checked-users="checkedUsers" @clear-checkbox="clearCheckbox()" />
    </div>

    <!-- 新建用户 -->
    <div class="mask" v-if="popup.userAdd">
      <user-add @close="popup.userAdd = false" @update="getUserList()" :userInfo="userInfo" />
    </div>

    <!-- 修改用户 -->
    <div class="mask" v-if="popup.userEdit">
      <user-edit @close="popup.userEdit = false" @update="getUserList()" :children-data="childrenData" />
    </div>

  </div>
</template>

<script>
import moment from 'moment'
import { bus, requestGetPerson } from '@/vuex/request.js'
import { getPageList } from '@/assets/js/get-page-list.js'
import userDelete from '@/components/module/userDelete'
import userAdd from '@/components/module/userAdd'
import userEdit from '@/components/module/userEdit'

export default {
  components: {
    userDelete,
    userAdd,
    userEdit
  },
  data () {
    return {
      page: 0,
      pages: 7,
      pageList: [1],
      checkbox: [false, false, false, false, false, false, false, false, false, false],
      popup: {
        userDelete: false,
        userAdd: false,
        userEdit: false
      },
      username: '',
      userData: {},
      freeList: 10,
      pageSize: 10,
      childrenData: {},
      checkedUsers: []
    }
  },
  props: ['userInfo'],
  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.userDelete = true
        this.checkedUsers = []
        this.checkbox.forEach((n, i) => {
          if (n) {
            this.checkedUsers.push(this.userData.data[i])
          }
        })
      }
    },
    getUserList () {
      let pageSize = this.pageSize
      let data = {
        pageNo: this.page,
        pageSize: pageSize,
        username: this.username,
        lv: this.userInfo.lv
      }
      requestGetPerson({}, data, (response) => {
        if (response.data) {
          if (response.data.state) {
            this.userData = 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.getUserList()
    }
  },
  filters: {
    format1 (timestemp) {
      return moment(timestemp).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>

<style lang="scss" scoped>
.userManageVue{
  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>
