<template>
  <BaseDialog
    :model-value="modelValue"
    @update:model-value="$emit('update:modelValue', $event)"
    :title="`${$t('memberNotes.title')} ${member ? member.firstName + ' ' + member.lastName : ''}`"
    size="large"
    @close="handleClose"
  >
    <div v-if="member" class="notes-modal-content">
      <div class="notes-header-info">
        {{ $t('memberNotes.phone') }}: {{ member.phone }}
      </div>
      <div class="notes-body">
        <div class="notes-left">
          <img v-if="member.imageUrl" :src="member.imageUrl" :alt="$t('memberNotes.memberImage')"
            class="member-image" />
        </div>
        <div class="notes-right">
          <div class="form-group">
            <label>{{ $t('memberNotes.tags') }}</label>
            <multiselect 
              v-model="localSelectedTags" 
              :options="availableTags" 
              :placeholder="$t('memberNotes.selectTags')"
              label="name" 
              track-by="id" 
              multiple 
              :close-on-select="false"
              @tag="$emit('add-tag', $event)" 
              @remove="$emit('remove-tag', $event)" 
              :allow-empty="false" 
            />
          </div>
          <div class="form-group">
            <label>{{ $t('memberNotes.newNote') }}</label>
            <textarea v-model="localNoteContent" :placeholder="$t('memberNotes.newNote')" rows="4" class="note-textarea"></textarea>
            <button @click="handleAddNote" class="btn-primary">{{ $t('memberNotes.add') }}</button>
          </div>
          <div class="notes-list">
            <h4>{{ $t('memberNotes.notes') }}</h4>
            <ul>
              <li v-for="note in notes" :key="note.id" class="note-item">
                <button @click="$emit('delete-note', note.id)" class="trash-btn">🗑️</button>
                <span class="note-content">{{ note.content }}</span>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </BaseDialog>
</template>

<script>
import BaseDialog from './BaseDialog.vue';
import Multiselect from 'vue-multiselect';

export default {
  name: 'MemberNotesDialog',
  components: {
    BaseDialog,
    Multiselect
  },
  props: {
    modelValue: {
      type: Boolean,
      default: false
    },
    member: {
      type: Object,
      default: null
    },
    notes: {
      type: Array,
      default: () => []
    },
    selectedTags: {
      type: Array,
      default: () => []
    },
    availableTags: {
      type: Array,
      default: () => []
    },
    noteContent: {
      type: String,
      default: ''
    }
  },
  emits: ['update:modelValue', 'close', 'add-note', 'delete-note', 'add-tag', 'remove-tag', 'update:noteContent', 'update:selectedTags'],
  data() {
    return {
      localNoteContent: this.noteContent,
      localSelectedTags: this.selectedTags
    };
  },
  watch: {
    noteContent(newVal) {
      this.localNoteContent = newVal;
    },
    selectedTags(newVal) {
      this.localSelectedTags = newVal;
    },
    localNoteContent(newVal) {
      this.$emit('update:noteContent', newVal);
    },
    localSelectedTags(newVal) {
      this.$emit('update:selectedTags', newVal);
    }
  },
  methods: {
    handleClose() {
      this.$emit('update:modelValue', false);
      this.$emit('close');
    },
    handleAddNote() {
      this.$emit('add-note', this.localNoteContent);
      this.localNoteContent = '';
    }
  }
};
</script>

<style scoped>
.notes-modal-content {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.notes-header-info {
  padding: 0.5rem;
  background: var(--surface-muted);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.notes-body {
  display: flex;
  gap: 1.5rem;
}

.notes-left {
  flex-shrink: 0;
}

.member-image {
  width: 250px;
  height: 250px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border-color);
}

.notes-right {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.form-group label {
  font-weight: 600;
  color: var(--text-color);
}

.note-textarea {
  width: 100%;
  padding: 0.75rem 0.875rem;
  border: 1px solid var(--border-color);
  border-radius: 10px;
  font-family: inherit;
  resize: vertical;
  background: var(--surface-muted);
  color: var(--text-color);
}

.notes-list h4 {
  margin: 0 0 0.5rem 0;
  font-size: 1rem;
}

.notes-list ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.note-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem;
  margin-bottom: 0.5rem;
  background: var(--surface-muted);
  border: 1px solid var(--border-color);
  border-radius: 10px;
}

.note-content {
  flex: 1;
}

.trash-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1.2rem;
  padding: 0.25rem;
  transition: transform 0.2s;
}

.trash-btn:hover {
  transform: scale(1.2);
}

.btn-primary {
  align-self: flex-start;
  padding: 0.5rem 1rem;
  border: 1px solid transparent;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--primary-color), var(--primary-hover));
  color: var(--text-on-primary);
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.btn-primary:hover {
  opacity: 0.95;
  transform: translateY(-1px);
}

@media (max-width: 768px) {
  .notes-body {
    flex-direction: column;
  }
  
  .member-image {
    width: 100%;
    height: auto;
    max-height: 300px;
  }
}
</style>
