<template>
  <div>
    <el-button v-if="showButton" type="primary" @click="handleRecommend" :loading="loading">推荐采购计划</el-button>
  </div>
</template>

<script>
import { recommendPurchasePlan } from '@/api/wms/purchasePlan'

export default {
  name: 'RecommendPurchasePanel',
  props: {
    orderId: {
      type: [String, Number],
      required: true
    },
    showButton: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      loading: false
    }
  },
  watch: {
    orderId: {
      handler(newVal) {
        this.handleRecommend()
      }
    }
  },
  mounted() {
    if (!this.showButton) {
      this.handleRecommend();
    }
  },
  methods: {
    async handleRecommend() {
      if (!this.orderId) {
        this.$message.error('缺少订单ID');
        return;
      }
      this.loading = true;
      try {
        const res = await recommendPurchasePlan(this.orderId);
        this.$emit('recommend', res.data);
      } catch (e) {
        this.$message.error('推荐失败');
      } finally {
        this.loading = false;
      }
    }
  }
}
</script> 