<template>
  <div class="card">
    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
      <h2>Contact Persons</h2>
      <div style="display: flex; gap: 10px;">
        <input 
          type="text" 
          :value="contactSearch"
          @input="$emit('search-contacts', $event.target.value)"
          placeholder="Search contacts..." 
          style="padding: 8px; border: 1px solid #ddd; border-radius: 4px;"
        >
        <button class="btn btn-primary" @click="$emit('add-contact')">Add Contact Person</button>
      </div>
    </div>
    
    <div v-if="contactsLoading" class="loading">
      Loading contact persons...
    </div>
    
    <div v-else-if="contacts.length === 0" class="loading">
      No contact persons found. Create your first contact person!
    </div>
    
    <div v-else class="contacts-list">
      <div v-for="contact in contacts" :key="contact.id" class="contact-card">
        <div class="contact-header">
          <div class="contact-name">
            <h3>{{ contact.first_name }} {{ contact.last_name }}</h3>
            <div class="contact-position">{{ contact.position || 'No position' }}</div>
          </div>
          <div class="contact-badges">
            <span v-if="contact.is_primary" class="badge badge-primary">Primary</span>
            <span v-if="contact.department" class="badge badge-secondary">{{ contact.department }}</span>
          </div>
        </div>
        
        <div class="contact-details">
          <div class="contact-info">
            <div v-if="contact.email" class="info-item">
              <strong>Email:</strong> {{ contact.email }}
            </div>
            <div v-if="contact.phone" class="info-item">
              <strong>Phone:</strong> {{ contact.phone }}
            </div>
            <div v-if="contact.department" class="info-item">
              <strong>Department:</strong> {{ contact.department }}
            </div>
            <div v-if="contact.notes" class="info-item">
              <strong>Notes:</strong> {{ contact.notes }}
            </div>
          </div>
          
          <div class="contact-actions">
            <button class="btn btn-sm btn-primary" @click="$emit('view-contact', contact)">View</button>
            <button class="btn btn-sm btn-warning" @click="$emit('edit-contact', contact)">Edit</button>
            <button class="btn btn-sm btn-danger" @click="$emit('delete-contact', contact.id)">Delete</button>
          </div>
        </div>
        
        <div class="contact-footer">
          <small>Created: {{ formatDate(contact.created_at) }}</small>
          <small v-if="contact.updated_at !== contact.created_at">
            | Updated: {{ formatDate(contact.updated_at) }}
          </small>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from '../../api/axios'
import { formatDate } from '../../utils/locale'

export default {
  name: 'ContactPersonsSection',
  props: {
    activeSection: {
      type: String,
      required: true
    },
    contacts: {
      type: Array,
      default: () => []
    },
    contactsLoading: {
      type: Boolean,
      default: false
    },
    contactSearch: {
      type: String,
      default: ''
    }
  },
  methods: {
    formatDate
  }
}
</script>

<style scoped>
.card {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

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

.contacts-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.contact-card {
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 15px;
  background: #fafafa;
  transition: box-shadow 0.2s;
}

.contact-card:hover {
  box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}

.contact-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 10px;
}

.contact-name h3 {
  margin: 0 0 5px 0;
  color: #333;
  font-size: 18px;
}

.contact-position {
  color: #666;
  font-size: 14px;
}

.contact-badges {
  display: flex;
  gap: 5px;
  flex-wrap: wrap;
}

.badge {
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
  text-transform: uppercase;
}

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

.badge-secondary {
  background-color: #6c757d;
  color: white;
}

.contact-details {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 10px;
}

.contact-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.info-item {
  font-size: 14px;
  line-height: 1.4;
}

.info-item strong {
  color: #333;
  min-width: 80px;
  display: inline-block;
}

.contact-actions {
  display: flex;
  gap: 5px;
  flex-wrap: wrap;
}

.btn {
  padding: 6px 12px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  text-decoration: none;
  display: inline-block;
  transition: background-color 0.2s;
}

.btn-sm {
  padding: 4px 8px;
  font-size: 11px;
}

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

.btn-primary:hover {
  background-color: #0056b3;
}

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

.btn-warning:hover {
  background-color: #e0a800;
}

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

.btn-danger:hover {
  background-color: #c82333;
}

.contact-footer {
  border-top: 1px solid #eee;
  padding-top: 8px;
  margin-top: 10px;
}

.contact-footer small {
  color: #666;
  font-size: 12px;
}
</style>
