<template>
  <div class="h-full w-full">
    <TableHeader>
      <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>
    </TableHeader>
    <!-- 表格 -->
    <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>
    <Pagination
      :total="total"
      :current.sync="current"
      :size.sync="size"
      @get="getUserList"
    ></Pagination>

    <!-- 分配角色 -->
    <AddUserRole
      ref="AddUserRole"
      :addVisible.sync="show"
      @addSuccess="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.current - 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 class="flex justify-center gap-2">
                  {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 (
              <div class="flex justify-center gap-4">
                <span
                  class="primary cursor-pointer"
                  onClick={() => this.apportion(row)}
                >
                  分配角色
                </span>
              </div>
            );
          },
        },
      ],
      searchForm: {
        type: "loginName",
        keyword: "",
      },
      tableData: [],
      current: 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.current,
        size: this.size,
        ...obj,
      });
      this.loading = false;
      if (res.data.code == 1) {
        let { data, total, dict } = res.data.data;
        if (!data.length && this.current > 1) {
          this.current -= 1;
          this.getUserList();
        }
        this.tableData = data;
        this.total = total;
        this.dict = dict;
      }
    },

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

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