<template>
  <BaseDialog
    :model-value="modelValue"
    @update:model-value="$emit('update:modelValue', $event)"
    :title="$t('memberSelection.title')"
    size="large"
    :close-on-overlay="false"
    @close="handleClose"
  >
    <div class="member-selection-content">
      <div class="controls-bar">
        <button class="btn-secondary" @click="$emit('select-all')">{{ $t('memberSelection.selectAll') }}</button>
        <button class="btn-secondary" @click="$emit('deselect-all')">{{ $t('memberSelection.deselectAll') }}</button>
      </div>
      
      <div class="selection-layout">
        <div class="members-column">
          <h4>{{ $t('memberSelection.members') }}</h4>
          <div class="checkbox-list">
            <label v-for="m in members" :key="m.id" class="checkbox-item">
              <input 
                type="checkbox" 
                :value="m.id" 
                :checked="selectedIds.includes(m.id)"
                @change="handleMemberToggle(m.id, $event.target.checked)" 
              />
              <span :class="{ active: activeMemberId === m.id }">
                {{ m.firstName }} {{ m.lastName }}
              </span>
            </label>
          </div>
        </div>
        
        <div class="recommendations-column" v-if="activeMember && showRecommendations">
          <h4>{{ $t('memberSelection.recommendations') }}</h4>
          <div v-if="recommendations && recommendations.length" class="checkbox-list">
            <label v-for="rec in recommendations" :key="rec.key" class="checkbox-item">
              <input 
                type="checkbox" 
                :checked="isRecommended(rec.key)"
                @change="handleRecommendationToggle(rec.key, $event.target.checked)" 
              />
              <span>{{ rec.name }} — {{ rec.date }} {{ rec.time }}</span>
            </label>
          </div>
          <div v-else class="no-data">
            <em>{{ $t('memberSelection.noRecommendations') }}</em>
          </div>
        </div>
      </div>
    </div>
    
    <template #footer>
      <button class="btn-secondary" @click="handleClose">{{ $t('memberSelection.close') }}</button>
      <button 
        class="btn-primary" 
        :disabled="selectedIds.length === 0" 
        @click="$emit('generate-pdf')"
      >
        {{ $t('memberSelection.generatePdf') }}
      </button>
    </template>
  </BaseDialog>
</template>

<script>
import BaseDialog from './BaseDialog.vue';

export default {
  name: 'MemberSelectionDialog',
  components: {
    BaseDialog
  },
  props: {
    modelValue: {
      type: Boolean,
      default: false
    },
    members: {
      type: Array,
      default: () => []
    },
    selectedIds: {
      type: Array,
      default: () => []
    },
    activeMemberId: {
      type: [Number, String],
      default: null
    },
    recommendations: {
      type: Array,
      default: () => []
    },
    recommendedKeys: {
      type: Array,
      default: () => []
    },
    showRecommendations: {
      type: Boolean,
      default: true
    }
  },
  emits: [
    'update:modelValue', 
    'close', 
    'select-all', 
    'deselect-all', 
    'toggle-member',
    'toggle-recommendation',
    'generate-pdf',
    'update:selectedIds',
    'update:activeMemberId'
  ],
  computed: {
    activeMember() {
      return this.members.find(m => m.id === this.activeMemberId);
    }
  },
  methods: {
    handleClose() {
      this.$emit('update:modelValue', false);
      this.$emit('close');
    },
    handleMemberToggle(memberId, checked) {
      this.$emit('update:activeMemberId', memberId);
      this.$emit('toggle-member', { memberId, checked });
    },
    handleRecommendationToggle(key, checked) {
      this.$emit('toggle-recommendation', { memberId: this.activeMemberId, key, checked });
    },
    isRecommended(key) {
      return this.recommendedKeys.includes(key);
    }
  }
};
</script>

<style scoped>
.member-selection-content {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 0.25rem;
}

.controls-bar {
  display: flex;
  gap: 0.5rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border-color);
}

.selection-layout {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.5rem;
  min-height: 400px;
}

.members-column,
.recommendations-column {
  display: flex;
  flex-direction: column;
}

.members-column h4,
.recommendations-column h4 {
  margin: 0 0 0.75rem 0;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--primary-soft);
  color: var(--primary-strong);
}

.checkbox-list {
  flex: 1;
  overflow-y: auto;
  max-height: 500px;
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 0.625rem;
  background: var(--surface-muted);
}

.checkbox-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.625rem 0.7rem;
  cursor: pointer;
  border-radius: 10px;
  transition: background-color 0.2s;
}

.checkbox-item:hover {
  background: rgba(47, 122, 95, 0.08);
}

.checkbox-item input[type="checkbox"] {
  cursor: pointer;
}

.checkbox-item span {
  flex: 1;
}

.checkbox-item span.active {
  font-weight: 600;
  color: var(--primary-color);
}

.no-data {
  padding: 2rem;
  text-align: center;
  color: var(--text-muted);
}

.btn-primary,
.btn-secondary {
  padding: 0.65rem 1.4rem;
  border: 1px solid transparent;
  border-radius: 10px;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-primary {
  background: linear-gradient(135deg, var(--primary-color), var(--primary-hover));
  color: var(--text-on-primary);
}

.btn-primary:hover:not(:disabled) {
  opacity: 0.9;
}

.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-secondary {
  background: var(--surface-color);
  border-color: var(--border-color);
  color: var(--text-color);
}

.btn-secondary:hover {
  background: var(--surface-muted);
  border-color: var(--primary-soft);
}

@media (max-width: 768px) {
  .selection-layout {
    grid-template-columns: 1fr;
  }
  
  .checkbox-list {
    max-height: 300px;
  }
}
</style>
