import React from "react";
import axios from 'axios';
import api from "./api/api"
import cookie from 'react-cookies'
import $ from "jquery"
class UserExist extends React.Component {
  
  constructor(props) {
    console.log("finalJson");
    super(props);
    if (
      localStorage.getItem("current_state") !== "exist_user") {
        this.props.history.push("/menu_page");
      }
  }

  componentDidMount=()=>{
    console.log("finalJson");
    let self = this;
    // Testing purpose QojjBgcvYKuRciNLarQZEMXwI4j2Jk24
    // Live purpose SHcHyNsbh1nUG8ffWEpVRlSa0nT686EZ
    let finalJson = {
      action: "login",
      client_id:  api.login_key,
      connection: "email",
      email: localStorage.getItem("user_email"),
      send: "link",
      authParams: {
        scope: "openid"
      }
    };
    console.log("finalJson", finalJson);
    self.setState({ loader: true });
      axios
        .request({
          method: "post",
          url: api.react_request,
          data: JSON.stringify(finalJson)
        })
        .then(function(login_response) {
          console.log("login_response", login_response);
          if (login_response.status === 200) {
            if (
              login_response.data.error_description ===
              "Public signup is disabled"
            ) {
              localStorage.setItem("login_failed_email",localStorage.getItem("user_email") );
            self.setState({ loader: false });
            localStorage.setItem("current_state", "login_failed"),
            self.props.history.push("/login_failed")
            } else {
              self.setState({ loader: false });
            }
          } else {
            localStorage.setItem("login_failed_email",localStorage.getItem("user_email") );
            self.setState({ loader: false });
            localStorage.setItem("current_state", "login_failed"),
            self.props.history.push("/login_failed")
          }
        });
  }

  render() {
    return (
    <div className="bcd_header_div_margin"> 
     <div className="allpages_desktop_boxes_div existinguser_header_div">
      <div className="e_user_div_header">
        <div className="e_user_first_div">
          <img
            src={api.image_path+"email_box.png"}
            alt=""
            title=""
            className="e_user_img"
          />
          <p className="e_user_pare">Check Your</p>
          <p className="e_user_pare email_text">
            <span className="email_text_e">E</span><span className="email_text_m">m</span><span className="email_text_a">a</span>
            <span className="email_text_i">i</span><span className="email_text_l">l</span><span className="email_text_hash">!</span>
          </p>
          <p className="e_user_pare_one">
            It appears you are already registered. We've sent a{" "}
            <span>login link</span> to your email address to access your account
            and continue.
          </p>
        </div>
        <div className="e_user_second_div">
          <img
            src={api.image_path+"prev_arrow.jpg"}
            width="40"
            className="next_icon next_left"
            onClick={() =>
              {localStorage.setItem("current_state","home"),this.props.history.push("/")}
            }
          />
        </div>
        <div />
      </div>
     </div>
    </div>  
    );
  }
}
export default UserExist;
