<!-- 筛选系统用户弹窗 -->
<script name="FilterRegistedStaff" lang="ts" setup>
import { column } from 'element-plus/es/components/table-v2/src/common'
import { ElMessage } from 'element-plus'
import type { IListQuery } from '@/views/resource/person/register/person-regitster'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { getStaffList } from '@/api/resource/register'

const emit = defineEmits(['recordSelected'])

// 定义变量
const refStaffTable = ref()

const showFilterDialog = ref(false)
const searchQuery = ref<IListQuery>({
  name: '',
  deptName: '',
  offset: 1,
  limit: 20,
})
const total = ref<number>(0) // 数据总条数
const columns = ref<Array<TableColumn>>([
  { text: '人员编号', value: 'staffNo', align: 'center', width: '160' },
  { text: '姓名', value: 'staffName', align: 'center' },
  { text: '岗位', value: 'station', align: 'center' },
  { text: '证号', value: 'certificateNumber', align: 'center' },
  { text: '工作部门', value: 'deptName', align: 'center' },
  { text: '计量专业', value: 'major', align: 'center' },
]) // 表头
const list = ref([]) // 表格数据
const maxSelect = ref<number>(0)

const rowSelected = ref([])

const checkStaffScore = (rows: any) => {
  if (rows.length === 0) {
    ElMessage.warning('至少选择一行')
    return false
  }
  for (let index = 0; index < rows.length; index++) {
    const row = rows[index]
    if (row.staffScore === null || row.staffScore === '') {
      ElMessage.warning(`请先完善第 ${index + 1} 行的效果（成绩）字段，不能为空`)
      return false
    }
  }

  return true
}

const getStaffRegisted = () => {
  getStaffList(searchQuery.value).then((res) => {
    if (res.code === 200) {
      list.value = res.data.rows
      total.value = Number(res.data.total)
    }
  })
}

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

const staffSelected = (e: any) => {
  if (e.length > maxSelect.value) {
    ElMessage.warning(`最多选择 ${maxSelect.value} 行`)
    refStaffTable.value.clearMulti()
  }
  else {
    rowSelected.value = e
  }
}

const submitStaffSelected = () => {
  if (checkStaffScore(rowSelected.value) === true) {
    emit('recordSelected', rowSelected.value)
  }
}

const showOrHideFilterDialog = (show: boolean, max = 0) => {
  if (show === false) {
    searchQuery.value.name = ''
    searchQuery.value.deptName = ''
    searchQuery.value.offset = 1
    searchQuery.value.limit = 20
  }
  else {
    getStaffRegisted()
  }

  showFilterDialog.value = show
  maxSelect.value = max
}

defineExpose({
  showOrHideFilterDialog,
})
</script>

<template>
  <el-dialog v-model="showFilterDialog" title="选择人员" :append-to-body="true" :close-on-click-modal="false" top="10vh" width="75%">
    <search-area :need-clear="false" @search="getStaffRegisted">
      <search-item>
        <el-input v-model="searchQuery.name" placeholder="姓名" clearable />
      </search-item>
      <search-item>
        <el-input v-model="searchQuery.deptName" placeholder="工作部门" clearable />
      </search-item>
    </search-area>

    <!-- 表格数据展示 -->
    <table-container :title-show="false">
      <!-- 表格区域 -->
      <normal-table
        ref="refStaffTable"
        :data="list" :total="total" :columns="columns"
        :query="{ limit: searchQuery.limit, offset: searchQuery.offset }"
        :is-showmulti-select="true"
        @change="changePage"
        @multi-select="staffSelected"
      >
        <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 label="效果（成绩）" align="center" width="150">
            <template #default="{ row }">
              <el-input v-model="row.staffScore" />
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="showOrHideFilterDialog(false)">取消</el-button>
        <el-button type="primary" @click="submitStaffSelected">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
