import { LockOutlined, UserOutlined } from "@ant-design/icons";
import { Alert, Button, Form, Input, Typography } from "antd";
import React from "react";
import { connect } from "react-redux";
import { Link } from "react-router-dom";
import { createStructuredSelector } from "reselect";
import ImEXOnlineLogo from "../../../assets/logo192.png";
import { emailSignInStart } from "../../../redux/user/user.actions";
import {
  selectLoginLoading,
  selectSignInError,
} from "../../../redux/user/user.selectors";
import "./sign-in.page.styles.scss";

const mapStateToProps = createStructuredSelector({
  signInError: selectSignInError,
  loginLoading: selectLoginLoading,
});

const mapDispatchToProps = (dispatch) => ({
  emailSignInStart: (email, password) =>
    dispatch(emailSignInStart({ email, password })),
});

export function SignInPage({ emailSignInStart, signInError, loginLoading }) {
  const handleFinish = (values) => {
    const { email, password } = values;
    emailSignInStart(email, password);
  };
  const [form] = Form.useForm();

  return (
    <div className="login-container">
      <div className="login-logo-container">
        <img src={ImEXOnlineLogo} height="100" width="100" alt="ImEX Online" />
        <Typography.Title>ImEX RPS</Typography.Title>
      </div>
      <Form onFinish={handleFinish} form={form} size="large">
        <Form.Item name="email" rules={[{ required: true }]}>
          <Input prefix={<UserOutlined />} placeholder="Email" />
        </Form.Item>
        <Form.Item name="password" rules={[{ required: true }]}>
          <Input
            prefix={<LockOutlined />}
            type="password"
            placeholder="Password"
          />
        </Form.Item>
        {signInError ? (
          <Alert type="error" message={signInError.message} />
        ) : null}
        <Button
          className="login-btn"
          type="primary"
          htmlType="submit"
          loading={loginLoading}
        >
          Login
        </Button>
      </Form>
      <Link to={"/resetpassword"}>
        <Button>Reset Password</Button>
      </Link>
    </div>
  );
}

export default connect(mapStateToProps, mapDispatchToProps)(SignInPage);
