{# app/templates/store/team.html #}
{% extends "store/base.html" %}
{% from 'shared/macros/headers.html' import page_header_flex, refresh_button %}
{% from 'shared/macros/alerts.html' import loading_state, error_state %}
{% from 'shared/macros/modals.html' import modal_simple %}
{% from 'shared/macros/tables.html' import table_wrapper %}

{% block title %}Team{% endblock %}

{% block alpine_data %}storeTeam(){% endblock %}

{% block content %}
<!-- Page Header -->
{% call page_header_flex(title='Team', subtitle='Manage your team members and roles') %}
    <div class="flex items-center gap-4">
        {{ refresh_button(loading_var='loading', onclick='loadMembers()', variant='secondary') }}
        <button
            @click="openInviteModal()"
            class="flex items-center px-4 py-2 text-sm font-medium leading-5 text-white transition-colors duration-150 bg-purple-600 border border-transparent rounded-lg hover:bg-purple-700 focus:outline-none focus:shadow-outline-purple"
        >
            <span x-html="$icon('user-plus', 'w-4 h-4 mr-2')"></span>
            Invite Member
        </button>
    </div>
{% endcall %}

{{ loading_state('Loading team...') }}

{{ error_state('Error loading team') }}

<!-- Stats Cards -->
<div x-show="!loading" class="grid gap-6 mb-8 md:grid-cols-3">
    <!-- Total Members -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-purple-500 bg-purple-100 rounded-full dark:text-purple-100 dark:bg-purple-500">
            <span x-html="$icon('users', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">Total Members</p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.total">0</p>
        </div>
    </div>

    <!-- Active Members -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-green-500 bg-green-100 rounded-full dark:text-green-100 dark:bg-green-500">
            <span x-html="$icon('check-circle', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">Active</p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.active_count">0</p>
        </div>
    </div>

    <!-- Pending Invitations -->
    <div class="flex items-center p-4 bg-white rounded-lg shadow-xs dark:bg-gray-800">
        <div class="p-3 mr-4 text-yellow-500 bg-yellow-100 rounded-full dark:text-yellow-100 dark:bg-yellow-500">
            <span x-html="$icon('clock', 'w-5 h-5')"></span>
        </div>
        <div>
            <p class="mb-2 text-sm font-medium text-gray-600 dark:text-gray-400">Pending Invitations</p>
            <p class="text-lg font-semibold text-gray-700 dark:text-gray-200" x-text="stats.pending_invitations">0</p>
        </div>
    </div>
</div>

<!-- Team Members Table -->
<div x-show="!loading && !error" class="mb-8">
{% call table_wrapper() %}
    <thead>
        <tr class="text-xs font-semibold tracking-wide text-left text-gray-500 uppercase border-b dark:border-gray-700 bg-gray-50 dark:text-gray-400 dark:bg-gray-800">
            <th class="px-4 py-3">Member</th>
            <th class="px-4 py-3">Role</th>
            <th class="px-4 py-3">Status</th>
            <th class="px-4 py-3">Joined</th>
            <th class="px-4 py-3">Actions</th>
        </tr>
    </thead>
    <tbody class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-800">
        <template x-for="member in members" :key="member.id">
            <tr class="text-gray-700 dark:text-gray-400">
                <!-- Member Info -->
                <td class="px-4 py-3">
                    <div class="flex items-center text-sm">
                        <div class="relative w-10 h-10 mr-3 rounded-full overflow-hidden bg-purple-100 dark:bg-purple-900 flex items-center justify-center">
                            <span class="text-sm font-semibold text-purple-600 dark:text-purple-300" x-text="getInitials(member)"></span>
                        </div>
                        <div>
                            <p class="font-semibold" x-text="`${member.first_name || ''} ${member.last_name || ''}`.trim() || member.email"></p>
                            <p class="text-xs text-gray-500 dark:text-gray-400" x-text="member.email"></p>
                        </div>
                    </div>
                </td>
                <!-- Role -->
                <td class="px-4 py-3 text-sm">
                    <span
                        class="px-2 py-1 font-semibold leading-tight text-purple-700 bg-purple-100 rounded-full dark:bg-purple-700 dark:text-purple-100"
                        x-text="getRoleName(member)"
                    ></span>
                </td>
                <!-- Status -->
                <td class="px-4 py-3 text-xs">
                    <span
                        :class="{
                            'px-2 py-1 font-semibold leading-tight rounded-full': true,
                            'text-green-700 bg-green-100 dark:bg-green-700 dark:text-green-100': member.is_active && !member.invitation_pending,
                            'text-yellow-700 bg-yellow-100 dark:bg-yellow-700 dark:text-yellow-100': member.invitation_pending,
                            'text-gray-700 bg-gray-100 dark:bg-gray-700 dark:text-gray-100': !member.is_active
                        }"
                        x-text="member.invitation_pending ? 'Pending' : (member.is_active ? 'Active' : 'Inactive')"
                    ></span>
                </td>
                <!-- Joined -->
                <td class="px-4 py-3 text-sm" x-text="formatDate(member.joined_at)"></td>
                <!-- Actions -->
                <td class="px-4 py-3">
                    <div class="flex items-center space-x-2 text-sm">
                        <!-- Resend invitation - pending only -->
                        <button
                            @click="resendInvitation(member)"
                            :disabled="saving"
                            class="p-1 text-gray-500 hover:text-green-600 dark:text-gray-400 dark:hover:text-green-400"
                            title="Resend Invitation"
                            x-show="member.invitation_pending"
                        >
                            <span x-html="$icon('paper-airplane', 'w-5 h-5')"></span>
                        </button>
                        <!-- Edit button - not for owners -->
                        <button
                            @click="openEditModal(member)"
                            class="p-1 text-gray-500 hover:text-blue-600 dark:text-gray-400 dark:hover:text-blue-400"
                            title="Edit"
                            x-show="!member.is_owner"
                        >
                            <span x-html="$icon('pencil', 'w-5 h-5')"></span>
                        </button>
                        <!-- Owner badge -->
                        <span x-show="member.is_owner" class="text-xs text-gray-400 dark:text-gray-500 italic">Owner</span>
                        <!-- Remove button - not for owners -->
                        <button
                            @click="confirmRemove(member)"
                            class="p-1 text-gray-500 hover:text-red-600 dark:text-gray-400 dark:hover:text-red-400"
                            title="Remove"
                            x-show="!member.is_owner"
                        >
                            <span x-html="$icon('trash', 'w-5 h-5')"></span>
                        </button>
                    </div>
                </td>
            </tr>
        </template>
        <!-- Empty State -->
        <tr x-show="members.length === 0">
            <td colspan="5" class="px-4 py-8 text-center text-gray-500 dark:text-gray-400">
                <div class="flex flex-col items-center">
                    <span x-html="$icon('users', 'w-12 h-12 text-gray-300 dark:text-gray-600 mb-4')"></span>
                    <p class="text-lg font-medium">No team members yet</p>
                    <p class="text-sm">Invite your first team member to get started</p>
                    <button
                        @click="openInviteModal()"
                        class="mt-4 px-4 py-2 text-sm font-medium text-white bg-purple-600 rounded-lg hover:bg-purple-700"
                    >
                        Invite Member
                    </button>
                </div>
            </td>
        </tr>
    </tbody>
{% endcall %}
</div>

