<template>
  <header class="topbar" :class="{ 'chat-mode': isChat }">
    <div class="title-group">
      <div class="eyebrow">{{ isChat ? 'Smart Finance Q&A' : 'Smart Expense Operations' }}</div>
      <h1>{{ currentView.title }}</h1>
      <p>{{ currentView.desc }}</p>
    </div>

    <div class="top-actions">
      <template v-if="isChat">
        <div class="kpi-chips">
          <div v-for="kpi in chatKpis" :key="kpi.label" class="kpi-chip" :style="{ '--chip-color': kpi.color }">
            <span class="chip-value">{{ kpi.value }}<small>{{ kpi.unit }}</small></span>
            <span class="chip-label">{{ kpi.label }}</span>
            <span class="chip-delta" :class="kpi.trend">{{ kpi.meta }}</span>
          </div>
        </div>
      </template>

      <template v-else-if="isOverview">
        <div class="range-combo" aria-label="首页时间范围">
          <div class="range-shell">
            <span class="range-meta">
              <i class="mdi mdi-calendar"></i>
              <span>{{ activeDateLabel }}</span>
            </span>

            <div class="range-tabs" role="tablist" aria-label="时间范围">
              <button
                v-for="option in rangeOptions"
                :key="option.value"
                type="button"
                role="tab"
                :aria-selected="activeRange === option.value"
                :class="{ active: activeRange === option.value }"
                @click="setRange(option.value)"
              >
                {{ option.label }}
              </button>
            </div>
          </div>

          <div class="custom-range-wrap">
            <button
              class="custom-range-btn"
              type="button"
              :class="{ active: isCustomRange }"
              :aria-expanded="calendarOpen"
              aria-haspopup="dialog"
              @click="calendarOpen = !calendarOpen"
            >
              <i class="mdi mdi-calendar-plus"></i>
              <span>选择时间段</span>
            </button>

            <div v-if="calendarOpen" class="calendar-popover" role="dialog" aria-label="选择看板时间段">
              <header>
                <strong>选择看板时间段</strong>
                <button type="button" aria-label="关闭日期选择" @click="calendarOpen = false">
                  <i class="mdi mdi-close"></i>
                </button>
              </header>

              <div class="date-fields">
                <label>
                  <span>开始日期</span>
                  <input v-model="draftStart" type="date" />
                </label>
                <label>
                  <span>结束日期</span>
                  <input v-model="draftEnd" type="date" />
                </label>
              </div>

              <footer>
                <button class="ghost-btn" type="button" @click="calendarOpen = false">取消</button>
                <button class="apply-btn" type="button" :disabled="!canApplyCustomRange" @click="applyCustomRange">
                  应用
                </button>
              </footer>
            </div>
          </div>
        </div>
      </template>

      <template v-else-if="isRequestDetail">
        <div class="detail-alert-strip">
          <span
            v-for="alert in detailAlerts"
            :key="alert.label"
            class="detail-alert-pill"
            :class="alert.tone"
          >
            <i :class="alert.icon || 'mdi mdi-alert-circle-outline'"></i>
            <span>{{ alert.label }}</span>
          </span>
        </div>
      </template>

      <template v-else-if="isWorkbench">
        <div class="topbar-toolset" aria-label="工作台快捷工具">
          <button class="topbar-icon-btn notification-btn" type="button" aria-label="通知">
            <i class="mdi mdi-bell-outline"></i>
            <span v-if="topbarNotificationCount" class="notification-badge">{{ topbarNotificationCount }}</span>
          </button>

          <button class="topbar-icon-btn" type="button" aria-label="帮助">
            <i class="mdi mdi-help-circle-outline"></i>
          </button>

          <button class="company-switcher" type="button" aria-label="切换公司">
            <span>{{ displayCompanyName }}</span>
            <i class="mdi mdi-chevron-down"></i>
          </button>
        </div>
      </template>

      <template v-else-if="isDocuments">
        <div class="kpi-chips">
          <div v-for="kpi in documentKpis" :key="kpi.label" class="kpi-chip" :style="{ '--chip-color': kpi.color }">
            <span class="chip-value">{{ kpi.value }}<small>单</small></span>
            <span class="chip-label">{{ kpi.label }}</span>
            <span class="chip-delta" :class="kpi.trend">{{ kpi.delta }} <i :class="kpi.arrow"></i></span>
          </div>
        </div>
      </template>

      <template v-else-if="isRequests">
        <div class="kpi-chips">
          <div v-for="kpi in requestKpis" :key="kpi.label" class="kpi-chip" :style="{ '--chip-color': kpi.color }">
            <span class="chip-value">{{ kpi.value }}<small>单</small></span>
            <span class="chip-label">{{ kpi.label }}</span>
            <span class="chip-delta" :class="kpi.trend">{{ kpi.delta }} <i :class="kpi.arrow"></i></span>
          </div>
        </div>
      </template>

      <template v-else-if="isLogs">
        <div class="kpi-chips">
          <div v-for="kpi in logsKpis" :key="kpi.label" class="kpi-chip" :style="{ '--chip-color': kpi.color }">
            <span class="chip-value">{{ kpi.value }}<small>{{ kpi.unit }}</small></span>
            <span class="chip-label">{{ kpi.label }}</span>
            <span class="chip-delta" :class="kpi.trend">{{ kpi.meta }}</span>
          </div>
        </div>
      </template>

      <template v-else-if="isApproval">
        <div class="kpi-chips">
          <div v-for="kpi in approvalKpis" :key="kpi.label" class="kpi-chip" :style="{ '--chip-color': kpi.color }">
            <span class="chip-value">{{ kpi.value }}<small>{{ kpi.unit }}</small></span>
            <span class="chip-label">{{ kpi.label }}</span>
            <span class="chip-delta" :class="kpi.trend">{{ kpi.meta }}</span>
          </div>
        </div>
        <div class="topbar-spacer"></div>
        <button class="create-top-btn" type="button">
          <i class="mdi mdi-check-circle"></i>
          <span>批量通过</span>
        </button>
      </template>

      <template v-else-if="isPolicies">
        <div class="kpi-chips">
          <div v-for="kpi in knowledgeKpis" :key="kpi.label" class="kpi-chip" :style="{ '--chip-color': kpi.color }">
            <span class="chip-value">{{ kpi.value }}</span>
            <span class="chip-label">{{ kpi.label }}</span>
            <span v-if="kpi.meta" class="chip-delta" :class="kpi.trend">{{ kpi.meta }}</span>
          </div>
        </div>
      </template>

      <template v-else-if="isEmployees">
        <div class="kpi-chips">
          <div v-for="kpi in employeeKpis" :key="kpi.label" class="kpi-chip" :style="{ '--chip-color': kpi.color }">
            <span class="chip-value">{{ kpi.value }}<small>{{ kpi.unit }}</small></span>
            <span class="chip-label">{{ kpi.label }}</span>
            <span class="chip-delta" :class="kpi.trend">{{ kpi.meta }}</span>
          </div>
        </div>
      </template>
    </div>
  </header>
