<!-- 选择设备 -->
<script lang="ts" setup name="ReceiveDialog">
import { ref } from 'vue'
import useUserStore from '@/store/modules/user'
import { listPageApi } from '@/api/device/standingBook'
const props = defineProps({
  dialogSelectDiviceVisible: {
    type: Boolean,
    default: false,
  },
  isMulti: Boolean,
})
const emits = defineEmits(['closeDialog', 'updateDeviceConfirm'])
const loadingTable = ref<boolean>(false)
const userStore = useUserStore()
// 查询条件
const listQuery = ref({
  equipmentName: '', // 设备名称
  equipmentNo: '', // 设备编号
  offset: 1,
  limit: 20,
})
const total = ref(20)
// 多选选中的内容
const checkoutList = ref([])

const list = ref([])// 表格数据
const columns = [
  {
    text: '设备名称',
    value: 'name',
  },
  {
    text: '设备编号',
    value: 'number',
  },
  {
    text: '型号',
    value: 'version',
  },
  {
    text: '出厂编号',
    value: 'millNumber',
  },
  {
    text: '管理状态',
    value: 'status',
  },
  {
    text: '使用人',
    value: 'person',
  },
  {
    text: '有效日期',
    value: 'date',
  },
]
const table = ref()
const dialogTableVisible = ref(false)// 控制对话框显隐

// 获取数据列表
const fetchData = () => {
  loadingTable.value = true
  listPageApi(listQuery.value as any).then((res) => {
    if (res.code === 200) {
      list.value = res.data.rows.map((item: any) => {
        return {
          ...item,
          name: item.equipmentName,
          number: item.equipmentNo,
          version: item.modelNo,
          millNumber: item.manufacturingNo,
          status: item.managerStateName,
          person: item.usePersonName,
          date: item.validDate,
          scope: item.mesureRange,
          dep: item.useDeptName,
        }
      })
      total.value = res.data.total
    }
    loadingTable.value = false
  }).catch((_) => {
    loadingTable.value = false
  })
}
watch(() => props.dialogSelectDiviceVisible, (newValue) => {
  dialogTableVisible.value = newValue
  fetchData()
  if (!newValue) {
    table.value.clearMulti() // 清除多选选中
  }
})
// 关闭对话框
const close = () => {
  console.log('close-dialog')
  emits('closeDialog')
}
// 确认
const confirm = () => {
  console.log('确认')
  emits('updateDeviceConfirm', checkoutList.value)
  // 调接口
  close()
}
// 重置
const clearList = () => {
  listQuery.value.equipmentName = ''
  listQuery.value.equipmentNo = ''
  fetchData()
}
// 搜索
const searchList = () => {
  fetchData()
}
// 多选选中
const handleSelectionChange = (val: any) => {
  console.log('多选', val)
  checkoutList.value = val
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
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
  }
  fetchData()
}
</script>

<template>
  <el-dialog v-model="dialogTableVisible" width="60%" title="选择设备" class="select-device-dialog" @close="close">
    <search-area
      :need-clear="true"
      @search="searchList" @clear="clearList"
    >
      <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-container>
      <normal-table
        ref="table"
        :data="list"
        :total="total"
        :columns="columns"
        :query="listQuery"
        :is-multi="isMulti"
        is-showmulti-select
        :list-loading="loadingTable"
        @change="changePage"
        @multiSelect="handleSelectionChange"
      >
        <template #preColumns>
          <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>
    </table-container>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="confirm">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
// 样式
.receive-dialog {
  .receive-dialog-item {
    display: flex;
    align-items: center;
    margin-bottom: 28px;

    .text {
      margin-right: 30px;
      white-space: nowrap;
    }
  }
}
</style>
