<template>
  <Dialog v-model="dialogVisible" title="物资柜检查记录" width="1500">
    <el-table
      v-loading="loading"
      :data="list"
    >
      <el-table-column label="计划名称" align="center" prop="planName" width="200" />
      <el-table-column label="物资柜" align="center" prop="cabinetName" />
      <el-table-column label="物资编号" align="center" prop="materialsId" />
      <el-table-column label="物资名称" align="center" prop="materialsName" />
      <el-table-column label="物资类型" align="center" prop="materialsTypeName" />
      <el-table-column label="物资图片" align="center" prop="materialsTypePicture">
        <template #default="scope">
          <UploadImg
            v-if="scope.row.materialsTypePicture"
            v-model="scope.row.materialsTypePicture"
            :limit="1"
            height="50px"
            width="50px"
          />
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="RFID" align="center" prop="materialsRfid" />
      <el-table-column label="检查时间" align="center" prop="createTime" width="250" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag
            :type="DICT_TYPE.CHECKS_STATUS"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="异常原因" align="center" prop="reason">
        <template #default="scope">
          <dict-tag
            :type="DICT_TYPE.EXCEPTIONS_STATUS"
            :value="scope.row.reason"
          />
        </template>
      </el-table-column>
    </el-table>

    <Pagination
      v-model:total="total"
      v-model:page="queryParams.current"
      v-model:limit="queryParams.size"
      @pagination="getList"
    />

    <template #footer>
      <el-button @click="dialogVisible = false">关 闭</el-button>
    </template>
  </Dialog>
</template>

<script lang="ts" setup>
import { DICT_TYPE } from '@/utils/dict'
import * as RecordApi from '@/api/material/checkRecord'

defineOptions({ name: 'CheckRecordDialog' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数

// 查询参数
const queryParams = reactive({
  current: 1,
  size: 10,
  planId: undefined,
  cabinetId: undefined
})

/** 打开弹窗 */
const open = async (row: any) => {
  dialogVisible.value = true
  queryParams.planId = row.planId
  queryParams.cabinetId = row.cabinetId
  await getList()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 获取检查记录列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await RecordApi.listCheckRecord(queryParams)
    list.value = data.records
    total.value = data.total
  } finally {
    loading.value = false
  }
}
</script>
