<template>
  <div class="members-mockup">
    <!-- Header -->
    <header class="members-header">
      <div 
        v-motion
        :initial="{ opacity: 0, y: -20 }"
        :enter="{ opacity: 1, y: 0 }"
        class="members-header__content"
      >
        <h1 class="members-header__title">Member Management</h1>
        <p class="members-header__subtitle">Manage {{ totalMembers }} MonacoUSA members</p>
      </div>
      
      <div 
        v-motion
        :initial="{ opacity: 0, y: -20 }"
        :enter="{ opacity: 1, y: 0, transition: { delay: 100 } }"
        class="members-header__actions"
      >
        <MonacoButton variant="glass" icon="download">
          Export Members
        </MonacoButton>
        <MonacoButton variant="primary" icon="user-plus">
          Add Member
        </MonacoButton>
      </div>
    </header>

    <!-- Search and Filters -->
    <div 
      v-motion
      :initial="{ opacity: 0, y: 20 }"
      :enter="{ opacity: 1, y: 0, transition: { delay: 200 } }"
      class="members-controls"
    >
      <div class="members-controls__search">
        <FloatingInput 
          v-model="searchQuery"
          label="Search members by name or email..."
          leftIcon="search"
          variant="glass"
          clearable
        />
      </div>
      
      <div class="members-controls__filters">
        <select class="filter-select">
          <option value="">Member Status</option>
          <option value="active">Active</option>
          <option value="inactive">Inactive</option>
          <option value="pending">Pending</option>
          <option value="suspended">Suspended</option>
        </select>
        
        <select class="filter-select">
          <option value="">Dues Status</option>
          <option value="current">Current</option>
          <option value="overdue">Overdue</option>
          <option value="exempt">Exempt</option>
        </select>
        
        <select class="filter-select">
          <option value="">Sort By</option>
          <option value="name">Name (A-Z)</option>
          <option value="joined">Join Date</option>
          <option value="dues">Dues Status</option>
        </select>
      </div>
    </div>

    <!-- Stats Bar -->
    <div 
      v-motion
      :initial="{ opacity: 0, x: -20 }"
      :enter="{ opacity: 1, x: 0, transition: { delay: 300 } }"
      class="members-stats"
    >
      <div class="stat-chip">
        <span class="stat-chip__value">{{ totalMembers }}</span>
        <span class="stat-chip__label">Total Members</span>
      </div>
      <div class="stat-chip">
        <span class="stat-chip__value">{{ activeMembers }}</span>
        <span class="stat-chip__label">Active Members</span>
      </div>
      <div class="stat-chip">
        <span class="stat-chip__value">{{ overdueDues }}</span>
        <span class="stat-chip__label">Overdue Dues</span>
      </div>
      <div class="stat-chip">
        <span class="stat-chip__value">{{ inactiveMembers }}</span>
        <span class="stat-chip__label">Inactive Members</span>
      </div>
    </div>

    <!-- View Toggle -->
    <div 
      v-motion
      :initial="{ opacity: 0 }"
      :enter="{ opacity: 1, transition: { delay: 350 } }"
      class="view-controls"
    >
      <div class="view-toggle">
        <button 
          class="view-toggle__btn"
          :class="{ 'view-toggle__btn--active': viewMode === 'table' }"
          @click="viewMode = 'table'"
        >
          <span>☰</span> Table View
        </button>
        <button 
          class="view-toggle__btn"
          :class="{ 'view-toggle__btn--active': viewMode === 'cards' }"
          @click="viewMode = 'cards'"
        >
          <span>⊞</span> Card View
        </button>
      </div>
    </div>

    <!-- Members Table/Cards -->
    <div 
      class="members-container"
      :class="`members-container--${viewMode}`"
    >
      <!-- Table View -->
      <template v-if="viewMode === 'table'">
        <div class="members-table">
          <table>
            <thead>
              <tr>
                <th>Member</th>
                <th>Status</th>
                <th>Dues</th>
                <th>Join Date</th>
                <th>Portal Account</th>
                <th>Actions</th>
              </tr>
            </thead>
            <tbody>
              <tr
                v-for="(member, index) in filteredMembers"
                :key="member.id"
                v-motion
                :initial="{ opacity: 0, x: -20 }"
                :enter="{ 
                  opacity: 1, 
                  x: 0,
                  transition: { delay: 400 + (index * 30) }
                }"
              >
                <td>
                  <div class="member-cell">
                    <div class="member-cell__avatar">
                      <img v-if="member.avatar" :src="member.avatar" :alt="member.name" />
                      <div v-else class="avatar-placeholder-small">{{ getInitials(member.name) }}</div>
                    </div>
                    <div>
                      <div class="member-cell__name">{{ member.name }}</div>
                      <div class="member-cell__email">{{ member.email }}</div>
                    </div>
                  </div>
                </td>
                <td>
                  <span 
                    class="status-badge"
                    :class="`status-badge--${member.memberStatus}`"
                  >
                    {{ member.memberStatus }}
                  </span>
                </td>
                <td>
                  <span 
                    class="dues-badge"
                    :class="`dues-badge--${member.duesStatus}`"
                  >
                    {{ member.duesStatus }}
                  </span>
                </td>
                <td>{{ member.joinDate }}</td>
                <td>
                  <span 
                    class="portal-status"
                    :class="{ 'portal-status--active': member.hasPortalAccess }"
                  >
                    {{ member.hasPortalAccess ? 'Active' : 'None' }}
                  </span>
                </td>
                <td>
                  <div class="table-actions">
                    <MonacoButton 
                      variant="ghost" 
                      size="sm"
                      icon="edit"
                      @click="editMember(member)"
                    >
                      Edit
                    </MonacoButton>
                    <MonacoButton 
                      variant="ghost" 
                      size="sm"
                      icon="mail"
                      @click="emailMember(member)"
                    >
                      Email
                    </MonacoButton>
                    <MonacoButton 
                      v-if="!member.hasPortalAccess"
                      variant="ghost" 
                      size="sm"
                      icon="user-plus"
                      @click="inviteToPortal(member)"
                    >
                      Invite
                    </MonacoButton>
                  </div>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </template>

      <!-- Card View -->
      <template v-else-if="viewMode === 'cards'">
        <MemberCard
          v-for="(member, index) in filteredMembers"
          :key="member.id"
          :member="member"
          :featured="member.featured"
          :delay="index"
          variant="glass"
          @click="editMember"
          @email="emailMember"
          @invite="inviteToPortal"
        />
      </template>
      
      <!-- List View -->
      <template v-else>
        <div
          v-for="(member, index) in filteredMembers"
          :key="member.id"
          v-motion
          :initial="{ opacity: 0, x: -20 }"
          :enter="{ 
            opacity: 1, 
            x: 0,
            transition: { delay: 400 + (index * 30) }
          }"
          class="member-list-item"
          @click="selectMember(member)"
        >
          <div class="member-list-item__avatar">
            <img v-if="member.avatar" :src="member.avatar" :alt="member.name" />
            <div v-else class="avatar-placeholder">{{ getInitials(member.name) }}</div>
            <span 
              v-if="member.status === 'online'" 
              class="status-indicator"
            />
          </div>
          
          <div class="member-list-item__info">
            <h3 class="member-list-item__name">{{ member.name }}</h3>
            <p class="member-list-item__title">{{ member.title }} at {{ member.company }}</p>
          </div>
          
          <div class="member-list-item__tags">
            <span 
              v-for="tag in member.tags?.slice(0, 2)" 
              :key="tag"
              class="tag"
            >
              {{ tag }}
            </span>
          </div>
          
          <div class="member-list-item__meta">
            <span class="meta-text">Member since {{ member.joinDate }}</span>
            <span v-if="member.connections" class="meta-text">{{ member.connections }} connections</span>
          </div>
          
          <div class="member-list-item__actions">
            <MonacoButton 
              variant="ghost" 
              size="sm"
              icon="edit"
              @click.stop="editMember(member)"
            >
              Edit
            </MonacoButton>
            <MonacoButton 
              variant="ghost" 
              size="sm"
              icon="mail"
              @click.stop="emailMember(member)"
            >
              Email
            </MonacoButton>
          </div>
        </div>
      </template>

    </div>

    <!-- Load More -->
    <div 
      v-motion
      :initial="{ opacity: 0 }"
      :enter="{ opacity: 1, transition: { delay: 800 } }"
      class="load-more"
    >
      <MonacoButton variant="glass" icon="refresh" block>
        Load More Members
      </MonacoButton>
    </div>

    <!-- Admin Quick Actions Widget -->
    <GlassCard 
      variant="glass"
      class="admin-widget"
      :animated="true"
      :delay="900"
    >
      <h4 class="admin-widget__title">Quick Actions</h4>
      <div class="admin-actions">
        <MonacoButton variant="glass" size="sm" icon="download" block>
          Export Member List
        </MonacoButton>
        <MonacoButton variant="glass" size="sm" icon="mail" block>
          Send Dues Reminder
        </MonacoButton>
        <MonacoButton variant="glass" size="sm" icon="chart" block>
          View Reports
        </MonacoButton>
        <MonacoButton variant="primary" size="sm" icon="user-plus" block>
          Add New Member
        </MonacoButton>
      </div>
    </GlassCard>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import GlassCard from '~/components/ui/GlassCard.vue'
