<template>
  <div id="root">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="用户姓名">
        <el-input v-model="formInline.userName" placeholder="用户姓名"/>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="formInline.loginName" placeholder="手机号"/>
      </el-form-item>
      <el-form-item label="身份证号">
        <el-input v-model="formInline.idCard" placeholder="身份证号"/>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="clear">清空</el-button>
        <el-button type="primary" @click="searchData">查询</el-button>
      </el-form-item>
    </el-form>
    <div class="button-div">
      <el-button type="warning" @click="blacklistUser">拉黑</el-button>
    </div>
    <el-table
      ref="multipleTable"
      :data="users"
      border
      tooltip-effect="dark"
      style="width: 100%;margin-top: 20px;"
      @selection-change="handleSelectionChange">
      <el-table-column
        align="center"
        type="selection"
        width="55"/>
      <el-table-column
        align="center"
        prop="userName"
        label="姓名"/>
      <el-table-column
        align="center"
        prop="loginName"
        label="手机号"
        width="120"/>
      <el-table-column
        align="center"
        prop="idCard"
        label="身份证号"/>
      <el-table-column
        align="center"
        prop="gender"
        label="性别">
        <template slot-scope="scope">{{ scope.row.gender === '1' ? '男' : '女' }}</template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="status"
        label="账号状态">
        <template slot-scope="scope">{{ scope.row.status === '2' ? '黑名单' : '正常' }}</template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="createDate"
        label="注册时间">
        <template slot-scope="scope">{{ formatDate(scope.row.createDate) }}</template>
      </el-table-column>
      <el-table-column
        align="center"
        label="日志数据">
        <template slot-scope="scope">
          <el-button type="primary" @click="lookUserLog(scope.row.id)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="footer-page">
      <el-pagination
        :current-page="formInline.pageNum"
        :page-sizes="[10, 20, 40, 100]"
        :page-size="formInline.pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>

<script>
export default {
  name: 'UserManagement',
  data() {
    return {
      formInline: {
        userName: '', // 用户名
        loginName: '', // 手机号
        idCard: '', // 身份证号
        pageNum: 1,
        pageSize: 10
      },
      total: 0, // 总数
      users: [], // 用户数据
      ids: [] // 选中的id 集合
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    clear() { // 清除
      this.formInline.userName = ''
      this.formInline.loginName = ''
      this.formInline.idCard = ''
      this.formInline.pageNum = 1
      this.formInline.pageSize = 10
      this.getData()
    },
    handleSelectionChange(val) { // 选中
      this.ids = []
      val.map((item, index) => {
        this.ids.push(item.id)
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
    },
    searchData() { // 搜索
      this.formInline.pageNum = 1
      this.formInline.pageSize = 10
      this.getData()
    },
    getData() {
      this.$store.dispatch('GetTaUserAll', this.formInline).then((res) => {
        const resCode = res.code
        if (resCode === '0') {
          const resData = res.data
          this.users = resData.item
          this.total = resData.total
          this.formInline.pageNum = resData.pageNum
          this.formInline.pageSize = resData.pageSize
        }
      }).catch(() => {
        console.log('GetTaUserAll error')
      })
    },
    formatDate(val) {
      if (val === null) {
        return
      }
      var value = new Date(val)
      var year = value.getFullYear()
      var month = value.getMonth() + 1
      var day = value.getDate()
      // var hour = value.getHours()
      // var minutes = value.getMinutes()
      // var seconds = value.getSeconds()
      // return year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds
      return year + '-' + month + '-' + day
    },
    lookUserLog(id) { // 跳转查看用户日志
      this.$router.push({ name: 'user-log', query: { id: id }})
    },
    blacklistUser() { // 拉黑移动端用户
      this.$store.dispatch('BlacklistUser', this.ids).then((res) => {
        const resCode = res.code
        if (resCode === '1') {
          this.$message.error(res.message)
          return
        }
        this.$message.success(res.message)
        this.getData()
      }).catch(() => {
        console.log('BlacklistUser error')
      })
    }
  }
}
</script>

<style scoped>
.demo-form-inline {
  margin-left: 20px;
  margin-top: 20px;
}
.button-div {
  margin-left: 20px;
}
.footer-page {
  display: flex;
  justify-content: flex-end;
}
</style>
