<template>
  <el-drawer
    title="物料详情"
    :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%">

        <div class="info-header">
          <div class="info-item">
            <span class="info-label">设备分类：</span>
            <span class="info-value">{{ rowInfo.deviceCategoryName }}</span>
          </div>
          <div class="separator"></div> <!-- 分隔线 -->
          <div class="info-item">
            <span class="info-label">BOM节点：</span>
            <span class="info-value">{{ rowInfo.bomNodeName }}</span>
          </div>
        </div>

        <el-table :data="materials" style="width: 100%">
          <el-table-column prop="name" label="物料名称" width="180" />
          <el-table-column prop="code" label="物料编码" width="180" />
          <!-- <el-table-column prop="model" label="规格型号" width="180" /> -->
          <el-table-column prop="unit" label="单位" width="180" />
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            width="180"
            :formatter="dateFormatter"
          />
          <el-table-column label="操作" align="right">
            <template #default="scope">
              <el-button
                size="small"
                type="danger"
                @click="handleDelete(scope.row)"
              >删除</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'
import {CommonBomMaterialApi, CommonBomMaterialVO} from '@/api/pms/commonbommaterial'
import {dateFormatter} from "@/utils/formatTime";
const drawerVisible = ref<boolean>(false)
const emit = defineEmits(['update:modelValue', 'add', 'delete', 'refresh'])
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

defineOptions({
  name: 'MaterialListDrawer'
})

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,
  rowInfo: {  // 新增props
    type: Object,
    default: () => ({
      deviceCategoryName: '',
      bomNodeName: ''
    })
  }
})

// 监听bom树节点ID变化
watch(() => props.nodeId, async (newVal) => {
  if (newVal) {
    await loadMaterials(newVal)
  }
})

// 加载指定bom节点下的物料数据
const loadMaterials = async (nodeId) => {
  queryParams.bomId = nodeId
  queryParams.pageNo = 1
  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
  }
}

/** 删除BOM节点已经挂载的物料 */
const handleDelete = async (row) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await CommonBomMaterialApi.deleteBomMaterial(props.nodeId, row.code)
    message.success(t('common.delSuccess'))
    // 刷新列表
    loadMaterials(props.nodeId)
  } catch {}
}

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

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

// 关闭抽屉
const handleClose = () => {
  emit('update:modelValue', false)
  emit('refresh') // 添加刷新事件
  materials.value = []
}

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

</script>

<style lang="scss" scoped>
.info-header {
  display: flex;
  align-items: center;
  padding: 18px 24px;
  background-color: #f5f7fa;
  border-bottom: 1px solid #ebeef5;
  margin-bottom: 20px;

  .info-item {
    flex: 1; /* 平分宽度 */
    display: flex;
    align-items: center; /* 垂直居中 */
  }

  .separator {
    width: 1px;
    height: 40px;
    background-color: #dcdfe6;
    margin: 0 24px;
  }

  .info-label {
    font-weight: bold;
    color: #606266;
    margin-right: 8px; /* 标签和值之间的间距 */
    font-size: 14px;
    white-space: nowrap; /* 防止标签换行 */
  }

  .info-value {
    color: #303133;
    font-size: 16px;
    font-weight: 500;
    white-space: nowrap; /* 防止值换行 */
    overflow: hidden;
    text-overflow: ellipsis; /* 超出部分显示省略号 */
  }
}
</style>
