<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.query"
        placeholder="请输入检索词"
        style="width: 60%;"
        class="filter-item"
        @keyup.enter.native="fetchData()"
      />
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="fetchData">
        检索
      </el-button>
      <el-button
        class="filter-item"
        style="margin: 0 10px 20px 0; float: right;"
        type="success"
        icon="el-icon-circle-plus-outline"
        @click="handleCreate"
      >
        新建
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column type="index" label="序号" align="center" fixed width="60" />
      <el-table-column label="工号" fixed align="center">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="昵称" fixed align="center">
        <template slot-scope="{row}">
          <span>{{ row.nickName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center">
        <template slot-scope="{row}">
          <span>{{ row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" width="60">
        <template slot-scope="{row}">
          <span>{{ row.gender | genderFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后登录时间" align="center" width="180">
        <template slot-scope="{row}">
          <span>{{ row.lastLoginTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" width="180">
        <template slot-scope="{row}">
          <span>{{ row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="280">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            更新
          </el-button>
          <el-button type="danger" size="mini" @click="resetPassword(row)">
            重置密码
          </el-button>
          <el-button v-if="row.isFrozen" size="mini" type="info" @click="handleFrozen(row)">
            解除冻结
          </el-button>
          <el-button v-else size="mini" type="danger" @click="handleFrozen(row)">
            冻结
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="fetchData"
    />

    <el-dialog :title="textMapping[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="params" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">
        <el-form-item label="工号" prop="name">
          <el-input v-model="params.name" placeholder="请输入工号" />
        </el-form-item>
        <el-form-item v-if="dialogStatus==='CREATE'" label="密码" prop="password">
          <el-input v-model="params.password" type="password" :show-password="true" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="params.nickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="params.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="params.gender">
            <el-option v-for="item in genders" :key="item.value" :label="item.name" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="头像" prop="profilePic">
          <single-image :value="params.profilePic" :show-preview="true" style="width: 100%" @success="uploadFile" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='CREATE'?createData():updateData()">
          提交
        </el-button>
      </div>
    </el-dialog>

    <el-dialog title="重置用户密码" :visible.sync="resetPasswordDialog">
      <el-form ref="dataForm" :rules="resetPasswordRules" :model="resetPasswordParams" label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="resetPasswordParams.newPassword " type="password" :show-password="true" placeholder="请输入密码" style="width: 60%" />
        </el-form-item>
        <el-form-item label="确认新密码" prop="newPasswordSecond">
          <el-input v-model="resetPasswordParams.newPasswordConfirm " type="password" :show-password="true" placeholder="请输入密码" style="width: 60%" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetPasswordDialog = false">
          取消
        </el-button>
        <el-button type="primary" @click="submitResetPassword">
          提交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import SingleImage from '@/components/Upload/SingleImage'
import { getUsers, changeUserFrozen, resetUserPassword, createUser, updateUser } from '@/api/user'

const genderMapping = { 0: '未知', 1: '男', 2: '女' }

export default {
  name: 'UserList',
  components: { Pagination, SingleImage },
  filters: {
    genderFilter(value) {
      return genderMapping[value]
    }
  },
  data() {
    return {
      tableKey: 0,
      listQuery: {
        query: '',
        pageNum: 1,
        pageSize: 20
      },
      list: [],
      rules: {
        name: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      },
      resetPasswordRules: {
        newPassword: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
        newPasswordConfirm: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      },
      listLoading: true,
      total: 0,
      dialogStatus: '',
      dialogFormVisible: false,
      params: {},
      profilePic: '',
      resetPasswordParams: {},
      resetPasswordDialog: false,
      genders: [{ value: 0, name: '未知' }, { value: 1, name: '男' }, { value: 2, name: '女' }],
      textMapping: { 'UPDATE': '更新', 'CREATE': '创建' }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getUsers(this.listQuery).then(res => {
        this.list = res.data.list
        this.total = res.data.count
        this.listLoading = false
      }).catch(res => {
        this.listLoading = false
        this.$message.error(res.data.message)
      })
    },
    handleCreate() {
      this.dialogStatus = 'CREATE'
      this.params = {}
      this.$set(this.params, 'gender', 0)
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      this.dialogStatus = 'UPDATE'
      this.params = row
      this.dialogFormVisible = true
    },
    handleFrozen(row) {
      this.listLoading = true
      changeUserFrozen(row.id).then(res => {
        this.fetchData()
        this.$message.success('提交成功')
      }).catch(res => {
        this.fetchData()
        this.$message.error(res.data.message)
      })
    },
    createData() {
      this.params.gender = this.gender
      createUser(this.params).then(res => {
        this.fetchData()
        this.$message.success('提交成功')
        this.dialogFormVisible = false
      }).catch(res => {
        this.fetchData()
        this.$message.error(res.data.message)
        this.dialogFormVisible = false
      })
    },
    updateData() {
      this.params.gender = this.gender
      updateUser(this.params.id, this.params).then(res => {
        this.fetchData()
        this.$message.success('提交成功')
        this.dialogFormVisible = false
      }).catch(res => {
        this.fetchData()
        this.$message.error(res.data.message)
        this.dialogFormVisible = false
      })
    },
    uploadFile(data) {
      this.$set(this.params, 'profilePic', data.fileUrl)
    },
    resetPassword(row) {
      this.resetPasswordParams = { id: row.id }
      this.resetPasswordDialog = true
    },
    submitResetPassword() {
      if (this.resetPasswordParams.newPassword !== this.resetPasswordParams.newPasswordConfirm) {
        this.$message.error('两次输入的密码不一致')
        return
      }
      resetUserPassword(this.resetPasswordParams.id, this.resetPasswordParams.newPassword).then(res => {
        this.fetchData()
        this.$message.success('提交成功')
        this.resetPasswordDialog = false
      }).catch(res => {
        this.fetchData()
        this.$message.error(res.data.message)
        this.resetPasswordDialog = false
      })
    }
  }
}
</script>

<style scoped>

</style>
