import React from 'react';
import { Card, Form, Input, Button, Alert, message } from 'antd';
import { emailTest, sendEmail as sendEmailApi } from '@/services/kratos/email';
import './index.less';

/**
 * 邮件发送页面
 */
const EmailPage: React.FC = () => {
  const [form] = Form.useForm();

  // 发送测试邮件
  const handleSendTestEmail = async () => {
    try {
      const res = await emailTest();
      if (res.code === 0) {
        message.success('发送成功');
      }
    } catch (error) {
      message.error('发送失败');
    }
  };

  // 发送邮件
  const handleSendEmail = async () => {
    try {
      const values = await form.validateFields();
      const res = await sendEmailApi(values);
      if (res.code === 0) {
        message.success('发送成功,请查收');
      }
    } catch (error) {
      message.error('发送失败');
    }
  };

  return (
    <div className="email-container">
      <Alert
        message="需要提前配置email配置文件，为防止不必要的垃圾邮件，在线体验功能不开放此功能体验。"
        type="warning"
        showIcon
        className="email-alert"
      />

      <Card className="email-form-card">
        <Form form={form} layout="horizontal" labelCol={{ span: 4 }} wrapperCol={{ span: 20 }}>
          <Form.Item name="to" label="目标邮箱" rules={[{ required: true, message: '请输入目标邮箱' }, { type: 'email', message: '请输入有效的邮箱地址' }]}>
            <Input placeholder="请输入目标邮箱" />
          </Form.Item>
          <Form.Item name="subject" label="邮件标题" rules={[{ required: true, message: '请输入邮件标题' }]}>
            <Input placeholder="请输入邮件标题" />
          </Form.Item>
          <Form.Item name="body" label="邮件内容" rules={[{ required: true, message: '请输入邮件内容' }]}>
            <Input.TextArea rows={6} placeholder="请输入邮件内容" />
          </Form.Item>
          <Form.Item wrapperCol={{ offset: 4, span: 20 }}>
            <div className="form-actions">
              <Button onClick={handleSendTestEmail}>发送测试邮件</Button>
              <Button type="primary" onClick={handleSendEmail}>发送邮件</Button>
            </div>
          </Form.Item>
        </Form>
      </Card>
    </div>
  );
};

export default EmailPage;
