<template>
  <div class="user-manage">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" inline>
        <el-form-item label="登录名前缀：">
          <el-input
            style="width: 180px;"
            v-model.trim="filter.loginNameStartWith"
            placeholder="登录名前缀"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="角色：">
          <el-select
            v-model="filter.role"
            style="width: 142px;"
            placeholder="角色"
            filterable
            clearable
          >
            <el-option
              v-for="item in roles"
              :key="item.code"
              :value="item.code"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label-width="0px">
          <el-button type="primary" @click="toPage(1)">查询</el-button>
          <el-button v-if="checkPrivilege('add')" type="success" @click="toAdd"
            >新增</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <div class="part-box part-box-pad">
      <el-table ref="TableList" :data="users">
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="loginName" label="用户名"></el-table-column>
        <el-table-column prop="name" label="姓名"></el-table-column>
        <el-table-column prop="roleNames" label="角色"> </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="170">
          <span slot-scope="scope">{{
            scope.row.createTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column prop="updateTime" label="修改时间" width="170">
          <span slot-scope="scope">{{
            scope.row.updateTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="160px"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              v-if="checkPrivilege('edit')"
              class="btn-primary"
              type="text"
              @click="toEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              v-if="checkPrivilege('resetPwd')"
              class="btn-danger"
              type="text"
              @click="toResetPwd(scope.row)"
              >重置密码</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="total,prev, pager, next"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
        >
        </el-pagination>
      </div>
    </div>

    <!-- ModifyUser -->
    <modify-user
      v-if="checkPrivilege('edit') || checkPrivilege('add')"
      ref="ModifyUser"
      :instance="curUser"
      :roles="roles"
      @modified="getList"
    ></modify-user>
  </div>
</template>

<script>
import ModifyUser from "../components/ModifyUser";
import { userListPage, userRoleList, userInsertOrUpdate } from "../api";

export default {
  name: "user-manage",
  components: { ModifyUser },
  data() {
    return {
      filter: {
        loginNameStartWith: "",
        role: ""
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      roles: [],
      roleType: {},
      users: [],
      curUser: {},
      loading: false
    };
  },
  mounted() {
    this.initData();
  },
  methods: {
    async initData() {
      await this.getRoleList();
      this.toPage(1);
    },
    async getRoleList() {
      const data = await userRoleList();
      this.roles = data || [];
      let roleType = {};
      this.roles.forEach(item => {
        roleType[item.code] = item.name;
      });
      this.roleType = roleType;
    },
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size
      };
      const data = await userListPage(datas);
      this.users = data.records.map(item => {
        item.roleNames = item.role.map(code => this.roleType[code]).join("，");
        return item;
      });
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toEdit(row) {
      this.curUser = row;
      this.$refs.ModifyUser.open();
    },
    toAdd() {
      this.curUser = {};
      this.$refs.ModifyUser.open();
    },
    async toResetPwd(row) {
      await userInsertOrUpdate({ id: row.id, password: "123456" });
      this.$message.success("密码重置成功！");
    }
  }
};
</script>
