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

    <div class="top-actions">
      <span v-if="isChat" class="search-wrap" :class="{ wide: isChat }">
        <i class="mdi mdi-magnify search-icon"></i>
        <input
          :value="search"
          type="search"
          :placeholder="isChat ? '搜索报销单、发票、单号、部门...' : '搜索申请人、单号、费用类型...'"
          @input="emit('update:search', $event.target.value)"
        />
      </span>

      <template v-if="isChat">
        <button class="icon-btn notification" type="button" aria-label="查看通知">
          <i class="mdi mdi-bell-outline"></i>
          <span>1</span>
        </button>
        <button class="profile-btn" type="button" aria-label="打开用户菜单">
          <span class="profile-avatar">张</span>
          <span class="profile-copy">
            <strong>张晓明</strong>
            <small>财务管理员</small>
          </span>
          <i class="mdi mdi-chevron-down"></i>
        </button>
      </template>

      <template v-else>
        <div v-if="isOverview" 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 v-if="isPolicies || isRequests"></template>

        <template v-else-if="isApproval">
          <button class="month-chip" type="button" aria-label="筛选审批日期">
            <i class="mdi mdi-calendar"></i>
            <span>筛选 / 日期</span>
            <i class="mdi mdi-chevron-down"></i>
          </button>
          <button class="icon-btn notification" type="button" aria-label="查看通知">
            <i class="mdi mdi-bell-outline"></i>
            <span>9</span>
          </button>
          <button class="profile-btn" type="button" aria-label="打开用户菜单">
            <span class="profile-avatar">张</span>
            <span class="profile-copy">
              <strong>张晓明</strong>
              <small>财务管理部</small>
            </span>
            <i class="mdi mdi-chevron-down"></i>
          </button>
        </template>

        <template v-else-if="isRequests"></template>

      </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: '' },
  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 isRequests = computed(() => props.activeView === 'requests')
const isApproval = computed(() => props.activeView === 'approval')
const isPolicies = computed(() => props.activeView === 'policies')
const calendarOpen = ref(false)
const draftStart = ref(props.customRange.start)
const draftEnd = ref(props.customRange.end)

const fallbackRangeLabels = ['今日', '本周', '本月']
const dateLabels = {
  '今日': '2024-07-12',
  '本周': '2024-07-06 ~ 2024-07-12',
  '本月': '2024-07'
}

