<template>
  <ScreenLayout :show-back-btn="true" back-text="返回菜单" back-target="/menu">
    <div class="page-call-staff">
      <div class="call-content">
        <!-- 状态图标 -->
        <div class="call-icon" :class="{ calling: calling, success: callSuccess }">
          <svg v-if="callSuccess" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <path d="M22 11.08V12a10 10 0 1 1-5.93-9.14" />
            <polyline points="22 4 12 14.01 9 11.01" />
          </svg>
          <svg v-else-if="calling" class="animate-pulse" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <path d="M18 8A6 6 0 0 0 6 8c0 7-3 9-3 9h18s-3-2-3-9" />
            <path d="M13.73 21a2 2 0 0 1-3.46 0" />
          </svg>
          <svg v-else viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <path d="M18 8A6 6 0 0 0 6 8c0 7-3 9-3 9h18s-3-2-3-9" />
            <path d="M13.73 21a2 2 0 0 1-3.46 0" />
          </svg>
        </div>

        <!-- 状态文字 -->
        <h1 class="call-title">{{ callTitle }}</h1>
        <p class="call-message">{{ callMessage }}</p>

        <!-- 操作按钮 -->
        <div v-if="!callSuccess" class="call-actions">
          <button class="btn-call" @click="handleCall" :disabled="calling">
            <span v-if="calling" class="loading-spinner"></span>
            <span v-else>呼叫工作人员</span>
          </button>
        </div>

        <div v-else class="call-actions">
          <button class="btn-return" @click="goBack">
            返回首页
          </button>
        </div>
      </div>
    </div>
  </ScreenLayout>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useScreenStore } from '@/stores/screen'
import * as api from '@/api/screen'
import ScreenLayout from '@/layouts/ScreenLayout.vue'

const router = useRouter()
const screenStore = useScreenStore()

const calling = ref(false)
const callSuccess = ref(false)

const callTitle = computed(() => {
  if (callSuccess.value) return '已通知'
  if (calling.value) return '正在呼叫...'
  return '需要帮助？'
})

const callMessage = computed(() => {
  if (callSuccess.value) return '工作人员即将到达，请稍候'
  if (calling.value) return '正在为您联系工作人员'
  return '点击按钮呼叫工作人员为您提供帮助'
})

const handleCall = async () => {
  if (calling.value) return
  calling.value = true

  try {
    await api.callStaff({})
    callSuccess.value = true
    screenStore.showAlert({
      type: 'success',
      message: '已通知工作人员，请稍候'
    })
  } catch {
    screenStore.showAlert({
      type: 'error',
      message: '呼叫失败，请重试'
    })
  } finally {
    calling.value = false
  }
}

const goBack = () => {
  router.push('/idle')
}
</script>

<style scoped>
.page-call-staff {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #e9f8ff 0%, #dff8f5 50%, #f7fbff 100%);
}

.call-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
  text-align: center;
  padding: 40px;
  animation: fadeInUp 0.6s ease-out;
}

.call-icon {
  width: 120px;
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-card);
  border-radius: 50%;
  color: var(--primary);
  box-shadow: var(--shadow-lg);
}

.call-icon svg {
  width: 60px;
  height: 60px;
}

.call-icon.calling {
  background: var(--primary-soft);
}

.call-icon.success {
  background: var(--success-soft);
  color: var(--success);
}

.call-title {
  font-size: 36px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0;
}

.call-message {
  font-size: 20px;
  color: var(--text-secondary);
  margin: 0;
  max-width: 400px;
}

.call-actions {
  margin-top: 16px;
}

.btn-call,
.btn-return {
  min-width: 280px;
  height: 64px;
  padding: 0 48px;
  font-size: 22px;
  font-weight: 500;
  border-radius: var(--radius-lg);
  cursor: pointer;
  transition: all var(--transition-fast);
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-call {
  background: var(--primary);
  color: white;
  border: none;
  box-shadow: var(--shadow-md);
}

.btn-call:hover:not(:disabled) {
  background: var(--primary-dark);
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
}

.btn-call:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.btn-return {
  background: var(--bg-card);
  color: var(--primary);
  border: 2px solid var(--primary);
}

.btn-return:hover {
  background: var(--primary);
  color: white;
}

.loading-spinner {
  width: 24px;
  height: 24px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.animate-pulse {
  animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}
</style>