import MonacoButton from '~/components/ui/MonacoButton.vue'
import FloatingInput from '~/components/ui/FloatingInput.vue'
import MemberCard from '~/components/ui/MemberCard.vue'

const searchQuery = ref('')
const viewMode = ref('table')
const totalMembers = ref(156)
const activeMembers = ref(142)
const overdueDues = ref(8)
const inactiveMembers = ref(14)

const members = ref([
  {
    id: 1,
    name: 'Alexandra Martin',
    email: 'alexandra.martin@example.com',
    avatar: '/api/placeholder/150/150',
    title: 'CEO',
    company: 'Monaco Ventures',
    memberStatus: 'active',
    duesStatus: 'current',
    hasPortalAccess: true,
    joinDate: 'Jan 2021',
    lastActive: '2 hours ago'
  },
  {
    id: 2,
    name: 'Jean-Paul Rousseau',
    email: 'jp.rousseau@example.com',
    avatar: '/api/placeholder/150/150',
    title: 'Managing Director',
    company: 'Riviera Capital',
    memberStatus: 'active',
    duesStatus: 'current',
    hasPortalAccess: true,
    joinDate: 'Mar 2020',
    lastActive: '1 day ago'
  },
  {
    id: 3,
    name: 'Sarah Chen',
    email: 'sarah.chen@example.com',
    avatar: '',
    title: 'VP of Technology',
    company: 'TechMonaco',
    memberStatus: 'active',
    duesStatus: 'overdue',
    hasPortalAccess: false,
    joinDate: 'Jun 2022',
    lastActive: 'Never'
  },
  {
    id: 4,
    name: 'Marcus Williams',
    email: 'marcus.w@example.com',
    avatar: '/api/placeholder/150/150',
    title: 'Partner',
    company: 'Monaco Law Group',
    memberStatus: 'inactive',
    duesStatus: 'overdue',
    hasPortalAccess: false,
    joinDate: 'Sep 2021',
    lastActive: '3 months ago'
  },
  {
    id: 5,
    name: 'Isabella Romano',
    email: 'isabella@example.com',
    avatar: '/api/placeholder/150/150',
    title: 'Creative Director',
    company: 'Romano Design Studio',
    memberStatus: 'active',
    duesStatus: 'current',
    hasPortalAccess: true,
    joinDate: 'Feb 2023',
    lastActive: '5 days ago'
  },
  {
    id: 6,
    name: 'Thomas Anderson',
    email: 't.anderson@example.com',
    avatar: '',
    title: 'Investment Manager',
    company: 'Monte Carlo Holdings',
    memberStatus: 'pending',
    duesStatus: 'exempt',
    hasPortalAccess: false,
    joinDate: 'Nov 2019',
    lastActive: 'Never'
  }
])