const rangeOptions = computed(() =>
  props.ranges.map((range, index) => ({
    value: range,
    label: fallbackRangeLabels[index] ?? 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 dateLabels[activeOption.value?.label] ?? '2024-07-06 ~ 2024-07-12'
})

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 applyDraft(start, end) {
  draftStart.value = start
  draftEnd.value = end
}

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}`
}
</script>

<style scoped>
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 18px 24px 20px;
  background: #fff;
}

.topbar.chat-mode {
  padding-bottom: 16px;
  border-bottom: 1px solid #eef2f7;
}

.title-group {
  min-width: 0;
}

.eyebrow {
  display: inline-block;
  margin-bottom: 8px;
  padding: 3px 10px;
  border-radius: 6px;
  background: linear-gradient(135deg, rgba(16, 185, 129, 0.10), rgba(59, 130, 246, 0.10));
  color: #0d9668;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 1.2px;
  text-transform: uppercase;
}

.topbar h1 {
  color: #0f172a;
  font-size: 26px;
  font-weight: 800;
  letter-spacing: 0;
  line-height: 1.2;
}

.topbar p {
  margin-top: 6px;
  max-width: 720px;
  color: #64748b;
  font-size: 14px;
  line-height: 1.5;
}

.top-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 14px;
  flex-wrap: wrap;
}

.search-wrap {
  position: relative;
  width: 256px;
}

.search-wrap.wide {
  width: min(340px, 28vw);
}

.search-icon {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: #64748b;
  font-size: 15px;
}

.search-wrap input {
  width: 100%;
  height: 42px;
  padding: 0 14px 0 40px;
  border: 1px solid #d7e0ea;
  border-radius: 8px;
  background: #fff;
  color: #0f172a;
  font-size: 14px;
  transition: border-color 160ms ease, box-shadow 160ms ease;
}

.search-wrap input::placeholder {
  color: #8da0b4;
}

.search-wrap input:focus {
  border-color: #10b981;
  box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.14);
  outline: none;
}

.range-combo {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.range-shell {
  height: 42px;
  display: inline-flex;
  align-items: center;
  padding: 3px;
  border: 1px solid #d7e0ea;
  border-radius: 10px;
  background: #fff;
  box-shadow: 0 1px 2px rgba(15, 23, 42, .04);
}

.range-meta {
  height: 34px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  border-right: 1px solid #e2e8f0;
  color: #334155;
  font-size: 13px;
  font-weight: 650;
  white-space: nowrap;
}

.range-meta .mdi {
  color: #10b981;
}

.range-tabs {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding-left: 3px;
}

.range-tabs button {
  height: 34px;
  min-width: 54px;
  padding: 0 14px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: #64748b;
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
  transition: background 160ms ease, color 160ms ease, box-shadow 160ms ease;
}

.range-tabs button:hover:not(.active) {
  background: #f1f5f9;
  color: #334155;
}

.range-tabs button.active {
  background: #10b981;
  color: #fff;
  box-shadow: 0 6px 14px rgba(16, 185, 129, .18);
}

.custom-range-wrap {
  position: relative;
}

.custom-range-btn {
  height: 42px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 0 13px;
  border: 1px solid #d7e0ea;
  border-radius: 10px;
  background: #fff;
  color: #334155;
  font-size: 13px;
  font-weight: 750;
  white-space: nowrap;
  transition: border-color 160ms ease, background 160ms ease, color 160ms ease, box-shadow 160ms ease;
}

.custom-range-btn:hover,
.custom-range-btn.active {
  border-color: rgba(16, 185, 129, .34);
  background: #f6fffb;
  color: #0f9f78;
}

.custom-range-btn.active {
  box-shadow: 0 0 0 3px rgba(16, 185, 129, .10);
}

.calendar-popover {
  position: absolute;
  top: calc(100% + 10px);
  right: 0;
  width: 336px;
  z-index: 40;
  display: grid;
  gap: 14px;
  padding: 16px;
  border: 1px solid #d7e0ea;
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 18px 42px rgba(15, 23, 42, .16);
}

.calendar-popover::before {
  content: "";
  position: absolute;
  top: -6px;
  right: 30px;
  width: 12px;
  height: 12px;
  border-top: 1px solid #d7e0ea;
  border-left: 1px solid #d7e0ea;
  background: #fff;
  transform: rotate(45deg);
}

.calendar-popover header,
.calendar-popover footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.calendar-popover header strong {
  color: #0f172a;
  font-size: 15px;
}

.calendar-popover header button {
  width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: #64748b;
}

.calendar-popover header button:hover {
  background: #f1f5f9;
  color: #0f172a;
}

.date-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.date-fields label {
  display: grid;
  gap: 6px;
}

.date-fields span {
  color: #64748b;
  font-size: 12px;
  font-weight: 700;
}

.date-fields input {
  width: 100%;
  height: 38px;
  padding: 0 9px;
  border: 1px solid #d7e0ea;
  border-radius: 8px;
  color: #0f172a;
  font-size: 13px;
}

.date-fields input:focus {
  border-color: #10b981;
  box-shadow: 0 0 0 3px rgba(16, 185, 129, .12);
  outline: none;
}

.ghost-btn,
.apply-btn {
  height: 36px;
  padding: 0 14px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 750;
}

.ghost-btn {
  border: 1px solid #d7e0ea;
  background: #fff;
  color: #334155;
}

.apply-btn {
  border: 0;
  background: #10b981;
  color: #fff;
}

.apply-btn:disabled {
  cursor: not-allowed;
  background: #cbd5e1;
}

.month-chip {
  height: 42px;
  display: inline-flex;
  align-items: center;
  gap: 9px;
  padding: 0 14px;
  border: 1px solid #d7e0ea;
  border-radius: 8px;
  background: #fff;
  color: #334155;
  font-size: 14px;
  font-weight: 750;
}

.month-chip .mdi:first-child {
  color: #64748b;
}

.month-chip:hover {
  border-color: rgba(16, 185, 129, .32);
  color: #0f9f78;
}

.icon-btn,
.profile-btn {
  border: 0;
  background: transparent;
}

.icon-btn {
  position: relative;
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  color: #334155;
  font-size: 20px;
}

.icon-btn:hover,
.profile-btn:hover {
  background: #f3f7fa;
}

.notification span {
  position: absolute;
  top: 4px;
  right: 5px;
  min-width: 18px;
  height: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 2px solid #fff;
  border-radius: 999px;
  background: #ff3b45;
  color: #fff;
  font-size: 11px;
  font-weight: 800;
  line-height: 1;
}

.profile-btn {
  min-height: 46px;
  display: inline-grid;
  grid-template-columns: 38px minmax(0, 1fr) 18px;
  align-items: center;
  gap: 10px;
  padding: 4px 6px;
  border-radius: 10px;
  color: #334155;
  text-align: left;
}

.profile-avatar {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  background: linear-gradient(135deg, #203047, #60758a);
  color: #fff;
  font-size: 14px;
  font-weight: 800;
}

.profile-copy {
  display: grid;
  gap: 2px;
}

.profile-copy strong {
  color: #172033;
  font-size: 14px;
  font-weight: 800;
}

.profile-copy small {
  color: #64748b;
  font-size: 12px;
}

.profile-btn .mdi {
  color: #64748b;
}

@media (max-width: 1120px) {
  .range-combo {
    width: 100%;
    justify-content: flex-end;
  }
}

@media (max-width: 960px) {
  .topbar {
    flex-direction: column;
    align-items: stretch;
  }

  .top-actions,
  .search-wrap,
  .search-wrap.wide,
  .date-chip,
  .month-chip,
  .top-btn {
    width: 100%;
  }

  .range-combo {
    justify-content: stretch;
  }

  .range-shell {
    flex: 1;
  }

  .range-meta {
    flex: 1;
  }

  .custom-range-btn {
    width: 100%;
    justify-content: center;
  }

  .calendar-popover {
    right: auto;
    left: 0;
  }

  .profile-btn {
    justify-content: start;
  }
}

@media (max-width: 640px) {
  .range-combo {
    display: grid;
    gap: 8px;
  }

  .range-shell {
    height: auto;
    display: grid;
    gap: 4px;
  }

  .range-meta {
    width: 100%;
    border-right: 0;
    border-bottom: 1px solid #e2e8f0;
    justify-content: center;
  }

  .range-tabs {
    width: 100%;
    padding-left: 0;
  }

  .range-tabs button {
    flex: 1;
    padding: 0 8px;
  }

  .calendar-popover {
    width: min(336px, calc(100vw - 32px));
  }

  .date-fields {
    grid-template-columns: 1fr;
  }
}
</style>