<!-- Invite Modal -->
<div x-show="showInviteModal" x-cloak class="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black bg-opacity-50">
    <div class="w-full max-w-md bg-white rounded-lg shadow-xl dark:bg-gray-800" @click.away="showInviteModal = false">
        <div class="flex items-center justify-between p-4 border-b dark:border-gray-700">
            <h3 class="text-lg font-semibold text-gray-800 dark:text-gray-200">Invite Team Member</h3>
            <button @click="showInviteModal = false" class="text-gray-400 hover:text-gray-600 dark:hover:text-gray-200">
                <span x-html="$icon('x', 'w-5 h-5')"></span>
            </button>
        </div>
        <div class="p-4">
            <div class="mb-4">
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Email *</label>
                <input
                    type="email"
                    x-model="inviteForm.email"
                    class="w-full px-4 py-2 text-sm text-gray-700 bg-gray-50 border border-gray-200 rounded-lg dark:text-gray-300 dark:bg-gray-700 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
                    placeholder="colleague@example.com"
                />
            </div>
            <div class="grid grid-cols-2 gap-4 mb-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">First Name</label>
                    <input
                        type="text"
                        x-model="inviteForm.first_name"
                        class="w-full px-4 py-2 text-sm text-gray-700 bg-gray-50 border border-gray-200 rounded-lg dark:text-gray-300 dark:bg-gray-700 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
                    />
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Last Name</label>
                    <input
                        type="text"
                        x-model="inviteForm.last_name"
                        class="w-full px-4 py-2 text-sm text-gray-700 bg-gray-50 border border-gray-200 rounded-lg dark:text-gray-300 dark:bg-gray-700 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
                    />
                </div>
            </div>
            <div class="mb-4">
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Role</label>
                <select
                    x-model="inviteForm.role_name"
                    class="w-full px-4 py-2 text-sm text-gray-700 bg-gray-50 border border-gray-200 rounded-lg dark:text-gray-300 dark:bg-gray-700 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
                >
                    <template x-for="role in roleOptions" :key="role.value">
                        <option :value="role.value" x-text="role.label"></option>
                    </template>
                </select>
                <p class="mt-1 text-xs text-gray-500 dark:text-gray-400" x-text="roleOptions.find(r => r.value === inviteForm.role_name)?.description"></p>
            </div>
        </div>
        <div class="flex justify-end gap-2 p-4 border-t dark:border-gray-700">
            <button @click="showInviteModal = false" class="px-4 py-2 text-sm text-gray-600 hover:text-gray-800 dark:text-gray-400 dark:hover:text-gray-200">
                Cancel
            </button>
            <button
                @click="sendInvitation()"
                :disabled="saving || !inviteForm.email"
                class="px-4 py-2 text-sm font-medium text-white bg-purple-600 rounded-lg hover:bg-purple-700 disabled:opacity-50"
            >
                <span x-show="!saving">Send Invitation</span>
                <span x-show="saving">Sending...</span>
            </button>
        </div>
    </div>
