<template>
    <div class="sale-section">
      <h3>{{ $t('falukant.branch.sale.title') }}</h3>
      <!-- Beispielhafte Inventar-Tabelle -->
      <div v-if="inventory.length > 0" class="inventory-table">
        <table>
          <thead>
            <tr>
              <th>{{ $t('falukant.branch.sale.region') }}</th>
              <th>{{ $t('falukant.branch.sale.product') }}</th>
              <th>{{ $t('falukant.branch.sale.quality') }}</th>
              <th>{{ $t('falukant.branch.sale.quantity') }}</th>
              <th>{{ $t('falukant.branch.sale.sell') }}</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(item, index) in inventory" :key="`${item.region.id}-${item.product.id}-${item.quality}`">
              <td>{{ item.region.name }}</td>
              <td>{{ $t(`falukant.product.${item.product.labelTr}`) }}</td>
              <td>{{ item.quality }}</td>
              <td>{{ item.totalQuantity }}</td>
              <td>
                <input type="number" v-model.number="item.sellQuantity" :min="1" :max="item.totalQuantity" />
                <button @click="sellItem(index)">{{ $t('falukant.branch.sale.sellButton') }}</button>
              </td>
            </tr>
          </tbody>
        </table>
        <button @click="sellAll">{{ $t('falukant.branch.sale.sellAllButton') }}</button>
      </div>
      <div v-else>
        <p>{{ $t('falukant.branch.sale.noInventory') }}</p>
      </div>
    </div>
  </template>
  
  <script>
  import apiClient from '@/utils/axios.js';
  export default {
    name: "SaleSection",
    props: { branchId: { type: Number, required: true } },
    data() {
      return {
        inventory: [],
      };
    },
    async mounted() {
      await this.loadInventory();
    },
    methods: {
      async loadInventory() {
        try {
          const response = await apiClient.get(`/api/falukant/inventory/${this.branchId}`);
          this.inventory = response.data.map(item => ({
            ...item,
            sellQuantity: item.totalQuantity,
          }));
        } catch (error) {
          console.error('Error loading inventory:', error);
        }
      },
      sellItem(index) {
        const item = this.inventory[index];
        const quantityToSell = item.sellQuantity || item.totalQuantity;
        apiClient.post(`/api/falukant/sell`, {
          branchId: this.branchId,
          productId: item.product.id,
          quantity: quantityToSell,
          quality: item.quality,
        }).catch(() => {
          alert(this.$t('falukant.branch.sale.sellError'));
        });
      },
      sellAll() {
        apiClient.post(`/api/falukant/sell/all`, { branchId: this.branchId })
        .catch(() => {
          alert(this.$t('falukant.branch.sale.sellAllError'));
        });
      },
    },
  };
  </script>
  
  <style scoped>
  .sale-section {
    border: 1px solid #ccc;
    margin: 10px 0;
    border-radius: 4px;
    padding: 10px;
  }
  .inventory-table table {
    width: 100%;
    border-collapse: collapse;
  }
  .inventory-table th,
  .inventory-table td {
    padding: 2px 3px;
    border: 1px solid #ddd;
  }
  </style>
  