<template>
  <div v-if="show" class="modal" @click.self="$emit('close')">
    <div class="modal-content">
      <span class="close" @click="$emit('close')">&times;</span>
      <h2>{{ isEditing ? 'Edit Item' : 'Add New Item' }}</h2>
      
      <form @submit.prevent="$emit('save')">
        <div class="form-group">
          <label for="name">Name *</label>
          <input 
            type="text" 
            id="name" 
            :value="formData.name"
            @input="$emit('update:formData', { ...formData, name: $event.target.value })"
            required 
            placeholder="Enter item name"
          >
        </div>
        
        <div class="form-group">
          <label for="description">Description</label>
          <textarea 
            id="description" 
            :value="formData.description"
            @input="$emit('update:formData', { ...formData, description: $event.target.value })"
            placeholder="Enter item description"
          ></textarea>
        </div>
        
        <div class="form-group">
          <label for="serial_number">Serial Number</label>
          <input 
            type="text" 
            id="serial_number" 
            :value="formData.serial_number"
            @input="$emit('update:formData', { ...formData, serial_number: $event.target.value })"
            placeholder="Enter serial number"
          >
        </div>
        
        <div class="form-group">
          <label for="picture">Picture</label>
          <input 
            type="file" 
            id="picture" 
            @change="$emit('file-upload', $event)"
            accept="image/*"
          >
          <div v-if="formData.picture" style="margin-top: 10px;">
            <img :src="formData.picture" :alt="formData.name" style="max-width: 200px; max-height: 200px; border-radius: 4px;">
          </div>
        </div>
        
        <div class="form-group">
          <label for="quantity">Quantity *</label>
          <input 
            type="number" 
            id="quantity" 
            :value="formData.quantity"
            @input="$emit('update:formData', { ...formData, quantity: $event.target.value })"
            required 
            min="0"
            placeholder="Enter quantity"
          >
        </div>
        
        <div class="form-group">
          <label for="price">Price *</label>
          <input 
            type="number" 
            id="price" 
            :value="formData.price"
            @input="$emit('update:formData', { ...formData, price: $event.target.value })"
            required 
            min="0" 
            step="0.01"
            placeholder="Enter price"
          >
        </div>
        
        <div style="display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px;">
          <button type="button" class="btn" @click="$emit('close')">Cancel</button>
          <button type="submit" class="btn btn-success">
            {{ isEditing ? 'Update' : 'Add' }} Item
          </button>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ItemModal',
  props: {
    show: {
      type: Boolean,
      default: false
    },
    isEditing: {
      type: Boolean,
      default: false
    },
    formData: {
      type: Object,
      required: true
    }
  },
  emits: ['close', 'save', 'update:formData', 'file-upload']
}
</script>

<style scoped>
.modal {
  position: fixed;
  z-index: 1000;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-content {
  background-color: white;
  padding: 30px;
  border-radius: 8px;
  width: 90%;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
  position: relative;
}

.close {
  position: absolute;
  right: 20px;
  top: 15px;
  font-size: 28px;
  font-weight: bold;
  cursor: pointer;
  color: #aaa;
}

.close:hover {
  color: #000;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: 600;
  color: #333;
}

.form-group input,
.form-group textarea {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
}

.form-group textarea {
  height: 100px;
  resize: vertical;
}

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

.btn-success {
  background-color: #28a745;
  color: white;
}

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