import React, { useState } from 'react';
import { Button, Form, Spinner, Card } from 'react-bootstrap';
import { toast } from 'react-toastify';
import './style.css';

const AudioUpload = ({ onAudioSelected }) => {
  const [audioFile, setAudioFile] = useState(null);
  const [loading, setLoading] = useState(false);
  const [audioFileName, setAudioFileName] = useState('');

  // 处理音频文件选择
  const handleAudioFileChange = (e) => {
    const selectedFile = e.target.files[0];
    if (selectedFile) {
      setAudioFile(selectedFile);
      setAudioFileName(selectedFile.name);
      
      // 调用回调函数将音频文件传递给父组件
      if (typeof onAudioSelected === 'function') {
        onAudioSelected(selectedFile);
      }
    }
  };

  return (
    <div className="audio-upload-container">
      <Card>
        <Card.Header as="h5">上传音频（必选）</Card.Header>
        <Card.Body>
          <Form>
            <Form.Group controlId="audioFile" className="mb-3">
              <Form.Label>选择音频文件</Form.Label>
              <div className="custom-file-upload">
                <input
                  type="file"
                  accept="audio/*"
                  onChange={handleAudioFileChange}
                  id="audio-file-input"
                  className="d-none"
                  required
                />
                <div className="file-upload-box">
                  <Button 
                    variant="outline-primary" 
                    onClick={() => document.getElementById('audio-file-input').click()}
                  >
                    选择音频
                  </Button>
                  <span className="ml-2">{audioFileName || '未选择音频文件'}</span>
                </div>
              </div>
              {!audioFile && (
                <Form.Text className="text-danger">
                  请上传音频文件，这是必选项
                </Form.Text>
              )}
            </Form.Group>
          </Form>
        </Card.Body>
      </Card>
    </div>
  );
};

export default AudioUpload; 