<!-- 用户列表弹窗 -->
<script lang="ts" setup name="selectPerson">
import { ElMessage } from 'element-plus'
import { listPageApi } from '@/api/device/standingBook'
const emits = defineEmits(['add'])
const dialogFormVisible = ref(false)
// 查询条件
const listQuery = ref({
  equipmentName: '', // 设备名称
  equipmentNo: '', // 设备编号
  limit: 10,
  offset: 1,
})
const loadingTable = ref<boolean>(false)
const list = ref([]) // 表格数据
const total = ref<number>(0)
const columns = ref([
  {
    text: '设备名称',
    value: 'equipmentName',
    align: 'center',
  },
  {
    text: '设备编号',
    value: 'equipmentNo',
    align: 'center',
  },
  {
    text: '型号',
    value: 'modelNo',
    align: 'center',
  },
  {
    text: '出厂编号',
    value: 'manufacturingNo',
    align: 'center',
  },
  {
    text: '管理状态',
    value: 'managerStateName',
    align: 'center',
  },
  {
    text: '使用人',
    value: 'usePersonName',
    align: 'center',
  },
  {
    text: '有效日期',
    value: 'validDate',
    align: 'center',
  },
])
// 获取数据列表
const fetchData = () => {
  loadingTable.value = true
  listPageApi(listQuery.value as any).then((res) => {
    if (res.code === 200) {
      list.value = res.data.rows.map(item => ({ ...item, checked: '' }))
      total.value = res.data.total
    }
    loadingTable.value = false
  }).catch((_) => {
    loadingTable.value = false
  })
}
// 搜索
const search = () => {
  fetchData()
}
// 重置
const reset = () => {
  listQuery.value = {
    equipmentName: '', // 姓名
    equipmentNo: '', // 所在部门
    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 checkedArr = list.value.filter(item => item.checked !== '')
  if (checkedArr.length) {
    console.log(checkedArr, '选中的设备')
    emits('add', checkedArr)
    dialogFormVisible.value = false
    reset()
  }
  else {
    ElMessage.warning('请先选择设备')
  }
}
// 取消
const resetForm = () => {
  dialogFormVisible.value = false
  reset()
}
// 初始化
const initDialog = () => {
  // reset()
  dialogFormVisible.value = true
  fetchData()
}
defineExpose({ initDialog })
</script>

<template>
  <el-dialog v-model="dialogFormVisible" title="选择设备" width="50%">
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.equipmentName" placeholder="设备名称" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.equipmentNo" placeholder="设备编号" clearable />
      </search-item>
    </search-area>
    <!-- 查询结果Table显示 -->
    <div style="padding: 12px;">
      <normal-table
        :data="list" :total="total" :columns="columns as any" :query="listQuery"
        :list-loading="loadingTable"
        @change="changePage"
      >
        <template #preColumns>
          <el-table-column label="选择" width="55" align="center">
            <template #default="scope">
              <el-checkbox v-model="scope.row.checked" :label="scope.$index" size="large" />
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </div>
    <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-checkbox__label) {
  display: none;
}
</style>