const filteredMembers = computed(() => {
  if (!searchQuery.value) return members.value
  
  const query = searchQuery.value.toLowerCase()
  return members.value.filter(member => 
    member.name.toLowerCase().includes(query) ||
    member.email?.toLowerCase().includes(query) ||
    member.company?.toLowerCase().includes(query)
  )
})

const getInitials = (name: string) => {
  return name.split(' ').map(n => n[0]).join('').toUpperCase().slice(0, 2)
}

const editMember = (member: any) => {
  console.log('Edit member:', member)
  // Would open edit dialog
}

const emailMember = (member: any) => {
  console.log('Email member:', member)
  // Would open email compose
}

const inviteToPortal = (member: any) => {
  console.log('Invite to portal:', member)
  // Would send portal invitation
}
</script>

<style scoped lang="scss">
.members-mockup {
  padding: 2rem;
  max-width: 1400px;
  margin: 0 auto;
  background: linear-gradient(135deg, #fef2f2 0%, #ffffff 100%);
  min-height: 100vh;
  position: relative;
}

.members-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
  flex-wrap: wrap;
  gap: 1.5rem;
  
  &__content {
    flex: 1;
  }
  
  &__title {
    font-size: 2.5rem;
    font-weight: 700;
    color: #27272a;
    margin: 0 0 0.5rem;
  }
  
  &__subtitle {
    font-size: 1.125rem;
    color: #6b7280;
    margin: 0;
  }
  
  &__actions {
    display: flex;
    gap: 1rem;
  }
}