</template>

<script setup>
import { computed, ref, watch } from 'vue'

const props = defineProps({
  currentView: { type: Object, required: true },
  search: { type: String, default: '' },
  activeView: { type: String, default: '' },
  ranges: { type: Array, default: () => [] },
  activeRange: { type: String, default: '' },
  employeeSummary: {
    type: Object,
    default: () => null
  },
  knowledgeSummary: {
    type: Object,
    default: () => null
  },
  logsSummary: {
    type: Object,
    default: () => null
  },
  requestSummary: {
    type: Object,
    default: () => null
  },
  documentSummary: {
    type: Object,
    default: () => null
  },
  companyName: {
    type: String,
    default: ''
  },
  detailMode: {
    type: Boolean,
    default: false
  },
  logDetailMode: {
    type: Boolean,
    default: false
  },
  detailAlerts: {
    type: Array,
    default: () => []
  },
  customRange: {
    type: Object,
    default: () => ({ start: '2024-07-06', end: '2024-07-12' })
  }
})

const emit = defineEmits([
  'update:search',
  'update:activeRange',
  'update:customRange',
  'batchApprove',
  'openChat',
  'newApplication'
])

const isChat = computed(() => props.activeView === 'chat')
const isOverview = computed(() => props.activeView === 'overview')
const isWorkbench = computed(() => props.activeView === 'workbench')
const isRequestDetail = computed(() => ['requests', 'documents'].includes(props.activeView) && props.detailMode)
const isDocuments = computed(() => props.activeView === 'documents' && !props.detailMode)
const isRequests = computed(() => props.activeView === 'requests')
const isLogs = computed(() => props.activeView === 'logs' && !props.logDetailMode)
const isApproval = computed(() => props.activeView === 'approval')
const isPolicies = computed(() => props.activeView === 'policies')
const isEmployees = computed(() => props.activeView === 'employees')
const displayCompanyName = computed(() => String(props.companyName || '远光软件股份有限公司').trim() || '远光软件股份有限公司')
const topbarNotificationCount = computed(() => {
  const summary = props.documentSummary ?? {}
  const count = Number(summary.toProcess ?? summary.toSubmit ?? 8)
  return Number.isFinite(count) && count > 0 ? Math.min(count, 99) : 0
})

