import type React from "react";
import { Card, Input, Space } from "antd";
import { useEffect, useState } from "react";

interface SettingProps {
  className?: string;
}

const Setting: React.FC<SettingProps> = ({ className }) => {
  const [email, setEmail] = useState<string>('');
  const [key, setKey] = useState<string>('');
  const [congShangId, setCongShangId] = useState<string>('');

  useEffect(() => {
    const savedEmail = localStorage.getItem('userEmail');
    const savedKey = localStorage.getItem('userKey');
    const savedCongShangId = localStorage.getItem('congShangId');
    if (savedEmail) {
      setEmail(savedEmail);
    }
    if (savedKey) {
      setKey(savedKey);
    }
    if (savedCongShangId) {
      setCongShangId(savedCongShangId);
    }
  }, []);

  const handleEmailChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const value = e.target.value;
    setEmail(value);
    localStorage.setItem('userEmail', value);
  };

  const handleKeyChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const value = e.target.value;
    setKey(value);
    localStorage.setItem('userKey', value);
  };

  const handleChangeCongShangId = (e: React.ChangeEvent<HTMLInputElement>) => {
    const value = e.target.value;
    setCongShangId(value);
    localStorage.setItem('congShangId', value);
  };

  return (
    <Card
      title="设置"
      className={`${className} size-full flex flex-col gap-y-2`}
    >
      <Space direction="vertical">
        <Input
          addonBefore="邮箱"
          value={email}
          onChange={handleEmailChange}
          placeholder="请输入您的Git邮箱"
        />
        <Input.Password 
          addonBefore="Key" 
          value={key}
          onChange={handleKeyChange}
          placeholder="请输入您的Key"
        />
        <Input
          addonBefore="丛商ID"
          value={congShangId}
          onChange={handleChangeCongShangId}
          placeholder="请输入您的丛商ID"
        />
      </Space>
    </Card>
  );
};

export default Setting;