</div>

<!-- Edit Modal -->
{% call modal_simple('editTeamMemberModal', 'Edit Team Member', show_var='showEditModal', size='sm') %}
<div class="space-y-4">
    <template x-if="selectedMember">
        <div>
            <p class="text-sm text-gray-600 dark:text-gray-400 mb-4">
                Edit <span class="font-semibold" x-text="selectedMember.email"></span>
            </p>
            <div class="mb-4">
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Role</label>
                <select
                    x-model="editForm.role_id"
                    class="w-full px-4 py-2 text-sm text-gray-700 bg-gray-50 border border-gray-200 rounded-lg dark:text-gray-300 dark:bg-gray-700 dark:border-gray-600 focus:border-purple-400 focus:outline-none focus:ring-1 focus:ring-purple-400"
                >
                    <template x-for="role in roles" :key="role.id">
                        <option :value="role.id" :selected="role.id === editForm.role_id" x-text="role.name"></option>
                    </template>
                </select>
            </div>
            <div class="flex items-center">
                <input type="checkbox" x-model="editForm.is_active" id="is_active" class="w-4 h-4 text-purple-600 rounded focus:ring-purple-500" />
                <label for="is_active" class="ml-2 text-sm text-gray-700 dark:text-gray-300">Active</label>
            </div>
        </div>
    </template>
    <div class="flex items-center justify-end gap-3 pt-4 border-t border-gray-200 dark:border-gray-700">
        <button
            @click="showEditModal = false"
            class="px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-600 transition-colors"
        >Cancel</button>
        <button
            @click="updateMember()"
            :disabled="saving"
            class="px-4 py-2 text-sm font-medium text-white bg-purple-600 rounded-lg hover:bg-purple-700 transition-colors disabled:opacity-50"
        >Save</button>
    </div>
</div>
{% endcall %}

<!-- Remove Confirmation Modal -->
{% call modal_simple('removeTeamMemberModal', 'Remove Team Member', show_var='showRemoveModal', size='sm') %}
<div class="space-y-4">
    <template x-if="selectedMember">
        <p class="text-sm text-gray-600 dark:text-gray-400">
            Are you sure you want to remove <span class="font-semibold" x-text="selectedMember.email"></span> from the team?
            They will lose access to this store.
        </p>
    </template>
    <div class="flex items-center justify-end gap-3 pt-4 border-t border-gray-200 dark:border-gray-700">
        <button
            @click="showRemoveModal = false"
            class="px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-600 transition-colors"
        >Cancel</button>
        <button
            @click="removeMember()"
            :disabled="saving"
            class="px-4 py-2 text-sm font-medium text-white bg-red-600 rounded-lg hover:bg-red-700 transition-colors disabled:opacity-50"
        >Remove</button>
    </div>
</div>
{% endcall %}
{% endblock %}

{% block extra_scripts %}
<script defer src="{{ url_for('tenancy_static', path='store/js/team.js') }}"></script>
{% endblock %}
