<template>
  <div class="drill-down-content">
    <!-- 钻取条件显示 -->
    <div class="drill-down-header">
      <el-tag size="large" v-if="queryParams.itemType">
        {{ queryParams.itemType === 'product' ? '成品' : '原材料' }}
      </el-tag>
      <el-tag size="large" v-if="itemName" type="primary">
        {{ itemName }}
      </el-tag>
      <el-tag size="large" v-if="warehouseName" type="success">
        {{ warehouseName }}
      </el-tag>
      <el-tag size="large" v-if="actualWarehouseName" type="info">
        {{ actualWarehouseName }}
      </el-tag>
    </div>

    <!-- 加载状态 -->
    <el-skeleton :loading="loading" animated>
      <template #template>
        <div class="demo-skeleton">
          <el-skeleton-item variant="p" style="width: 80%" />
          <el-skeleton-item variant="text" style="width: 30%" />
          <el-skeleton-item variant="text" style="width: 60%" />
        </div>
      </template>

      <!-- 钻取表格 -->
      <el-table 
        max-height="400" 
        v-loading="loading" 
        :data="list" 
        border 
        stripe 
        style="width: 100%"
        v-if="!loading"
      >
        <el-table-column prop="warehouseName" label="库位" align="center" min-width="150"></el-table-column>
        <el-table-column label="实际库区" align="center" prop="actualWarehouseName" v-if="!hideWarehouseQuery"/>
        <el-table-column prop="currentCoilNo" label="当前卷号" align="center" min-width="120"></el-table-column>
        <el-table-column prop="enterCoilNo" label="入场卷号" align="center" min-width="180"></el-table-column>
        <el-table-column label="物料类型" align="center" prop="itemType">
          <template slot-scope="scope">
            {{ scope.row.itemType == 'product' ? '成品' : '原料' }}
          </template>
        </el-table-column>
        <el-table-column label="产品类型" align="center" min-width="250">
          <template slot-scope="scope">
            <ProductInfo v-if="scope.row.itemType === 'product'" :product="scope.row.product" />
            <RawMaterialInfo v-else-if="scope.row.itemType === 'raw_material'" :material="scope.row.rawMaterial" />
          </template>
        </el-table-column>
        <el-table-column label="更新时间" align="center" prop="updateTime" />
        <el-table-column prop="grossWeight" label="毛重(kg)" align="center" min-width="100"></el-table-column>
        <el-table-column prop="netWeight" label="净重(kg)" align="center" min-width="100"></el-table-column>
      </el-table>
    </el-skeleton>

    <!-- 分页 -->
    <div class="pagination-container" v-if="!loading && list.length > 0">
      <el-pagination 
        background 
        :current-page.sync="pageNum"
        :page-size.sync="pageSize" 
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper" 
        :total="total"
        @size-change="handleSizeChange" 
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { listMaterialCoil } from "@/api/wms/coil";
import RawMaterialInfo from "@/components/KLPService/Renderer/RawMaterialInfo";
import ProductInfo from "@/components/KLPService/Renderer/ProductInfo";

export default {
  components: {
    RawMaterialInfo,
    ProductInfo
  },
  props: {
    // 查询参数：包含warehouseId, itemType, itemId
    queryParams: {
      type: Object,
      default: () => ({
        warehouseId: null,
        itemType: null,
        itemId: null,
        actualWarehouseId: null
      })
    },
    // 物料名称（用于显示标签）
    itemName: {
      type: String,
      default: ''
    },
    // 仓库名称（用于显示标签）
    warehouseName: {
      type: String,
      default: ''
    },
    // 实际库区名称（用于显示标签）
    actualWarehouseName: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      loading: false,
      list: [],
      total: 0,
      pageNum: 1,
      pageSize: 20
    };
  },
  watch: {
    // 监听查询参数变化，重新加载数据
    queryParams: {
      deep: true,
      handler() {
        // 重置页码
        this.pageNum = 1;
        // 重新加载数据
        this.getList();
      }
    }
  },
  mounted() {
    // 初始加载数据
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      
      // 构建查询参数
      const params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        itemType: this.queryParams.itemType,
        itemId: this.queryParams.itemId,
        warehouseId: this.queryParams.warehouseId
      };

      // 调用API获取数据
      listMaterialCoil({ ...params, dataType: 1 }).then(res => {
        this.list = res.rows || [];
        this.total = res.total || 0;
        this.loading = false;
      }).catch(error => {
        console.error('获取钻取数据失败:', error);
        this.loading = false;
        this.$message.error('获取明细数据失败');
      });
    },

    // 处理分页大小变化
    handleSizeChange(val) {
      this.pageSize = val;
      this.getList();
    },

    // 处理当前页变化
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getList();
    }
  }
};
</script>

<style scoped>
.drill-down-content {
  padding: 10px 0;
}

.drill-down-header {
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #f0f0f0;
}

.drill-down-header .el-tag {
  margin-right: 10px;
  margin-bottom: 10px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

/* 骨架屏样式 */
.demo-skeleton {
  padding: 20px 0;
}
</style>