import React, { useState, useEffect } from 'react';
import { Plus, BookOpen, ChevronRight } from 'lucide-react';
import { apiClient } from '../../services/apiClient';
import { templateService } from '../../services/templateService';
import { AssessmentTemplate } from '../../types';

interface QuestionBank {
  id: string;
  name: string;
  description?: string;
  status: string;
  templateId?: string;
  createdAt: string;
}

export default function QuestionBankView() {
  const [banks, setBanks] = useState<QuestionBank[]>([]);
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState('');
  const [showDrawer, setShowDrawer] = useState(false);
  const [formData, setFormData] = useState({ 
    name: '', 
    description: '',
    templateId: '' 
  });
  const [saving, setSaving] = useState(false);
  const [templates, setTemplates] = useState<AssessmentTemplate[]>([]);
  const [loadingTemplates, setLoadingTemplates] = useState(false);

  useEffect(() => {
    fetchData();
  }, []);

  const fetchData = async () => {
    try {
      setLoading(true);
      const res = await apiClient.request('/question-banks', {});
      if (!res.ok) throw new Error(res.status.toString());
      const data = await res.json();
      setBanks(Array.isArray(data) ? data : (data.data || []));
    } catch (err: any) {
      setError(err.message || '加载失败');
    } finally {
      setLoading(false);
    }
  };

  const openDrawer = () => {
    setFormData({ name: '', description: '', templateId: '' });
    setLoadingTemplates(true);
    templateService.getAll()
      .then(data => setTemplates(data))
      .catch(err => console.error('加载模板失败:', err))
      .finally(() => setLoadingTemplates(false));
    setShowDrawer(true);
  };

  const handleCreate = async (e: React.FormEvent) => {
    e.preventDefault();
    if (!formData.name.trim()) return;
    
    setSaving(true);
    try {
      const payload: any = {
        name: formData.name,
        description: formData.description,
      };
      if (formData.templateId) {
        payload.templateId = formData.templateId;
      }
      
      const res = await apiClient.request('/question-banks', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(payload),
      });
      if (!res.ok) throw new Error(res.status.toString());
      
      setShowDrawer(false);
      fetchData();
    } catch (err: any) {
      console.error('创建失败:', err);
      alert('创建失败: ' + (err.message || '未知错误'));
    } finally {
      setSaving(false);
    }
  };

  return (
    <div className="p-6 bg-white min-h-screen">
      <div className="flex items-center justify-between mb-6">
        <h1 className="text-2xl font-bold">题库管理</h1>
        <button 
          onClick={openDrawer}
          className="flex items-center gap-2 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700"
        >
          <Plus size={18} />
          <span>创建题库</span>
        </button>
      </div>

      {loading ? (
        <div className="text-center py-8 text-gray-500">加载中...</div>
      ) : error ? (
        <div className="text-center py-8 text-red-500">错误: {error}</div>
      ) : banks.length === 0 ? (
        <div className="text-center py-8 text-gray-500">
          <BookOpen size={48} className="mx-auto mb-4 text-gray-300" />
          <p>暂无题库，点击上方按钮创建</p>
        </div>
      ) : (
        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
          {banks.map((bank) => (
            <div key={bank.id} className="border rounded-lg p-4">
              <h3 className="font-semibold">{bank.name}</h3>
              <p className="text-sm text-gray-500">{bank.description}</p>
              <p className="text-xs text-gray-400 mt-2">状态: {bank.status}</p>
            </div>
          ))}
        </div>
      )}

      {/* Drawer */}
      <>
        {showDrawer && (
          <div
            className="fixed inset-0 bg-black/20 backdrop-blur-sm z-40 transition-opacity duration-300"
            onClick={() => setShowDrawer(false)}
          />
        )}
        <div
          className={`fixed right-0 top-0 h-full w-full max-w-md bg-white shadow-2xl z-50 transform transition-transform duration-300 ease-out ${showDrawer ? 'translate-x-0' : 'translate-x-full'}`}
        >
          <div className="flex flex-col h-full">
            <div className="flex items-center justify-between px-6 py-4 border-b bg-slate-50">
              <h2 className="text-xl font-semibold text-slate-800 flex items-center gap-2">
                <Plus className="w-6 h-6 text-blue-600" />
                创建题库
              </h2>
              <button
                onClick={() => setShowDrawer(false)}
                className="p-2 text-slate-400 hover:text-slate-600 hover:bg-slate-200 rounded-full transition-colors"
              >
                <ChevronRight size={24} />
              </button>
            </div>
            <div className="flex-1 overflow-y-auto p-6">
              <form id="create-form" onSubmit={handleCreate} className="space-y-6">
                <div>
                  <label className="block text-sm font-medium text-slate-700 mb-1">
                    名称 <span className="text-red-500">*</span>
                  </label>
                  <input
                    type="text"
                    value={formData.name}
                    onChange={(e) => setFormData({...formData, name: e.target.value})}
                    className="w-full px-4 py-3 border rounded-xl focus:outline-none focus:ring-2 focus:ring-blue-500 bg-slate-50"
                    placeholder="输入题库名称"
                    required
                    autoFocus
                  />
                </div>
                <div>
                  <label className="block text-sm font-medium text-slate-700 mb-1">
                    描述
                  </label>
                  <input
                    type="text"
                    value={formData.description}
                    onChange={(e) => setFormData({...formData, description: e.target.value})}
                    className="w-full px-4 py-3 border rounded-xl focus:outline-none focus:ring-2 focus:ring-blue-500 bg-slate-50"
                    placeholder="输入描述"
                  />
                </div>
                <div>
                  <label className="block text-sm font-medium text-slate-700 mb-1">
                    关联模板
                  </label>
                  <select
                    value={formData.templateId}
                    onChange={(e) => setFormData({...formData, templateId: e.target.value})}
                    className="w-full px-4 py-3 border rounded-xl focus:outline-none focus:ring-2 focus:ring-blue-500 bg-slate-50"
                    disabled={loadingTemplates}
                  >
                    <option value="">不选择模板</option>
                    {templates.map(t => (
                      <option key={t.id} value={t.id}>{t.name}</option>
                    ))}
                  </select>
                  {loadingTemplates && <span className="text-xs text-slate-500">加载中...</span>}
                </div>
              </form>
            </div>
            <div className="p-6 border-t bg-slate-50">
              <button
                type="submit"
                form="create-form"
                disabled={saving || !formData.name.trim()}
                className="w-full flex items-center justify-center gap-2 px-6 py-3 bg-blue-600 text-white font-medium rounded-xl hover:bg-blue-700 active:scale-[0.98] transition-all disabled:opacity-50 disabled:cursor-not-allowed shadow-lg shadow-blue-600/20"
              >
                <Plus size={20} />
                {saving ? '创建中...' : '创建'}
              </button>
            </div>
          </div>
        </div>
      </>
    </div>
  );
}