<template>
  <div class="toolbar">
    <div class="filters">
      <n-input 
        v-model:value="searchQueryModel" 
        :placeholder="t('Search')" 
        clearable 
        style="width: 200px" 
      />
    </div>
    <div class="view-toggle">
      <button
        class="toggle-btn"
        :class="{ active: viewModeModel === '2col' }"
        @click="viewModeModel = '2col'"
        :title="t_ctx('List')"
      >
        <Icon icon="tabler:layout-list" :size="16" />
      </button>
      <button
        class="toggle-btn"
        :class="{ active: viewModeModel === '3col' }"
        @click="viewModeModel = '3col'"
        :title="t_ctx('Preview')"
      >
        <Icon icon="tabler:layout-columns" :size="16" />
      </button>
    </div>
    <button class="refresh-btn" @click="reloadHandler" :disabled="loadingModel">
      <i :class="loadingModel ? 'fa fa-spinner fa-spin' : 'fa fa-refresh'"></i>
    </button>
    <button 
      v-if="selectedKeysModel.length === 0"
      class="create-btn email-compose-btn" 
      @click="openComposeEmail"
      :disabled="loadingModel"
    >
      <i class="fa fa-plus"></i>
      {{ t('New Email') }}
    </button>
    <button 
      v-else
      class="delete-btn" 
      @click="deleteSelectedHandler" 
      :disabled="loadingModel"
    >
      <i class="fa fa-trash"></i>
      {{ t('Delete Selected') }} ({{ selectedKeysModel.length }})
    </button>

    <!-- Email Compose Drawer -->
    <EmailComposeModal ref="composeRef" @sent="onEmailSent" />
  </div>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import { NInput } from 'naive-ui'
import { Icon } from '@iconify/vue'
import { t } from '@/shared/i18n'
import EmailComposeModal from '@/shared/components/EmailComposeModal.vue'

interface Props {
  context?: {
    entity?: string
    searchQuery?: string
    viewMode?: '2col' | '3col'
    selectedKeys?: string[]
    loading?: boolean
    canEdit?: boolean
    reload?: () => void
    createRecordHandler?: () => void
    handleDeleteSelected?: () => void
    router?: any
    t?: any
  }
  // Also support direct props (same as DefaultListToolbar)
  entity?: string
  searchQuery?: string
  viewMode?: '2col' | '3col'
  selectedKeys?: string[]
  loading?: boolean
  canEdit?: boolean
}

const props = defineProps<Props>()
const emit = defineEmits<{
  (e: 'update:searchQuery', value: string): void
  (e: 'update:viewMode', value: '2col' | '3col'): void
  (e: 'reload'): void
  (e: 'create'): void
  (e: 'delete-selected'): void
}>()

const composeRef = ref<InstanceType<typeof EmailComposeModal> | null>(null)

// 使用上下文的翻译函数（如果有），否则使用全局翻译
const t_ctx = computed(() => props.context?.t ?? t)

// Resolve values from context or direct props
const searchQueryModel = computed({
  get: () => props.context?.searchQuery ?? props.searchQuery ?? '',
  set: (value) => emit('update:searchQuery', value),
})
const viewModeModel = computed({
  get: () => props.context?.viewMode ?? props.viewMode ?? '3col',
  set: (value) => emit('update:viewMode', value),
})
const selectedKeysModel = computed(() => props.context?.selectedKeys ?? props.selectedKeys ?? [])
const loadingModel = computed(() => props.context?.loading ?? props.loading ?? false)

function reloadHandler() {
  if (props.context?.reload) props.context.reload()
  else emit('reload')
}

function deleteSelectedHandler() {
  if (props.context?.handleDeleteSelected) props.context.handleDeleteSelected()
  else emit('delete-selected')
}

function openComposeEmail() {
  composeRef.value?.open()
}

function onEmailSent(_data: any) {
  // Reload list to show the new email
  reloadHandler()
}
</script>

<style scoped>
.toolbar { 
  display: flex; 
  align-items: center; 
  gap: 12px; 
}

.filters { 
  display: flex; 
  gap: 8px; 
  align-items: center; 
}

.view-toggle { 
  display: flex; 
  background: #f8fafc; 
  border-radius: 8px; 
  padding: 2px; 
  border: 1px solid #e2e8f0;
}

.toggle-btn { 
  width: 32px; 
  height: 32px; 
  border: none; 
  background: transparent; 
  color: #6b7280; 
  cursor: pointer; 
  display: flex; 
  align-items: center; 
  justify-content: center; 
  border-radius: 6px; 
  transition: all 0.2s;
}

.toggle-btn:hover { 
  background: #f1f5f9; 
  color: #475569; 
}

.toggle-btn.active { 
  background: #e2e8f0; 
  color: #1e293b; 
}

.refresh-btn {
  width: 36px; 
  height: 36px; 
  border: none; 
  border-radius: 8px; 
  background: #f8fafc;
  color: #64748b; 
  cursor: pointer; 
  display: flex; 
  align-items: center;
  justify-content: center; 
  font-size: 14px; 
  transition: all 0.2s;
}

.refresh-btn:hover { 
  background: #e2e8f0; 
  color: #475569; 
}

.refresh-btn:disabled { 
  opacity: 0.6; 
  cursor: not-allowed; 
}

/* 新建邮件按钮 - 与默认列表页创建按钮风格一致 */
.email-compose-btn {
  height: 36px; 
  padding: 0 16px; 
  border: 1px solid #1fc76f; 
  border-radius: 8px; 
  background: #e6f8f0;
  color: #0d684b; 
  cursor: pointer; 
  display: flex; 
  align-items: center; 
  gap: 6px;
  font-size: 14px; 
  font-weight: 500; 
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative; 
  overflow: hidden;
}

.email-compose-btn:hover { 
  background: #dcfce7; 
  border-color: #1fc76f;
  color: #166534; 
  transform: translateY(-1px); 
  box-shadow: 0 2px 8px rgba(31, 199, 111, 0.15); 
}

.email-compose-btn:active { 
  background: #1fc76f; 
  border-color: #1fc76f;
  color: white;
  transform: translateY(0); 
  box-shadow: 0 1px 4px rgba(31, 199, 111, 0.2); 
}

.email-compose-btn:disabled { 
  background: #f1f5f9; 
  border-color: #e2e8f0;
  color: #94a3b8; 
  opacity: 0.6; 
  cursor: not-allowed; 
  transform: none; 
  box-shadow: none; 
}

.email-compose-btn i { 
  font-size: 12px; 
}

.delete-btn {
  background: #ef4444; 
  color: white; 
  border: none; 
  padding: 8px 16px; 
  border-radius: 6px;
  cursor: pointer; 
  font-size: 14px; 
  font-weight: 500; 
  display: flex; 
  align-items: center;
  gap: 6px; 
  transition: all 0.2s; 
}

.delete-btn:hover { 
  background: #dc2626; 
}

.delete-btn:disabled { 
  opacity: 0.5; 
  cursor: not-allowed; 
}
</style>
