import React, { Component } from "react";
import Drawer from "./drawer";
import cookie from 'react-cookies'
import axios from "axios";
import CircularProgress from "@material-ui/core/CircularProgress";
import FlatButton from "material-ui/FlatButton";
import Dialog from "material-ui/Dialog";
import api from "./api/api";
class contest4 extends Component {
  constructor(props) {
    super(props);
    if (
      localStorage.getItem("current_state") !== "contest4" ) {
        this.props.history.push("/menu_page");
      }
    console.log("session", localStorage.getItem("current_state"));
    console.log("is_active", localStorage.getItem("login_is_active"));
    this.state = {
      loader: false,
      is_active: localStorage.getItem("login_is_active") == 1 ? true : false,
      open: false,
      alertmsg: "",
      product_orientation: false,
      contest_index: localStorage.getItem("contest_index")
    };
    this.handleClick = this.handleClick.bind(this);
    this.handleClose.bind(this);
  }
  handleClose = () => {
    this.setState({ open: false });
  };
  handleClick = () => {
    var self = this;
    self.setState({ loader: true });

    let get_contest = {
      action: "update",
      user_id: cookie.load("user_id"),
      user_metadata: {
        contest_orientation: 1
      }
    };

    console.log("get_contest", get_contest);
    axios
      .request({
        url: api.react_request,
        method: "POST",
        data: JSON.stringify(get_contest)
      })
      .then(function(response) {
        self.setState({ loader: false });
        console.log("response", response);
        console.log(response.data);
        localStorage.setItem(
          "contest_orientation",
          response.data.user_metadata.contest_orientation
        );
        console.log(JSON.parse(localStorage.getItem("contest_array")));
        let contest_array = JSON.parse(localStorage.getItem("contest_array"));
        // console.log("index", index);
        // console.log("contest_array[index]._id",contest_array[index]._id)
        self.setState({ loader: false });
        localStorage.setItem("current_state", "contest");
        self.props.history.push(
          "/Contest/" +
            contest_array[localStorage.getItem("contest_index")]._id +
            "/" +
            cookie.load("user_id")
        );
      })
      .catch(error => {
        console.log("error", error);
        self.setState({ loader: false });
        self.setState({
          open: true,
          alertmsg: "Something Wrong... Please try again later"
        });
      });
  };

  render() {
    return (
      <div className="page-bg">
        <center>{this.state.is_active && <Drawer prop={this.props} />}</center>
        {!this.state.loader && (
        <div className="welcome_img freebie1-p text-center">
          <img
            src={api.image_path+"familyoneContest.png"}
            alt=""
            title=""
            className="max-w-150"
          />
        </div>
        )}
        <div className="inner_container2 m_t20">
          <div className="container-fluid">
            {this.state.loader && (
              <div>
              <br/><br/><br/><br/><br/><br/>
              <CircularProgress
                size={100}
                style={{ color: "black", marginTop: "17%" }}
              />
             </div>
            )}
          </div>
          {!this.state.loader && (
            <div className="white_container newwhite_container allo_pagewhitediv">
              <div className="text-right next-arrow">
                <a onClick={() => this.handleClick()}>
                  <img
                    src={api.image_path+"next_arrow.jpg"}
                    alt=""
                    title=""
                    width="30"
                  />
                </a>
              </div>
              <div className="text-right prev-arrow">
                <a
                  onClick={() => {
                    localStorage.setItem("current_state", "contest3"),
                      this.props.history.push("/contest3");
                  }}
                >
                  <img
                    src={api.image_path+"prev_arrow.jpg"}
                    alt=""
                    title=""
                    width="30"
                  />
                </a>
              </div>
              <div className="welcome_img text-center contast3_img">
                <img
                  src={api.image_path+"contest-orientation-4.jpg"}
                  alt=""
                  title=""
                  className="freebie2_img contest4_img"
                />
              </div>
              <div className="contest_welcome_txt2 m_t10 text-center">
                <b>Lend a hand</b>
                <br />
                What if you don't <br />qualify for a contest<br /> but a
                relative does?
              </div>
              <p className="page-txt m_t10">
                You can still <b>social share </b>these contests. <br />
                <p>
                  With <span className="contest4_spantag">every resulting entry</span> we'll credit <b>+1 entry point </b>
                  <span className="contest4_spantag"> each relative</span> in your family-share network
                who does enter.</p>
              </p>
              <div className="m_t10">
                <ul className="dot_ul m-auto">
                  <li>
                    <a
                      onClick={() => {
                        localStorage.setItem("current_state", "contest3"),
                          this.props.history.push("/contest3");
                      }}
                    />
                  </li>
                  <li>
                    <a
                      onClick={() => {
                        localStorage.setItem("current_state", "contest3"),
                          this.props.history.push("/contest3");
                      }}
                    />
                  </li>
                  <li>
                    <a
                      onClick={() => {
                        localStorage.setItem("current_state", "contest3"),
                          this.props.history.push("/contest3");
                      }}
                    />
                  </li>
                  <li className="active">
                    <a onClick={() => this.handleClick()} />
                  </li>
                </ul>
              </div>
            </div>
          )}
          {!this.state.loader && (
            <div className="freebie_footer_div text-center">
              <a onClick={() => this.handleClick()} className="skip_link">
                Skip
              </a>
            </div>
          )}
        </div>
      </div>
    );
  }
}
export default contest4;
