PositionAbilityArea.tsx 6.31 KB
import { useState } from 'react';
import type { PositionTab, Agent, MediaType } from '../types/home';
import { QrcodeModal } from './QrcodeModal';

export interface PositionAbilityAreaProps {
  tab: PositionTab;
  currentAgent: Agent | undefined;
  currentAgentIndex: number;
  onAgentSelect: (index: number) => void;
  onOpenQrcodeModal: () => void;
  qrcodeModalOpen: boolean;
  onCloseQrcodeModal: () => void;
}

export function PositionAbilityArea({
  tab,
  currentAgent,
  currentAgentIndex,
  onAgentSelect,
  onOpenQrcodeModal,
  qrcodeModalOpen,
  onCloseQrcodeModal,
}: PositionAbilityAreaProps) {
  const [mediaError, setMediaError] = useState(false);

  const handleMediaError = () => {
    setMediaError(true);
  };

  const renderMedia = (mediaUrl: string | null, mediaType: MediaType | null) => {
    if (!mediaUrl || !mediaType || mediaError) {
      return (
        <div className="w-full h-full bg-surface-container flex items-center justify-center">
          <span className="text-on-surface-variant">素材不可用</span>
        </div>
      );
    }

    if (mediaType === 'mp4') {
      return (
        <video
          src={mediaUrl}
          data-testid="agent-video"
          className="w-full h-full object-cover"
          autoPlay
          loop
          muted
          playsInline
          onError={handleMediaError}
        />
      );
    }

    return (
      <img
        src={mediaUrl}
        data-testid="agent-image"
        alt="Agent 素材"
        className="w-full h-full object-contain group-hover:scale-110 transition-transform duration-500"
        onError={handleMediaError}
      />
    );
  };

  return (
    <section className="py-20 bg-surface" data-testid="position-ability-area">
      <div className="max-w-container-max mx-auto px-margin-mobile tablet:px-margin-desktop">
        <div className="grid grid-cols-1 tablet:grid-cols-2 gap-16 items-center">
          {/* Left: Mockup UI */}
          <div className="rounded-[32px] p-8 tablet:p-12 border border-outline-variant/30 bg-white/80 backdrop-blur-xl flex flex-col items-center text-center relative shadow-sm">
            <div className="absolute top-6 left-8 flex gap-2">
              <div className="w-3 h-3 rounded-full bg-red-400/50" />
              <div className="w-3 h-3 rounded-full bg-yellow-400/50" />
              <div className="w-3 h-3 rounded-full bg-green-400/50" />
            </div>
            <div className="absolute top-6 right-8">
              <div className="px-3 py-1 bg-primary text-[10px] text-white font-bold rounded-md">GL-DIGITECH</div>
            </div>

            <div className="mb-10 mt-8 w-48 h-48 bg-primary/5 rounded-full flex items-center justify-center p-8 group overflow-hidden" data-testid="agent-media-container">
              {currentAgent ? (
                renderMedia(currentAgent.media_url, currentAgent.media_type)
              ) : (
                <div className="w-full h-full bg-surface-container flex items-center justify-center">
                  <span className="text-on-surface-variant">暂无 Agent 素材</span>
                </div>
              )}
            </div>

            <h3 className="text-headline-sm mb-2">
              {currentAgent?.speech_title ?? '岗位智能体'}
              <span className="text-on-surface-variant text-label-md font-normal ml-2">/ Position Agent</span>
            </h3>
            <p className="text-body-md text-on-surface-variant max-w-sm mb-8">
              {currentAgent?.speech_content ?? '为每个岗位配置专属 AI 助手,辅助决策,提升效率'}
            </p>

            <button
              onClick={onOpenQrcodeModal}
              className="flex items-center gap-2 px-8 py-3 bg-primary-container text-white rounded-xl text-label-md transition-transform active:scale-95"
            >
              立即体验 <span className="text-[18px]">↗</span>
            </button>
          </div>

          {/* Right: Content */}
          <div className="space-y-8">
            <div className="inline-block border-l-4 border-primary pl-4 py-1">
              <p className="text-label-sm text-primary uppercase tracking-widest mb-1">核心能力</p>
              <h2 className="text-headline-md">
                {tab.main_title.split(' ').map((word, index) => (
                  index === 1 ? <span key={index} className="text-primary">{word}</span> : word + ' '
                ))}
              </h2>
            </div>

            <p className="text-body-md text-on-surface-variant leading-relaxed">
              {tab.sub_title}
            </p>

            {tab.agents.length > 0 && (
              <div className="space-y-4">
                {tab.agents.map((agent, index) => (
                  <div
                    key={agent.agent_id}
                    onClick={() => onAgentSelect(index)}
                    className={`p-5 rounded-2xl border transition-all group cursor-default ${
                      index === currentAgentIndex
                        ? 'border-primary/40 bg-primary/5 shadow-md'
                        : 'border-outline-variant/30 bg-surface-container-lowest hover:border-primary/40 hover:shadow-md'
                    }`}
                  >
                    <div className="flex items-start gap-4">
                      <div className={`w-10 h-10 rounded-xl flex items-center justify-center flex-shrink-0 transition-colors ${
                        index === currentAgentIndex
                          ? 'bg-primary text-white'
                          : 'bg-primary/10 text-primary group-hover:bg-primary group-hover:text-white'
                      }`}>
                        <span className="text-[20px]">★</span>
                      </div>
                      <div>
                        <p className="text-label-md font-bold mb-1">{agent.speech_title ?? agent.agent_name ?? 'Agent'}</p>
                        <p className="text-body-md text-on-surface-variant text-sm">{agent.speech_content ?? ''}</p>
                      </div>
                    </div>
                  </div>
                ))}
              </div>
            )}
          </div>
        </div>
      </div>

      <QrcodeModal
        isOpen={qrcodeModalOpen}
        onClose={onCloseQrcodeModal}
        positionName={tab.position_name}
        qrcodeUrl={tab.qrcode_url}
        qrcodeStatus={tab.qrcode_status}
      />
    </section>
  );
}