import {Alert} from 'antd';
import React, {useState} from 'react';
import {connect, Dispatch} from 'umi';
import {StateType} from '@/models/login';
import {LoginParamsType} from '@/services/login';
import {ConnectState} from '@/models/connect';
import LoginFrom from './components/Login';

import styles from './style.less';

const {Tab, UserName, Password, Mobile, Captcha, Submit} = LoginFrom;

interface LoginProps {
  dispatch: Dispatch;
  userLogin: StateType;
  submitting?: boolean;
}

const LoginMessage: React.FC<{
  content: string;
}> = ({content}) => (
  <Alert
    style={{
      marginBottom: 24,
    }}
    message={content}
    type="error"
    showIcon
  />
);

const Login: React.FC<LoginProps> = (props) => {
  const {userLogin = {}, submitting} = props;
  // const {status, type: loginType} = userLogin;
  // const [autoLogin, setAutoLogin] = useState(true);
  const [identityType, setType] = useState<string>('account');

  const handleSubmit = (values: LoginParamsType) => {
    const {dispatch} = props;
    dispatch({
      type: 'login/login',
      payload: {...values, identityType},
    });
  };
  const getSuccess = () => {
    const item = localStorage.getItem("success");
    return item == null ? "SUCCESS" : item;
  }
  return (
    <div className={styles.main}>
      <LoginFrom activeKey={identityType} onTabChange={setType} onSubmit={handleSubmit}>
        <Tab key="account" tab="账户密码登录">
          {getSuccess() !== 'SUCCESS' && !submitting && (
            <LoginMessage content="账户或密码错误"/>
          )}

          <UserName
            name="identify"
            placeholder="用户名"
            rules={[
              {
                required: true,
                message: '请输入用户名!',
              },
            ]}
          />
          <Password
            name="credential"
            placeholder="密码"
            rules={[
              {
                required: true,
                message: '请输入密码！',
              },
            ]}
          />
        </Tab>
        {/*
        <Tab key="mobile" tab="手机号登录">
          {status === 'error' && loginType === 'mobile' && !submitting && (
            <LoginMessage content="验证码错误"/>
          )}
          <Mobile
            name="mobile"
            placeholder="手机号"
            rules={[
              {
                required: true,
                message: '请输入手机号！',
              },
              {
                pattern: /^1\d{10}$/,
                message: '手机号格式错误！',
              },
            ]}
          />
          <Captcha
            name="captcha"
            placeholder="验证码"
            countDown={120}
            getCaptchaButtonText=""
            getCaptchaSecondText="秒"
            rules={[
              {
                required: true,
                message: '请输入验证码！',
              },
            ]}
          />
        </Tab>
        <div>
          <a
            style={{
              float: 'right',
            }}
          >
            忘记密码
          </a>
        </div>
         */}


        <Submit loading={submitting}>登录</Submit>
        {/*
        <div className={styles.other}>
          其他登录方式
          <AlipayCircleOutlined className={styles.icon}/>
          <TaobaoCircleOutlined className={styles.icon}/>
          <WeiboCircleOutlined className={styles.icon}/>
          <Link className={styles.register} to="/user/register">
            注册账户
          </Link>
        </div>
        */}

      </LoginFrom>
    </div>
  );
};

export default connect(({login, loading}: ConnectState) => ({
  userLogin: login,
  submitting: loading.effects['login/login'],
}))(Login);
