<template>
  <el-dialog :title="title" v-model="dialogVisible" width="1200px" append-to-body>
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="80px">
      <el-form-item label="标签编码" prop="tagCode">
        <el-input
          v-model="queryParams.tagCode"
          placeholder="请输入标签编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="EPC编码" prop="epcCode">
        <el-input
          v-model="queryParams.epcCode"
          placeholder="请输入EPC编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="TID编码" prop="tidCode">
        <el-input
          v-model="queryParams.tidCode"
          placeholder="请输入TID编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标签类型" prop="tagType">
        <el-input
          v-model="queryParams.tagType"
          placeholder="请输入标签类型"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标签状态" prop="tagStatus">
        <el-input
          v-model="queryParams.tagStatus"
          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="rfidTagList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标签ID" align="center" prop="tagId" />
      <el-table-column label="标签编码" align="center" prop="tagCode" />
      <el-table-column label="EPC编码" align="center" prop="epcCode" />
      <el-table-column label="TID编码" align="center" prop="tidCode" />
      <el-table-column label="标签类型" align="center" prop="tagType" />
      <el-table-column label="标签状态" align="center" prop="tagStatus" />
      <el-table-column label="最后读取时间" align="center" prop="lastReadTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.lastReadTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </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="RfidTagSelectDialog">
import { ref, reactive, watch } from 'vue'
import { listWmsRfidTag } from "@/api/ware/wmsRfidTag"
import { parseTime } from "@/utils/ruoyi"

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

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,
  tagCode: undefined,
  epcCode: undefined,
  tidCode: undefined,
  tagType: undefined,
  tagStatus: undefined
})

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

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

// 初始化
const init = () => {
  selectionList.value = []
  rfidTagList.value = []
  queryParams.pageNum = 1
  queryParams.pageSize = 10
  queryParams.tagCode = undefined
  queryParams.epcCode = undefined
  queryParams.tidCode = undefined
  queryParams.tagType = undefined
  queryParams.tagStatus = undefined
}

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

// 重置查询
const resetQuery = () => {
  queryParams.tagCode = undefined
  queryParams.epcCode = undefined
  queryParams.tidCode = undefined
  queryParams.tagType = undefined
  queryParams.tagStatus = 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>