<!-- 选择设备对话框 -->
<script lang="ts" setup name="ReceiveDialog">
import { ref } from 'vue'
import dayjs from 'dayjs'
import type { IdeviceList } from './receive'
import useUserStore from '@/store/modules/user'
import { listPageApi } from '@/api/device/standingBook'
import type { TableColumn } from '@/components/NormalTable/table_interface'

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,
  equipmentNo: '', // 设备编号
  equipmentName: '', // 名称
  mesureType: '', // 检定方式
  managerState: '1', // 管理状态 1在用
  useDept: '', // 使用部门
  assetType: '1', // 资产类型 1测量设备
  equipmentCategory: '', // 设备类别
  isCalibrationTestEquipment: '', // 校准/检定设备
  isMeasureAccount: '', // 测量工装
  isStandardSupportEquipment: '', // 标准配套设备
  isFixedAssets: '', // 固定资产
  validDateStart: '', // 有效开始日期
  validDateEnd: '', // 有效结束日期
  abc: '',
  limit: 20,
  offset: 1,
  ids: [] as string[],
})
const total = ref(20)
// 多选选中的内容
const checkoutList = ref([])

const list = ref([])// 表格数据
const columns = ref<TableColumn[]>([
  { text: '设备名称', value: 'equipmentName', align: 'center', fixed: true },
  { text: '设备编号', value: 'equipmentNo', align: 'center', fixed: true, width: '160' },
  { text: '型号', value: 'modelNo', align: 'center' },
  { text: '出厂编号', value: 'manufacturingNo', align: 'center' },
  { text: '管理状态', value: 'managerStateName', align: 'center', width: '120' },
  { text: '使用人', value: 'usePersonName', align: 'center' },
  { text: '有效日期', value: 'validDate', align: 'center', width: '120' },
])
const table = ref()
const dialogTableVisible = ref(false)// 控制对话框显隐

// 获取数据列表
const fetchData = () => {
  loadingTable.value = true
  listPageApi(listQuery.value).then((res) => {
    list.value = res.data.rows.map((item: IdeviceList) => {
      return {
        ...item,
        validDate: item.validDate ? dayjs(item.validDate).format('YYYY-MM-DD') : item.validDate,
      }
    })
    total.value = res.data.total
    loadingTable.value = false
  }).catch((_) => {
    loadingTable.value = false
  })
}
// 重置
const clearList = () => {
  listQuery.value.equipmentName = ''
  listQuery.value.equipmentNo = ''
  fetchData()
}
watch(() => props.dialogSelectDiviceVisible, (newValue) => {
  dialogTableVisible.value = newValue
  clearList()
  if (!newValue) {
    table.value.clearMulti() // 清除选中
  }
})
// 关闭对话框
const close = () => {
  emits('closeDialog')
}
// 确认
const confirm = () => {
  emits('updateDeviceConfirm', checkoutList.value)
  close()
}

// 搜索
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="65%" title="选择设备" class="select-device-dialog" @close="close">
    <search-area
      style="padding-left: 0;padding-right: 0;"
      :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"
      :page-sizes="[20]"
      :height="240"
      @change="changePage"
      @multiSelect="handleSelectionChange"
    >
      <template #preColumns>
        <el-table-column label="#" width="55" align="center" fixed>
          <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 type="primary" @click="confirm">
          确定
        </el-button>
        <el-button @click="close">取消</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>
