<template>
  <Dialog v-model="dialogVisible" title="绑定物资" width="1000">
    <el-table
      v-loading="loading"
      :data="dialogMatList"
      @selection-change="handleSelectionChange"
      style="height: 550px"
    >
      <el-table-column type="selection" width="55" align="center" />
      <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="supplier" />
      <el-table-column label="有效期" align="center" prop="expirationDate" />
      <el-table-column label="状态" align="center" prop="loanState">
        <template #default="scope">
          <dict-tag
            :type="DICT_TYPE.MATERIAL_STATUS"
            :value="scope.row.loanState"
          />
        </template>
      </el-table-column>
    </el-table>

    <div class="dialog-footer">
      <Pagination
        v-model:total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
      <div class="dialog-buttons">
        <el-button type="primary" @click="submitBind">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </div>
  </Dialog>
</template>

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

defineOptions({ name: 'BindDialog' })

const props = defineProps({
  cabinetId: {
    type: String,
    required: true
  }
})

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

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

// 查询参数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  materialsCabinetId: 0
})

/** 打开弹窗 */
const open = () => {
  dialogVisible.value = true
  getList()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 查询物资列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await MaterialApi.listMaterials(queryParams)
    dialogMatList.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: any[]) => {
  materialsIds.value = selection.map((item) => item.id)
}

/** 提交绑定 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitBind = async () => {
  try {
    const data = {
      materialsCabinetId: props.cabinetId,
      materialsIds: materialsIds.value
    }
    await MaterialApi.updateMaterialsBinding(data)
    message.success('绑定成功')
    dialogVisible.value = false
    emit('success')
  } catch {}
}
</script>
<style scoped lang="scss">
.dialog-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  margin-top: 10px;

  .dialog-buttons {
    display: flex;
    gap: 8px;
  }
}
</style>
