<template>
  <div>
    <div class="title">用户管理</div>
    <el-form
      ref="searchFormRef"
      class="form"
      :inline="true"
      label-width="98px"
      :model="queryParams"
    >
      <el-form-item label="用户名称" prop="username">
        <el-input
          clearable
          placeholder="请输入"
          style="width: 220px"
          v-model="queryParams.username"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label-width="100px">
        <el-button icon="Search" type="primary" @click="handleQuery"> 搜索 </el-button>
        <el-button icon="RefreshRight" @click="resetQuery"> 重置 </el-button>
      </el-form-item>
    </el-form>

    <div style="margin-bottom: 20px">
      <el-button icon="Plus" type="primary" @click="add"> 新建 </el-button>
    </div>
    <div ref="tableContainerEl" class="table_border table">
      <el-table
        ref="tableRef"
        :data="tableData"
        v-loading="loading"
        header-row-class-name="table-header"
      >
        <el-table-column align="center" label="用户名" prop="username" show-overflow-tooltip />
        <el-table-column align="center" label="电话" prop="phone" show-overflow-tooltip />
        <el-table-column align="center" label="邮件" prop="email" show-overflow-tooltip />
        <el-table-column align="center" label="角色" prop="roles">
          <template #default="{ row }">
            {{ row.roles?.map((item) => item.name)?.join(',') }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="180">
          <template #default="{ row }">
            <el-button link type="primary" @click="editRole(row)"> 分配角色 </el-button>
            <el-button link type="primary" @click="edit(row)"> 编辑 </el-button>

            <el-button link type="danger" @click="handleDelete(row)"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:limit="queryParams.limit"
      v-model:page="queryParams.page"
      @pagination="getList"
    />

    <HandleDialog
      v-if="isShowDialog"
      :type="rowType"
      :row="currentRow"
      @closed="isShowDialog = false"
      @on-success="getList"
    />
    <RoleDialog
      v-if="isShowRoleDialog"
      :row="currentRow"
      :roleList="roleList"
      @closed="isShowRoleDialog = false"
      @on-success="getList"
    />
  </div>
</template>

<script setup>
import { useTableRPC } from '@/utils/hook'
import dayjs from 'dayjs'
import RPC from '@/utils/rpc'
import HandleDialog from './HandleDialog.vue'
import RoleDialog from './RoleDialog.vue'

const rowType = ref('')
const currentRow = ref(null)
const add = () => {
  isShowDialog.value = true
  currentRow.value = null
  rowType.value = 'add'
}
const edit = (row) => {
  isShowDialog.value = true
  rowType.value = 'edit'
  currentRow.value = row
}
const editRole = (row) => {
  isShowRoleDialog.value = true
  currentRow.value = row
}

const isShowRoleDialog = ref(false)
const roleList = ref([])
RPC.post('/role/list', { page: 1, limit: -1 }).then((res) => {
  roleList.value = res.rows || []
})

const {
  loading,
  tableData,
  tableRef,
  searchFormRef,
  total,
  queryParams,
  getList,
  handleQuery,
  resetQuery,
  handleDelete,
  isShowDialog,
  maxHeight,
  tableContainerEl,
} = useTableRPC({
  queryParams: {},
  remoteDataFn: '/user/list',
  deleteConfig: {
    remote: '/user/delete',
    key: 'id',
    message(row) {
      return `确认删除 “${row.username}”`
    },
  },
})

getList()
</script>

<style lang="scss" scoped>
.title {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 20px;
}
</style>
