<template>
  <div class="error-page">
    <div class="error-card glass">
      <div class="error-visual" :class="{ forbidden: codeText === '403' }">
        <div class="error-code">{{ codeText }}</div>
        <!-- <div class="error-badge">
          <el-icon :size="18">
            <component :is="iconComponent" />
          </el-icon>
          <span class="error-badge-text">{{ badgeText }}</span>
        </div> -->
      </div>

      <div class="error-content">
        <div class="error-title">{{ displayTitle }}</div>
        <div class="error-subtitle">{{ displaySubTitle }}</div>
        <!-- <div class="error-actions">
          <el-button type="primary" @click="goHome">
            <el-icon class="btn-icon"><House /></el-icon>
            返回首页
          </el-button>
          <el-button @click="goBack">
            <el-icon class="btn-icon"><ArrowLeft /></el-icon>
            返回上一页
          </el-button>
          <el-button v-if="codeText === '403'" type="danger" plain @click="switchAccount">
            <el-icon class="btn-icon"><SwitchButton /></el-icon>
            登录
          </el-button>
        </div> -->
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useAuthStore } from '../stores/auth'
import { ArrowLeft, CircleCloseFilled, House, SwitchButton, WarningFilled } from '@element-plus/icons-vue'

const props = defineProps({
  code: { type: [String, Number], default: '404' },
  title: { type: String, default: '' },
  subTitle: { type: String, default: '' },
})

const auth = useAuthStore()
const route = useRoute()
const router = useRouter()

const codeText = computed(() => String(props.code || '404'))

const badgeText = computed(() => {
  if (codeText.value === '403') return '无权限'
  return '未找到'
})

const iconComponent = computed(() => {
  if (codeText.value === '403') return CircleCloseFilled
  return WarningFilled
})

const displayTitle = computed(() => {
  if (props.title) return props.title
  if (codeText.value === '403') return '抱歉，您没有权限访问该页面'
  return '抱歉，您访问的页面不存在'
})

const displaySubTitle = computed(() => {
  if (props.subTitle) return props.subTitle
  if (codeText.value === '403') return '请登录账号或联系管理员开通权限，或切换账号后重试。'
  return '链接可能已失效，或页面已被删除。'
})

function goHome() {
  if (auth.token && auth.roles.length > 0) {
    router.push('/admin')
    return
  }
  router.push('/portal/home')
}

function goBack() {
  router.back()
}

function switchAccount() {
  const redirect = route.query.from ? String(route.query.from) : '/admin'
  auth.logout()
  router.replace({ path: '/login', query: { redirect } })
}
</script>

<style scoped>
.error-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100%;
  padding: 32px 16px;
  flex: 1;
}

.error-card {
  width: 100%;
  max-width: 760px;
  border-radius: 24px;
  box-shadow: var(--ios-shadow);
  overflow: hidden;
  display: grid;
  grid-template-columns: 260px 1fr;
  gap: 0;
}

.error-visual {
  padding: 28px 24px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  background: linear-gradient(135deg, rgba(0, 122, 255, 0.18) 0%, rgba(0, 198, 251, 0.12) 100%);
  border-right: 1px solid var(--ios-border);
}

.error-visual.forbidden {
  background: linear-gradient(135deg, rgba(255, 59, 48, 0.16) 0%, rgba(255, 149, 0, 0.10) 100%);
}

.error-code {
  font-size: 68px;
  font-weight: 900;
  line-height: 1;
  letter-spacing: -2px;
  background: linear-gradient(135deg, var(--ios-primary) 0%, #00c6fb 100%);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.error-visual.forbidden .error-code {
  background: linear-gradient(135deg, var(--ios-danger) 0%, var(--ios-warning) 100%);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.error-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  width: fit-content;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.7);
  border: 1px solid rgba(255, 255, 255, 0.55);
}

.error-badge-text {
  font-weight: 600;
  color: #1d1d1f;
}

.error-content {
  padding: 28px 28px 24px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 10px;
}

.error-title {
  font-size: 20px;
  font-weight: 800;
  letter-spacing: -0.2px;
}

.error-subtitle {
  color: #8e8e93;
  font-size: 14px;
}

.error-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 18px;
}

.btn-icon {
  margin-right: 6px;
}

@media (max-width: 720px) {
  .error-card {
    grid-template-columns: 1fr;
  }
  .error-visual {
    border-right: none;
    border-bottom: 1px solid var(--ios-border);
  }
  .error-code {
    font-size: 60px;
  }
}
</style>
