<template>
  <el-row>
    <el-col style="margin-bottom: 1rem;">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/admin/dashboard' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>
    <el-col>
      <el-table
        :data="users"
        style="width: 100%">
        <el-table-column
          label="#"
          width="40"
          prop="idUser">
        </el-table-column>
        <el-table-column
          label="头像"
          width="60"
          prop="avatarUrl">
          <template slot-scope="scope">
            <el-avatar v-if="scope.row.avatarUrl" size="medium" :src="scope.row.avatarUrl"></el-avatar>
            <el-avatar v-else size="medium" src="https://rymcu.com/article/1578475481946.png"></el-avatar>
          </template>
        </el-table-column>
        <el-table-column
          label="昵称"
          width="180"
          prop="nickname">
          <template slot-scope="scope">
            <el-link rel="nofollow" type="primary" @click="onRouter('user', scope.row.nickname)" :underline="false">{{
              scope.row.nickname }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column
          label="账号"
          width="180"
          prop="account">
        </el-table-column>
        <el-table-column
          label="注册时间"
          width="180"
          prop="createdTime">
        </el-table-column>
        <el-table-column
          label="状态"
          width="180"
          prop="status">
          <template slot-scope="scope">
            <el-tag type="primary" disable-transitions>
              {{scope.row.status === '0' ? '正常' : '禁用'}}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleRole(scope.$index, scope.row)">授权</el-button>
            <el-button v-if="scope.row.status == 1" size="mini" type="primary"
                       @click="toggleStatus(scope.$index, scope.row)">启用
            </el-button>
            <el-button v-else size="mini" type="danger" @click="toggleStatus(scope.$index, scope.row)">禁用</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-col>
    <el-col>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagination.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pagination.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total">
      </el-pagination>
    </el-col>
    <el-col>
      <el-dialog :visible.sync="dialogVisible">
        <el-form>
          <el-form-item>
            <el-radio-group v-model="idRole">
              <el-radio v-for="role in roles" :key="role.idRole" border :label="role.idRole">{{ role.name }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateRole">确 定</el-button>
        </div>
      </el-dialog>
    </el-col>
  </el-row>
</template>

<script>
  import {mapState} from 'vuex';

  export default {
    name: "users",
    fetch({store, params, error}) {
      return Promise.all([
        store
          .dispatch('admin/fetchUsers', params)
          .catch(err => error({statusCode: 404})),
        store.dispatch("admin/fetchRoles")
      ])
    },
    computed: {
      ...mapState({
        users: state => state.admin.user.users,
        pagination: state => state.admin.user.pagination,
        roles: state => state.admin.role.roles
      })
    },
    data() {
      return {
        order: 'desc',
        idRole: 0,
        idUser: 0,
        dialogVisible: false
      }
    },
    methods: {
      onRouter(name, data) {
        this.$router.push({
          path: `/${name}/${data}`
        })
      },
      handleRole(index, user) {
        let _ts = this;
        _ts.$set(_ts, 'idUser', users.idUser);
        _ts.$axios.$get('/api/admin/user/' + users.idUser + '/role')
          .then(function (res) {
            _ts.$set(_ts, 'dialogVisible', true);
            _ts.$set(_ts, 'idRole', res[0].idRole);
          });
      },
      toggleStatus(index, user) {
        let _ts = this;
        let title, status;
        if (users.status == 0) {
          title = '禁用';
          status = 1;
        } else {
          title = '启用';
          status = 0;
        }
        _ts.$confirm('确定' + title + '用户' + users.nickname + '(' + users.account + ')?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          _ts.$axios.$patch('/api/admin/user/update-status', {
            idUser: users.idUser,
            status: status
          }).then(function (res) {
            if (res && res.message) {
              _ts.$message.error(res.message);
            } else {
              _ts.$message({
                type: 'success',
                message: title + '成功!'
              });
              _ts.handleCurrentChange(1);
            }
          });
        }).catch(() => {
          _ts.$message({
            type: 'info',
            message: '已取消'
          });
        });
      },
      handleSizeChange(page) {
        let _ts = this;
        _ts.$store.dispatch('admin/fetchUsers', {
          page: page
        })
      },
      handleCurrentChange(page) {
        let _ts = this;
        _ts.$store.dispatch('admin/fetchUsers', {
          page: page
        })
      },
      updateRole() {
        let _ts = this;
        let data = {
          idUser: _ts.idUser,
          idRole: _ts.idRole
        };
        _ts.$axios.$patch('/api/admin/user/update-role', data).then(function (res) {
          if (res && res.message) {
            _ts.$message.error(res.message);
          } else {
            _ts.$message({
              message: '授权成功',
              type: 'success'
            });
            _ts.$set(_ts, 'dialogVisible', false);
            _ts.$set(_ts, 'idUser', 0);
            _ts.$set(_ts, 'idRole', 0);
          }
        })
      }
    },
    mounted() {
      this.$store.commit("setActiveMenu", "admin-user");
    }
  }
</script>

<style scoped>

</style>