const requestKpis = computed(() => {
  const summary = props.requestSummary ?? {}
  const total = Number(summary.total ?? 0)
  const draft = Number(summary.draft ?? 0)
  const inProgress = Number(summary.inProgress ?? 0)
  const completed = Number(summary.completed ?? 0)

  return [
    { label: '全部单据', value: total, delta: '当前', trend: 'up', arrow: 'mdi mdi-minus', color: 'var(--theme-primary)' },
    { label: '草稿', value: draft, delta: '待提交', trend: draft > 0 ? 'down' : 'up', arrow: draft > 0 ? 'mdi mdi-arrow-down' : 'mdi mdi-minus', color: '#f59e0b' },
    { label: '审批中', value: inProgress, delta: '处理中', trend: inProgress > 0 ? 'up' : 'down', arrow: inProgress > 0 ? 'mdi mdi-arrow-up' : 'mdi mdi-minus', color: '#3b82f6' },
    { label: '已完成', value: completed, delta: '已归档', trend: 'up', arrow: 'mdi mdi-arrow-up' , color: 'var(--success)' }
  ]
})

const documentKpis = computed(() => {
  const summary = props.documentSummary ?? {}
  const total = Number(summary.total ?? 0)
  const toSubmit = Number(summary.toSubmit ?? 0)
  const toProcess = Number(summary.toProcess ?? 0)
  const archived = Number(summary.archived ?? 0)

  return [
    { label: '全部单据', value: total, delta: '当前', trend: 'up', arrow: 'mdi mdi-minus', color: 'var(--theme-primary)' },
    { label: '待提交', value: toSubmit, delta: '草稿待办', trend: toSubmit > 0 ? 'down' : 'up', arrow: toSubmit > 0 ? 'mdi mdi-arrow-down' : 'mdi mdi-minus', color: '#f59e0b' },
    { label: '待我处理', value: toProcess, delta: '审批待办', trend: toProcess > 0 ? 'down' : 'up', arrow: toProcess > 0 ? 'mdi mdi-arrow-down' : 'mdi mdi-minus', color: '#3b82f6' },
    { label: '已归档', value: archived, delta: '归档入账', trend: 'up', arrow: 'mdi mdi-arrow-up', color: 'var(--success)' }
  ]
})

const logsKpis = computed(() => {
  const summary = props.logsSummary ?? {}
  const total = Number(summary.total ?? 0)
  const errors = Number(summary.errors ?? 0)
  const warnings = Number(summary.warnings ?? 0)
  const info = Number(summary.info ?? 0)

  return [
    { label: '系统日志', value: total, unit: '条', meta: '当前', trend: 'up', color: 'var(--theme-primary)' },
    { label: '错误数量', value: errors, unit: '条', meta: errors > 0 ? '需要关注' : '运行正常', trend: errors > 0 ? 'down' : 'up', color: '#ef4444' },
    { label: '告警数量', value: warnings, unit: '条', meta: warnings > 0 ? '建议排查' : '暂无告警', trend: warnings > 0 ? 'down' : 'up', color: '#f59e0b' },
    { label: '正常数量', value: info, unit: '条', meta: total ? `占比 ${Math.round((info / total) * 100)}%` : '等待数据', trend: 'up', color: 'var(--success)' }
  ]
})

const chatKpis = [
  { label: '今日已问数', value: 86, unit: '次', meta: '较昨日 +18', trend: 'up', color: 'var(--theme-primary)' },
  { label: '已解决问题', value: 72, unit: '条', meta: '解决率 83.7%', trend: 'up', color: '#3b82f6' },
  { label: '知识命中率', value: '92.3', unit: '%', meta: '较昨日 +2.6%', trend: 'up', color: '#8b5cf6' },
  { label: '平均响应时长', value: 2.1, unit: 's', meta: '较昨日 -0.3s', trend: 'down', color: '#f59e0b' }
]

