<template>
  <div>
    <el-form :model="form" inline>
      <!-- <el-form-item v-if="$store.state.user.orgId === null" label="机构">
        <OrgSelect v-model="form.orgId"></OrgSelect>
      </el-form-item> -->
      <el-form-item label="角色">
        <RoleSelect v-model="form.roleCode"></RoleSelect>
      </el-form-item>
      <el-form-item label="登录名">
        <el-input v-model.trim="form.loginName"></el-input>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model.trim="form.name"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <StateSelect v-model="form.enableState"></StateSelect>
      </el-form-item>
      <el-button @click="searchForm">查询</el-button>
      <el-button @click="add">新增</el-button>
      <!-- <el-button>导入</el-button> -->
    </el-form>

    <el-table :data="tableData" stripe style="width: 100%;">
      <el-table-column type="selection" width="42" />
      <el-table-column width="100" label="ID">
        <span slot-scope="scope">{{ scope.row.id }}</span>
      </el-table-column>
      <el-table-column
        width="200"
        v-if="$store.state.user.orgId === null"
        label="机构名称"
      >
        <span slot-scope="scope">{{ scope.row.orgName }}</span>
      </el-table-column>
      <el-table-column width="100" label="姓名">
        <span slot-scope="scope">{{ scope.row.name }}</span>
      </el-table-column>
      <el-table-column label="登录名">
        <span slot-scope="scope">{{ scope.row.loginName }}</span>
      </el-table-column>
      <el-table-column width="100" label="角色">
        <span slot-scope="scope">{{ scope.row.roleNameStr }}</span>
      </el-table-column>
      <el-table-column width="120" label="状态">
        <span slot-scope="scope">{{
          scope.row.enable | zeroOneEnableDisableFilter
        }}</span>
      </el-table-column>
      <el-table-column width="120" label="更新人">
        <span slot-scope="scope">{{ scope.row.updateName }}</span>
      </el-table-column>
      <el-table-column width="100" label="更新时间">
        <span slot-scope="scope">{{
          scope.row.updateTime | datetimeFilter
        }}</span>
      </el-table-column>
      <el-table-column :context="_self" label="操作" width="250">
        <div slot-scope="scope">
          <el-button size="mini" type="primary" plain @click="edit(scope.row)">
            编辑
          </el-button>
          <el-popconfirm
            title="确定将此用户密码重置为 123456 吗？"
            @onConfirm="resetUserPassword(scope.row)"
          >
            <el-button slot="reference" size="mini" type="primary" plain>
              重置密码
            </el-button>
          </el-popconfirm>
          <el-button
            size="mini"
            type="primary"
            plain
            @click="toggleEnableUser(scope.row)"
          >
            {{ scope.row.enable ? "禁用" : "启用" }}
          </el-button>
        </div>
      </el-table-column>
    </el-table>
    <div class="page float-right">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100, 200, 300]"
        @size-change="handleSizeChange"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      />
    </div>

    <UserManagementDialog
      ref="theDialog"
      :user="selectedUser"
      @reload="searchForm"
    />
  </div>
</template>

<script>
import RoleSelect from "@/components/RoleSelect.vue";
import StateSelect from "@/components/StateSelect";
// import OrgSelect from "@/components/OrgSelect";
import {
  searchUsers,
  toggleEnableUser,
  resetUserPassword,
} from "../../../api/system-user";
import UserManagementDialog from "./UserManagementDialog";

export default {
  name: "UserManagement",
  components: {
    RoleSelect,
    StateSelect,
    // OrgSelect,
    UserManagementDialog,
  },
  data() {
    return {
      form: {
        orgId: "",
        roleCode: "",
        loginName: "",
        name: "",
        enableState: null,
      },
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 10,
      selectedUser: {},
    };
  },
  async created() {},
  methods: {
    async searchForm() {
      const res = await searchUsers({
        orgId: this.form.orgId,
        role: this.form.roleCode,
        enable: this.form.enableState,
        loginName: this.form.loginName,
        name: this.form.name,
        pageNumber: this.currentPage,
        pageSize: this.pageSize,
      });
      this.tableData = res.data.data.records.records;
      this.total = res.data.data.records.total;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.searchForm();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.searchForm();
    },
    add() {
      this.selectedUser = {};
      this.$refs.theDialog.openDialog();
    },
    edit(user) {
      this.selectedUser = user;
      this.$refs.theDialog.openDialog();
    },
    async toggleEnableUser(user) {
      await toggleEnableUser({
        id: user.id,
        enable: user.enable === 0 ? 1 : 0,
      });
      this.searchForm();
    },
    async resetUserPassword(user) {
      await resetUserPassword({
        id: user.id,
        password: "123456",
      });
      this.$notify({ type: "success", title: "重置成功" });
    },
  },
};
</script>

<style></style>
