<template>
  <div class="user-page">
    <table-header>
      <div slot="right">
        <el-form ref="searchForm" :model="searchForm" inline size="small">
          <el-form-item prop="type">
            <el-select
              style="width: 120px"
              v-model="searchForm.type"
              placeholder="请选择"
            >
              <el-option
                v-for="(v, key) in typeList"
                :key="key"
                :label="v"
                :value="key"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="keyword">
            <el-input
              v-model="searchForm.keyword"
              style="width: 200px"
              class="ml10 mr10"
              placeholder="请输入关键字搜索"
              @keyup.native.enter="handleSearch"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">搜 索</el-button>
          </el-form-item>
          <el-form-item>
            <el-button @click="handleReset">重 置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </table-header>
    <!-- 表格 -->
    <div class="table-content">
      <y-table
        ref="MyTable"
        :loading="loading"
        :data="tableData"
        :column="column"
        border
        tooltip-effect="dark"
        :max-height="600"
        :row-key="(row) => row.id"
      ></y-table>
    </div>
    <y-pagination
      :total="total"
      :page.sync="page"
      :pageSize.sync="size"
      @change="getUserList"
    ></y-pagination>

    <!-- 分配角色 -->
    <AddUserRole
      ref="AddUserRole"
      :show.sync="show"
      @success="getUserList"
    ></AddUserRole>
  </div>
</template>

<script>
import TableHeader from "@/components/TableHeader.vue";
import AddUserRole from "./components/AddUserRole.vue";
import { getUserList } from "@/api/system";
let typeList = {
  loginName: "按登录名",
  realName: "按用户姓名",
  mobile: "按手机号码",
};
export default {
  components: {
    TableHeader,
    AddUserRole,
  },
  data() {
    return {
      typeList,
      column: [
        {
          label: "序号",
          type: "index",
          width: "55",
          align: "center",
          index: (index) => {
            return (this.page - 1) * this.size + index + 1;
          },
        },
        {
          label: "登录名",
          prop: "loginName",
          align: "center",
        },
        {
          label: "用户姓名",
          prop: "realName",
          align: "center",
        },
        {
          label: "手机号码",
          prop: "mobile",
          align: "center",
        },
        {
          label: "所属角色",
          prop: "roleIds",
          align: "center",
          formatter: (row) => {
            if (row.roleIds) {
              return (
                <div style="display: flex; justify-content: center; gap:4px">
                  {row.roleIds.split(",").map((v) => {
                    return (
                      <el-tag size="small" type="info">
                        {this.dict.roleIds[v]}
                      </el-tag>
                    );
                  })}
                </div>
              );
            }
          },
        },
        {
          label: "状态",
          prop: "status",
          align: "center",
          formatter: (row) => {
            if (this.dict.status) {
              return this.dict.status[row.status];
            }
          },
        },
        {
          label: "创建时间",
          prop: "createTime",
          align: "center",
          formatter: (row) => {
            return this.$moment(row.createTime).format("YYYY-MM-DD HH:mm:ss");
          },
        },
        {
          label: "操作",
          align: "center",
          width: "120",
          formatter: (row) => {
            return (
              <span
                v-hasPermi={["system:user:userRole"]}
                class="primary pointer"
                onClick={() => this.apportion(row)}
              >
                分配角色
              </span>
            );
          },
        },
      ],
      searchForm: {
        type: "loginName",
        keyword: "",
      },
      tableData: [],
      page: 1,
      size: 10,
      total: 0,
      loading: false,
      selectRows: [],
      show: false,
      title: "新增",
      dict: {}, // 字典
      resShow: false,
    };
  },
  created() {
    this.getUserList();
  },
  computed: {},
  methods: {
    // 列表
    async getUserList() {
      this.loading = true;
      let obj = {};
      let value = `%${this.searchForm.keyword}%`;
      obj[this.searchForm.type] = value;
      let res = await getUserList({
        page: this.page,
        size: this.size,
        ...obj,
      });
      this.loading = false;
      if (res.data.code == 1) {
        let { data, total, dict } = res.data.data;
        if (!data.length && this.page > 1) {
          this.page -= 1;
          this.getUserList();
        }
        this.tableData = data;
        this.total = total;
        this.dict = dict;
      }
    },

    // 搜索
    handleSearch() {
      this.page = 1;
      this.$clearSelection("MyTable");
      this.getUserList();
    },
    // 重置
    handleReset() {
      this.page = 1;
      this.$clearSelection("MyTable");
      this.$resetForm("searchForm");
      this.getUserList();
    },
    apportion(row) {
      let data = { ...row };
      this.$refs.AddUserRole.onEdit(data);
      this.show = true;
    },
  },
};
</script>

<style lang="less" scoped></style>