.members-controls {
  display: flex;
  gap: 1.5rem;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
  
  &__search {
    flex: 1;
    min-width: 300px;
  }
  
  &__filters {
    display: flex;
    gap: 0.75rem;
    flex-wrap: wrap;
  }
}

.filter-select {
  padding: 0.75rem 1rem;
  background: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(20px);
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 12px;
  font-size: 0.875rem;
  color: #27272a;
  cursor: pointer;
  transition: all 0.2s;
  outline: none;
  
  &:hover {
    background: rgba(255, 255, 255, 0.8);
    border-color: rgba(220, 38, 38, 0.2);
  }
  
  &:focus {
    border-color: #dc2626;
    box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.1);
  }
}

.members-stats {
  display: flex;
  gap: 1rem;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
}

.stat-chip {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  background: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 20px;
  
  &__value {
    font-size: 1.125rem;
    font-weight: 700;
    color: #dc2626;
  }
  
  &__label {
    font-size: 0.875rem;
    color: #6b7280;
  }
}

.view-controls {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 1.5rem;
}

.view-toggle {
  display: flex;
  gap: 0.25rem;
  padding: 0.25rem;
  background: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(20px);
  border-radius: 12px;
  
  &__btn {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
    background: transparent;
    border: none;
    border-radius: 8px;
    font-size: 0.875rem;
    color: #6b7280;
    cursor: pointer;
    transition: all 0.2s;
    
    &:hover {
      background: rgba(220, 38, 38, 0.05);
    }
    
    &--active {
      background: #dc2626;
      color: white;
    }
  }
}

.members-container {
  margin-bottom: 2rem;
  
  &--cards {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 1.5rem;
  }
  
  &--table {
    overflow-x: auto;
  }
}

.members-table {
  width: 100%;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(20px);
  border-radius: 12px;
  overflow: hidden;
  
  table {
    width: 100%;
    border-collapse: collapse;
  }
  
  th {
    padding: 1rem;
    text-align: left;
    font-size: 0.875rem;
    font-weight: 600;
    color: #6b7280;
    background: rgba(255, 255, 255, 0.5);
    border-bottom: 2px solid rgba(220, 38, 38, 0.1);
  }
  
  td {
    padding: 1rem;
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
  }
  
  tr:hover {
    background: rgba(255, 255, 255, 0.95);
  }
}

.member-cell {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  
  &__avatar {
    width: 2.5rem;
    height: 2.5rem;
    border-radius: 8px;
    overflow: hidden;
    flex-shrink: 0;
    
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }
  
  &__name {
    font-weight: 600;
    color: #27272a;
    margin-bottom: 0.125rem;
  }
  
  &__email {
    font-size: 0.75rem;
    color: #6b7280;
  }
}

.status-badge {
  display: inline-block;
  padding: 0.25rem 0.75rem;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 500;
  text-transform: capitalize;
  
  &--active {
    background: rgba(16, 185, 129, 0.1);
    color: #10b981;
  }
  
  &--inactive {
    background: rgba(107, 114, 128, 0.1);
    color: #6b7280;
  }
  
  &--pending {
    background: rgba(251, 146, 60, 0.1);
    color: #fb923c;
  }
  
  &--suspended {
    background: rgba(239, 68, 68, 0.1);
    color: #ef4444;
  }
}

