<!-- 人员登记列表 -->
<script name="RegisterList" lang="ts" setup>
import { ElMessage, ElMessageBox } from 'element-plus'
import type { IListQuery } from './person-regitster'
import { deleteStaff, getStaffList } from '@/api/resource/register'
import type { TableColumn } from '@/components/NormalTable/table_interface'

// 定义变量
const { proxy } = getCurrentInstance() as any
const router = useRouter()
const searchQuery = ref<IListQuery>({
  name: '',
  deptName: '',
  offset: 1,
  limit: 20,
})
const loadingTable = ref<boolean>(false) // 表格loading
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 exportExcelBtn = () => {

}

const batchImport = () => {

}

const templateDownload = () => {

}

const addStaff = () => {
  router.push({
    query: {
      type: 'create',
    },
    path: 'register/detail',
  })
}

// 获取数据列表
const fetchData = (isNowPage = false) => {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    searchQuery.value.offset = 1
  }
  getStaffList(searchQuery.value).then((res) => {
    if (res.code === 200) {
      list.value = res.data.rows
      total.value = Number(res.data.total)
    }
    loadingTable.value = false
  }).catch((_) => {
    loadingTable.value = false
  })
}

const search = () => {
  fetchData(true)
}

const clearList = () => {
  searchQuery.value = {
    name: '',
    deptName: '',
    offset: 1,
    limit: 20,
  }
  fetchData()
}

// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
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
  }
  fetchData(true)
}

// 查看详情信息
const detail = (row: any) => {
  sessionStorage.setItem('staffBasicInfo', JSON.stringify(row))
  router.push({
    query: {
      type: 'detail',
      id: row.id,
    },
    path: 'register/detail',
  })
}

// 编辑人员基本信息
const update = (row: any) => {
  sessionStorage.setItem('staffBasicInfo', JSON.stringify(row))
  router.push({
    query: {
      type: 'update',
      id: row.id,
    },
    path: 'register/detail',
  })
}

// 删除人员信息
const deleteStaffById = (staffId: string, staffName: string) => {
  ElMessageBox.confirm(`是否删除人员 ${staffName}`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    deleteStaff({ id: staffId }).then((res) => {
      if (res.code === 200) {
        ElMessage.success(`人员 ${staffName} 删除成功`)
        fetchData(true)
      }
      else {
        ElMessage.error(`人员 ${staffName} 删除失败: ${res.message}`)
      }
    })
  })
}

onMounted(() => {
  search()
})
</script>

<template>
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="clearList">
      <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>
      <!-- 表头区域 -->
      <template #btns-right>
        <!-- <icon-button v-if="proxy.hasPerm(`/measure/person/list/add`)" icon="icon-resource" title="筛选参数持证" @click="add" />
        <icon-button v-if="proxy.hasPerm(`/measure/person/list/add`)" icon="icon-excel" title="导出参数持证表" @click="add" />
        <icon-button v-if="proxy.hasPerm(`/measure/person/list/add`)" icon="icon-excel" title="导出人员持证表" @click="add" />
        <icon-button v-if="proxy.hasPerm(`/measure/person/list/add`)" icon="icon-excel" title="导出任职一览表" @click="add" />
        <icon-button v-if="proxy.hasPerm(`/measure/person/list/add`)" icon="icon-excel" title="导出人员统计表" @click="add" /> -->
        <icon-button v-if="proxy.hasPerm(`/resource/person/register/add`)" icon="icon-add" title="新建" @click="addStaff" />
        <icon-button v-if="proxy.hasPerm(`/resource/person/list/import`)" icon="icon-import" title="批量导入" @click="batchImport" />
        <icon-button v-if="proxy.hasPerm(`/resource/person/list/import`)" icon="icon-template" title="模板下载" @click="templateDownload" />
        <icon-button v-if="proxy.hasPerm(`/resource/person/list/export`)" icon="icon-export" title="导出" @click="exportExcelBtn" />
      </template>

      <!-- 表格区域 -->
      <normal-table
        id="registerTabel"
        :data="list" :total="total" :columns="columns"
        :query="{ limit: searchQuery.limit, offset: searchQuery.offset }"
        :list-loading="loadingTable"
        @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 v-if="proxy.hasPerm(`/resource/person/register/edit`)" size="small" type="primary" link @click="update(row)">
                编辑
              </el-button>
              <el-button size="small" type="primary" link @click="detail(row)">
                详情
              </el-button>
              <el-button v-if="proxy.hasPerm(`/resource/person/register/del`)" size="small" type="danger" link @click="deleteStaffById(row.id, row.staffName)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>
