<template>
  <div v-if="activeSection === 'items'" class="card">
    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
      <h2>Items</h2>
      <button class="btn btn-primary" @click="$emit('add-item')">Add New Item</button>
    </div>
    
    <div v-if="loading" class="loading">
      Loading items...
    </div>
    
    <div v-else-if="items.length === 0" class="loading">
      No items found. Add your first item!
    </div>
    
    <table v-else class="table">
      <thead>
        <tr>
          <th>ID</th>
          <th>Name</th>
          <th>Description</th>
          <th>Serial Number</th>
          <th>Picture</th>
          <th>Quantity</th>
          <th>Price</th>
          <th>Created</th>
          <th>Actions</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in items" :key="item.id">
          <td>{{ item.id }}</td>
          <td>{{ item.name }}</td>
          <td>{{ item.description || '-' }}</td>
          <td>{{ item.serial_number || '-' }}</td>
          <td>
            <img v-if="item.picture" :src="item.picture" :alt="item.name" style="width: 50px; height: 50px; object-fit: cover; border-radius: 4px;">
            <span v-else>-</span>
          </td>
          <td>{{ item.quantity }}</td>
          <td>${{ parseFloat(item.price).toFixed(2) }}</td>
          <td>{{ formatDate(item.created_at) }}</td>
          <td>
            <div class="actions">
              <button class="btn btn-primary" @click="$emit('view-details', item)">Details</button>
              <button class="btn btn-warning" @click="$emit('edit-item', item)">Edit</button>
              <button class="btn btn-danger" @click="$emit('delete-item', item.id)">Delete</button>
            </div>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  name: 'ItemsSection',
  props: {
    activeSection: {
      type: String,
      required: true
    },
    items: {
      type: Array,
      required: true
    },
    loading: {
      type: Boolean,
      default: false
    }
  },
  emits: ['add-item', 'view-details', 'edit-item', 'delete-item'],
  methods: {
    formatDate(dateString) {
      if (!dateString) return '-';
      const date = new Date(dateString);
      return date.toLocaleDateString();
    }
  }
}
</script>

<style scoped>
h2 {
  color: #333 !important;
}

.loading {
  text-align: center;
  padding: 40px;
  color: #6c757d;
}

.actions {
  display: flex;
  gap: 5px;
}

.table {
  width: 100%;
  border-collapse: collapse;
}

.table th,
.table td {
  padding: 12px;
  text-align: left;
  border-bottom: 1px solid #dee2e6;
}

.table th {
  background-color: #f8f9fa;
  font-weight: 600;
}

.btn {
  padding: 6px 12px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}

.btn-primary {
  background-color: #007bff;
  color: white;
}

.btn-warning {
  background-color: #ffc107;
  color: #212529;
}

.btn-danger {
  background-color: #dc3545;
  color: white;
}

.btn:hover {
  opacity: 0.9;
}
</style>