.dues-badge {
  display: inline-block;
  padding: 0.25rem 0.75rem;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 500;
  text-transform: capitalize;
  
  &--current {
    background: rgba(16, 185, 129, 0.1);
    color: #10b981;
  }
  
  &--overdue {
    background: rgba(239, 68, 68, 0.1);
    color: #ef4444;
  }
  
  &--exempt {
    background: rgba(107, 114, 128, 0.1);
    color: #6b7280;
  }
}

.portal-status {
  font-size: 0.75rem;
  color: #6b7280;
  
  &--active {
    color: #10b981;
    font-weight: 500;
  }
}

.table-actions {
  display: flex;
  gap: 0.5rem;
}

.member-list-item {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1rem 1.5rem;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.2s;
  
  &:hover {
    background: rgba(255, 255, 255, 0.95);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
    transform: translateX(4px);
  }
  
  &__avatar {
    position: relative;
    width: 3rem;
    height: 3rem;
    border-radius: 10px;
    overflow: hidden;
    flex-shrink: 0;
    
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }
  
  &__info {
    flex: 1;
    min-width: 0;
  }
  
  &__name {
    margin: 0 0 0.25rem;
    font-size: 1rem;
    font-weight: 600;
    color: #27272a;
  }
  
  &__title {
    margin: 0;
    font-size: 0.875rem;
    color: #6b7280;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  
  &__tags {
    display: flex;
    gap: 0.5rem;
    flex-shrink: 0;
  }
  
  &__meta {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    flex-shrink: 0;
  }
  
  &__actions {
    display: flex;
    gap: 0.5rem;
    flex-shrink: 0;
  }
}

.avatar-placeholder,
.avatar-placeholder-small {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #fee2e2 0%, #fecaca 100%);
  font-weight: 600;
  color: #dc2626;
}

.avatar-placeholder {
  font-size: 1.25rem;
}

.avatar-placeholder-small {
  font-size: 0.875rem;
}

.status-indicator {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 0.75rem;
  height: 0.75rem;
  background: #10b981;
  border: 2px solid white;
  border-radius: 50%;
}

.tag {
  padding: 0.25rem 0.5rem;
  background: rgba(220, 38, 38, 0.1);
  color: #dc2626;
  border-radius: 6px;
  font-size: 0.75rem;
  font-weight: 500;
}

.meta-text {
  font-size: 0.75rem;
  color: #6b7280;
}

.compact-card {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.2s;
  
  &:hover {
    background: rgba(255, 255, 255, 0.95);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    transform: translateY(-2px);
  }
  
  &__avatar {
    width: 2.5rem;
    height: 2.5rem;
    border-radius: 8px;
    overflow: hidden;
    flex-shrink: 0;
    
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }
  
  &__info {
    flex: 1;
    min-width: 0;
  }
  
  &__name {
    margin: 0 0 0.125rem;
    font-size: 0.875rem;
    font-weight: 600;
    color: #27272a;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  
  &__role {
    margin: 0;
    font-size: 0.75rem;
    color: #6b7280;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  
  &__connect {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 2rem;
    height: 2rem;
    background: #dc2626;
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    
    &:hover {
      background: #b91c1c;
      transform: scale(1.1);
    }
    
    &--connected {
      background: rgba(16, 185, 129, 0.1);
      color: #10b981;
      
      &:hover {
        background: rgba(16, 185, 129, 0.2);
      }
    }
  }
}

.load-more {
  max-width: 400px;
  margin: 2rem auto;
}

.admin-widget {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
  width: 280px;
  z-index: 10;
  
  &__title {
    margin: 0 0 1rem;
    font-size: 1rem;
    font-weight: 600;
    color: #dc2626;
    text-align: center;
  }
}

.admin-actions {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

// Responsive
@media (max-width: 768px) {
  .members-header {
    &__actions {
      width: 100%;
      
      button {
        flex: 1;
      }
    }
  }
  
  .members-controls {
    flex-direction: column;
    
    &__filters {
      width: 100%;
      
      .filter-select {
        flex: 1;
      }
    }
  }
  
  .member-list-item {
    flex-direction: column;
    align-items: flex-start;
    
    &__tags,
    &__meta {
      display: none;
    }
  }
  
  .admin-widget {
    display: none;
  }
  
  .members-table {
    font-size: 0.75rem;
    
    th, td {
      padding: 0.5rem;
    }
    
    .table-actions {
      flex-direction: column;
      gap: 0.25rem;
    }
  }
}
</style>