<!-- 筛选系统用户弹窗 -->
<script name="FilterSysUser" lang="ts" setup>
import type { IlistQueryType } from '@/views/system/user/user-interface'
import { getUserList } from '@/api/system/user'
import type { TableColumn } from '@/components/NormalTable/table_interface'

const emit = defineEmits(['recordSelected'])

// 定义变量
const showFilterDialog = ref(false)
const searchQuery = reactive<IlistQueryType>({
  keywords: '',
  deptId: '',
  offset: 1,
  limit: 10,
  sort: '',
  beginTime: '',
  endTime: '',
  deptType: '',
})
const sysUserList = ref([])
const total = ref<number>(0) // 数据总条数
const columns = ref <Array<TableColumn>>([
  { text: '账号', value: 'account', align: 'center' },
  { text: '姓名', value: 'name', align: 'center' },
  { text: '部门', value: 'deptName', align: 'center' },
]) // 表头

const getSysUserList = () => {
  getUserList(searchQuery).then((res) => {
    if (res.code === 200) {
      sysUserList.value = res.data.rows
      total.value = Number(res.data.total)
    }
  })
}

// 逻辑
const changePage = (val: { size?: number; page?: number }) => {
  if (val && val.size) {
    searchQuery.limit = val.size
  }
  if (val && val.page) {
    searchQuery.offset = val.page
  }
  getSysUserList()
}

const selectSingleUser = (row: Object) => {
  emit('recordSelected', row)
}

const showOrHideFilterDialog = (show: boolean) => {
  if (show === false) {
    searchQuery.keywords = ''
    searchQuery.deptId = ''
    searchQuery.offset = 1
    searchQuery.limit = 10
  }
  else {
    getSysUserList()
  }

  showFilterDialog.value = show
}

defineExpose({
  showOrHideFilterDialog,
})
</script>

<template>
  <el-dialog v-model="showFilterDialog" title="选择用户" :append-to-body="true" :close-on-click-modal="false" top="10vh">
    <search-area :need-clear="false" @search="getSysUserList">
      <search-item>
        <el-input v-model="searchQuery.keywords" placeholder="姓名" clearable />
      </search-item>
      <search-item>
        <dept-select v-model="searchQuery.deptId" placeholder="工作部门" style="width: 100%;" />
      </search-item>
    </search-area>

    <!-- 表格数据展示 -->
    <table-container :title-show="false">
      <!-- 表格区域 -->
      <normal-table
        id="sysUserFilter"
        :data="sysUserList" :total="total" :columns="columns"
        :query="{ limit: searchQuery.limit, offset: searchQuery.offset }"
        @change="changePage"
      >
        <template #preColumns>
          <el-table-column label="序号" width="55" align="center">
            <template #default="scope">
              {{ (searchQuery.offset - 1) * searchQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
        <template #columns>
          <el-table-column fixed="right" label="操作" align="center" width="130">
            <template #default="{ row }">
              <el-button size="small" type="primary" @click="selectSingleUser(row)">
                选定
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </el-dialog>
</template>
