<template>
  <div>
    <el-table :data="stockList" style="width: 100%" v-loading="loading">
      <el-table-column prop="raw_material_name" label="原料名称"></el-table-column>
      <el-table-column prop="stock" label="当前库存">
        <template #default="scope">
          {{ formatWeight(scope.row.stock) }}
        </template>
      </el-table-column>
      <el-table-column prop="last_updated" label="最后更新时间">
        <template #default="scope">
          {{ formatRFC3339(scope.row.last_updated) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <el-button size="small" type="primary" @click="handleAdjust(scope.row)">调整库存</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pagination.page_size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total">
    </el-pagination>
  </div>
</template>

<script>
import { ref, onMounted, watch } from 'vue';
import { InventoryApi } from '../../api/inventory';
import { ElMessage } from 'element-plus';
import { formatRFC3339, formatWeight } from '../../utils/format'; // 导入 formatWeight

export default {
  name: 'StockListTable',
  props: {
    stockFilter: {
      type: String,
      default: 'all', // 默认值与父组件保持一致
    },
    searchRawMaterialName: {
      type: String,
      default: '',
    },
  },
  emits: ['adjust-stock'], // 声明组件将发出的事件
  setup(props, { expose, emit }) { // 接收 emit 函数
    const stockList = ref([]);
    const loading = ref(false);
    const pagination = ref({
      page: 1,
      page_size: 10,
      total: 0,
    });

    const fetchStockList = async (filter = props.stockFilter, rawMaterialName = props.searchRawMaterialName) => {
      loading.value = true;
      try {
        const params = {
          page: pagination.value.page,
          page_size: pagination.value.page_size,
        };
        if (filter === 'in_stock') {
          params.has_stock = true;
        }
        if (rawMaterialName) {
          params.raw_material_name = rawMaterialName;
        }
        const res = await InventoryApi.getCurrentStockList(params);
        stockList.value = res.data.list;
        pagination.value.total = res.data.pagination.total;
      } catch (error) {
        ElMessage.error('获取库存列表失败');
      } finally {
        loading.value = false;
      }
    };

    const handleSizeChange = (val) => {
      pagination.value.page_size = val;
      fetchStockList();
    };

    const handleCurrentChange = (val) => {
      pagination.value.page = val;
      fetchStockList();
    };

    // 处理库存调整操作
    const handleAdjust = (row) => {
      emit('adjust-stock', row); // 只传递行数据
    };

    onMounted(() => {
      fetchStockList();
    });

    watch(() => props.stockFilter, (newFilter) => {
      pagination.value.page = 1; // 筛选条件变化时重置页码
      fetchStockList(newFilter, props.searchRawMaterialName);
    });

    watch(() => props.searchRawMaterialName, (newRawMaterialName) => {
      pagination.value.page = 1; // 搜索条件变化时重置页码
      fetchStockList(props.stockFilter, newRawMaterialName);
    });

    // 暴露 fetchStockList 方法给父组件
    expose({
      fetchStockList,
    });

    return {
      stockList,
      loading,
      pagination,
      handleSizeChange,
      handleCurrentChange,
      formatRFC3339,
      formatWeight, // 暴露给模板
      handleAdjust, // 暴露给模板
    };
  },
};
</script>
