<!-- 用户列表弹窗 -->
<script lang="ts" setup name="selectPerson">
import { getUserList } from '@/api/system/user'
import { getDeptTreeList } from '@/api/system/dept'
import type { TreeStructure } from '@/views/system/user/user-interface'
const emits = defineEmits(['add'])
const dialogFormVisible = ref(false)
// 查询条件
const listQuery = ref({
  keywords: '', // 姓名
  deptId: '', // 所在部门
  limit: 5,
  offset: 1,
})
const loadingTable = ref<boolean>(false)
const list = ref([]) // 表格数据
const total = ref<number>(0)
const select = ref()
const columns = ref([
  {
    text: '姓名',
    value: 'name',
  },
  {
    text: '账户',
    value: 'account',
  },
  {
    text: '所在部门',
    value: 'deptName',
  },
])
// 获取部门
const deptIdList = ref<TreeStructure[]>([])
const getDeptList = () => {
  getDeptTreeList().then((res) => {
    deptIdList.value = res.data
  })
}
// 获取数据列表
const fetchData = () => {
  select.value = ''
  getUserList(listQuery.value).then((res) => {
    if (res.code === 200) {
      list.value = res.data.rows
      total.value = res.data.total
    }
  })
}
// 搜索
const search = () => {
  fetchData()
}
// 重置
const reset = () => {
  listQuery.value = {
    keywords: '', // 姓名
    deptId: '', // 所在部门
    limit: 10,
    offset: 1,
  }
  search()
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size?: number;page?: number }) => {
  if (val && val.size) {
    listQuery.value.limit = val.size
  }
  if (val && val.page) {
    listQuery.value.offset = val.page
  }
  search()
}
// 点击保存
const submitForm = () => {
  const person = list.value.filter((item, index) => index == select.value)[0]
  emits('add', person)
  dialogFormVisible.value = false
}
// 取消
const resetForm = () => {
  dialogFormVisible.value = false
}
// 初始化
const initDialog = () => {
  dialogFormVisible.value = true
  getDeptList()
  fetchData()
}
defineExpose({ initDialog })
</script>

<template>
  <el-dialog v-if="dialogFormVisible" v-model="dialogFormVisible" title="选择人员">
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.keywords" placeholder="真实姓名" clearable />
      </search-item>
      <search-item>
        <el-select v-model="listQuery.deptId" placeholder="所在部门" clearable>
          <el-option v-for="item in deptIdList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </search-item>
    </search-area>
    <!-- 查询结果Table显示 -->
    <normal-table
      :data="list" :total="total" :columns="columns" :query="listQuery"
      :list-loading="loadingTable" @change="changePage"
    >
      <template #preColumns>
        <el-table-column label="选择" width="55" align="center">
          <template #default="scope">
            <el-radio v-model="select" :label="scope.$index" class="radio" />
          </template>
        </el-table-column>
        <!-- <el-table-column label="#" width="55" align="center">
          <template #default="scope">
            {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }}
          </template>
        </el-table-column> -->
      </template>
    </normal-table>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="submitForm">确认</el-button>
        <el-button @click="resetForm">
          取消
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
:deep(.el-radio__label) {
  display: none;
}
</style>
