<template>
  <el-drawer
    :title="t('bomList.materialDetail')"
    :append-to-body="true"
    :model-value="modelValue"
    @update:model-value="$emit('update:modelValue', $event)"
    :show-close="false"
    direction="rtl"
    :size="computedSize"
    :before-close="handleClose"
  >
    <template v-if="nodeId">
      <div v-loading="loading" style="height: 100%">
        <el-table :data="filteredMaterials" style="width: 100%">
          <el-table-column prop="bomNodeId" :label="t('bomList.bomNode')" width="180" v-if="false"/>
          <el-table-column prop="factory" :label="t('workOrderMaterial.factory')" width="180" v-if="!hideExtraColumns"/>
          <el-table-column prop="costCenter" :label="t('workOrderMaterial.costCenter')" width="180" v-if="!hideExtraColumns"/>
          <el-table-column prop="projectDepartment" :label="t('workOrderMaterial.storageLocation')"  width="180" v-if="!hideExtraColumns"/>
          <el-table-column prop="materialName" :label="t('workOrderMaterial.materialName')" width="180" />
          <el-table-column prop="materialCode" :label="t('workOrderMaterial.materialCode')" width="180" />
          <el-table-column prop="unit" :label="t('workOrderMaterial.unit')" width="180" />
          <el-table-column prop="unitPrice" :label="t('workOrderMaterial.unitPrice')" width="180" />
          <el-table-column prop="quantity" :label="t('workOrderMaterial.ConsumptionQuantity')" width="180" />
          <el-table-column prop="materialSource" :label="t('bomList.type')" width="180" />
          <el-table-column :label="t('workplace.operation')" align="right" v-if="!props.detail">
            <template #default="scope">
              <el-button
                size="small"
                type="danger"
                @click="handleDelete(scope.row)"
              >{{ t('form.delete') }}</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="loadMaterials(props.nodeId)"
        />
        -->
      </div>
    </template>

  </el-drawer>
</template>
<script setup lang="ts">

import { ref, watch, defineOptions, defineEmits } from 'vue'
import { ElMessage } from 'element-plus'
import * as PmsMaterialApi from '@/api/pms/material'
const drawerVisible = ref<boolean>(false)
const emit = defineEmits(['update:modelValue', 'add', 'delete'])
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

defineOptions({
  name: 'SelectedMaterialDrawer'
})

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  createTime: [],
  bomId: '',
  name: '',
  code: ''
})

const windowWidth = ref(window.innerWidth)
// 动态计算百分比
const computedSize = computed(() => {
  return windowWidth.value > 1200 ? '60%' : '80%'
})

const loading = ref(false)
const total = ref(0) // 列表的总页数
// const materials = ref([])

const props = defineProps({
  modelValue: Boolean,
  nodeId: Number,
  detail: Boolean,
  materials: Array, // 接收父组件传递的完整物料列表
  hideExtraColumns: {
    type: Boolean,
    default: false
  }
})

// 监听bom树节点ID变化
watch(() => props.nodeId, (newVal) => {
  console.log('传递过来的nodeId', newVal)
  if (newVal) {
    // await loadMaterials(newVal)
  }
})

// 计算属性过滤当前节点物料
const filteredMaterials = computed(() => {
  return props.materials?.filter(item =>
    item.bomNodeId === props.nodeId
  ) || []
})

// 加载指定bom节点下的物料数据
const loadMaterials = async (nodeId) => {
  queryParams.bomId = nodeId
  try {
    loading.value = true
    // API调用
    const data = await PmsMaterialApi.listByBomId(queryParams)
    materials.value = data.list
    total.value = data.total
  } catch (error) {
    ElMessage.error('数据加载失败')
  } finally {
    loading.value = false
  }
}

/** 删除物料前的确认 */
const handleDelete = async (row: any) => {
  try {
    await message.delConfirm()
    // 用户确认后触发删除事件
    emit('delete', row)
    // message.success(t('common.delSuccess'))
  } catch (error) {
    // 用户取消操作
    console.log('删除操作已取消')
  }
}

// 打开抽屉
const openDrawer = () => {
  drawerVisible.value = true
}

// 关闭抽屉
const closeDrawer = () => {
  drawerVisible.value = false
}

// 关闭抽屉
const handleClose = () => {
  emit('update:modelValue', false)
  // materials.value = []
}

defineExpose({ openDrawer, closeDrawer }) // 暴露方法给父组件

</script>

<style lang="scss" scoped></style>
