<template>
  <el-dialog :title="title" v-model="dialogVisible" width="1000px" append-to-body>
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="80px">
      <el-form-item label="天线编码" prop="antennaCode">
        <el-input
          v-model="queryParams.antennaCode"
          placeholder="请输入天线编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="读写器ID" prop="readerId">
        <el-input
          v-model="queryParams.readerId"
          placeholder="请输入读写器ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库ID" prop="warehouseId">
        <el-input
          v-model="queryParams.warehouseId"
          placeholder="请输入仓库ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安装位置" prop="installationLocation">
        <el-input
          v-model="queryParams.installationLocation"
          placeholder="请输入安装位置"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="ptlTagList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="天线ID" align="center" prop="antennaId" />
      <el-table-column label="天线编码" align="center" prop="antennaCode" />
      <el-table-column label="读写器ID" align="center" prop="readerId" />
      <el-table-column label="仓库ID" align="center" prop="warehouseId" />
      <el-table-column label="库区ID" align="center" prop="areaId" />
      <el-table-column label="货架ID" align="center" prop="locationId" />
      <el-table-column label="安装位置" align="center" prop="installationLocation" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="handleQuery"
    />

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
        <el-button @click="handleCancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="PtlTagSelectDialog">
import { ref, reactive, watch } from 'vue'
import { listWmsRfidAntenna } from "@/api/ware/wmsRfidAntenna"

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  title: {
    type: String,
    default: '选择PTL标签'
  }
})

const emit = defineEmits(['update:visible', 'confirm', 'cancel'])

// 本地对话框可见性状态
const dialogVisible = ref(false)

// 监听props.visible变化，更新对话框可见性
watch(() => props.visible, (newVal) => {
  dialogVisible.value = newVal
})

// 监听dialogVisible变化，向父组件发送更新
watch(dialogVisible, (newVal) => {
  emit('update:visible', newVal)
})

// 表格加载状态
const loading = ref(false)

// 分页总条数
const total = ref(0)

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  antennaCode: undefined,
  readerId: undefined,
  warehouseId: undefined,
  installationLocation: undefined
})

// 表格数据
const ptlTagList = ref([])

// 选中的数据
const selectionList = ref([])

// 初始化
const init = () => {
  selectionList.value = []
  ptlTagList.value = []
  queryParams.pageNum = 1
  queryParams.pageSize = 10
  queryParams.antennaCode = undefined
  queryParams.readerId = undefined
  queryParams.warehouseId = undefined
  queryParams.installationLocation = undefined
}

// 查询数据
const handleQuery = () => {
  loading.value = true
  listWmsRfidAntenna(queryParams).then(response => {
    ptlTagList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 重置查询
const resetQuery = () => {
  queryParams.antennaCode = undefined
  queryParams.readerId = undefined
  queryParams.warehouseId = undefined
  queryParams.installationLocation = undefined
  handleQuery()
}

// 处理选择变化
const handleSelectionChange = (selection) => {
  selectionList.value = selection
}

// 处理确认选择
const handleConfirm = () => {
  if (selectionList.value.length === 0) {
    ElMessage.warning('请选择一条记录')
    return
  }
  emit('confirm', selectionList.value)
  dialogVisible.value = false
}

// 处理取消
const handleCancel = () => {
  emit('cancel')
  dialogVisible.value = false
}

// 当对话框打开时初始化数据
watch(() => dialogVisible.value, (newVal) => {
  if (newVal) {
    init()
    handleQuery()
  }
})
</script>

<style scoped>
.dialog-footer {
  text-align: right;
}
</style>