const approvalKpis = [
  { label: '待审批单据', value: 12, unit: '单', meta: '较昨日 +3', trend: 'up', color: 'var(--theme-primary)' },
  { label: '高风险单据', value: 4, unit: '单', meta: '较昨日 +1', trend: 'up', color: '#ef4444' },
  { label: '即将超时', value: 3, unit: '单', meta: '30 分钟内', trend: 'down', color: '#f59e0b' },
  { label: '今日已处理', value: 28, unit: '单', meta: '通过率 86%', trend: 'up', color: 'var(--success)' }
]

const knowledgeKpis = computed(() => {
  const summary = props.knowledgeSummary ?? {}
  const totalDocuments = Number(summary.totalDocuments ?? 0)

  return [
    {
      label: '文档总数',
      value: String(totalDocuments),
      meta: '',
      trend: 'up',
      color: 'var(--theme-primary)'
    }
  ]
})

const employeeKpis = computed(() => {
  const summary = props.employeeSummary ?? {}
  const total = Number(summary.total ?? 0)
  const active = Number(summary.active ?? 0)
  const onboarding = Number(summary.onboarding ?? 0)
  const disabled = Number(summary.disabled ?? 0)
  const followUp = Number(summary.followUp ?? 0)
  const departments = Number(summary.departments ?? 0)

  return [
    {
      label: '员工总数',
      value: total,
      unit: '人',
      meta: `覆盖 ${departments} 个部门`,
      trend: 'up',
      color: 'var(--theme-primary)'
    },
    {
      label: '在职账号',
      value: active,
      unit: '人',
      meta: total ? `占比 ${Math.round((active / total) * 100)}%` : '等待数据',
      trend: 'up',
      color: '#3b82f6'
    },
    {
      label: '待处理状态',
      value: onboarding + disabled,
      unit: '人',
      meta: `试用 ${onboarding} / 停用 ${disabled}`,
      trend: onboarding + disabled > 0 ? 'down' : 'up',
      color: '#f59e0b'
    },
    {
      label: '同步待处理',
      value: followUp,
      unit: '人',
      meta: followUp > 0 ? '存在待同步账号' : '资料已同步',
      trend: followUp > 0 ? 'down' : 'up',
      color: '#8b5cf6'
    }
  ]
})
const calendarOpen = ref(false)
const draftStart = ref(props.customRange.start)
const draftEnd = ref(props.customRange.end)

const rangeOptions = computed(() =>
  props.ranges.map((range, index) => ({
    value: range,
    label: String(range)
  }))
)

const activeOption = computed(() =>
  rangeOptions.value.find((option) => option.value === props.activeRange) ?? rangeOptions.value[0]
)

const isCustomRange = computed(() => props.activeRange === 'custom')
const activeDateLabel = computed(() => {
  if (isCustomRange.value) return formatRangeLabel(props.customRange.start, props.customRange.end)
  return buildPresetRangeLabel(activeOption.value?.label)
})

const canApplyCustomRange = computed(() =>
  Boolean(draftStart.value && draftEnd.value && draftStart.value <= draftEnd.value)
)

watch(
  () => props.customRange,
  (range) => {
    draftStart.value = range.start
    draftEnd.value = range.end
  },
  { deep: true }
)

function setRange(range) {
  emit('update:activeRange', range)
  calendarOpen.value = false
}

function applyCustomRange() {
  if (!canApplyCustomRange.value) return
  emit('update:customRange', { start: draftStart.value, end: draftEnd.value })
  emit('update:activeRange', 'custom')
  calendarOpen.value = false
}

function formatRangeLabel(start, end) {
  if (!start || !end) return '选择时间段'
  if (start === end) return start
  return `${start} ~ ${end}`
}

function toDateLabel(date) {
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  return `${year}-${month}-${day}`
}

function buildPresetRangeLabel(label) {
  const now = new Date()
  const today = toDateLabel(now)

  if (label === '今日') {
    return today
  }

  if (label === '近10日') {
    const start = new Date(now)
    start.setHours(0, 0, 0, 0)
    start.setDate(start.getDate() - 9)
    return `${toDateLabel(start)} ~ ${today}`
  }

  if (label === '本周') {
    const start = new Date(now)
    const day = start.getDay() || 7
    start.setHours(0, 0, 0, 0)
    start.setDate(start.getDate() - day + 1)
    return `${toDateLabel(start)} ~ ${today}`
  }

  if (label === '本月') {
    return `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}`
  }

  return today
}
</script>

<style scoped src="../../assets/styles/components/top-bar.css"></style>
