import React, { Component } from "react";
import Drawer from "./drawer";

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";
import cookie from 'react-cookies'
class newMemberOrientation3 extends Component {
  constructor(props) {
    super(props);
    // if (
    //   localStorage.getItem("current_state") !== "freebie1" ||
    //   localStorage.getItem("user_id") == "" ||
    //   localStorage.getItem("user_id") == "undefined" ||
    //   typeof localStorage.getItem("user_id") == undefined
    // ) {
    //   this.props.history.push("/");
    // }
    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,
      past_member:localStorage.getItem("past_member")=="yes"?true:false,
    };
    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: {
        offer_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(
          "offer_orientation",
          response.data.user_metadata.offer_orientation
        );

        console.log(JSON.parse(localStorage.getItem("contest_array")));
        let contest_array = JSON.parse(localStorage.getItem("contest_array"));
        // localStorage.setItem(
        //   "offer_orientation_index",
        //   index
        // );
        localStorage.setItem("current_state", "contest")
        if (
          typeof localStorage.getItem("need_to_claim") !==
            undefined &&
          localStorage.getItem("need_to_claim") !==
            "undefined" &&
          localStorage.getItem("need_to_claim") !== "null" &&
          localStorage.getItem("need_to_claim") !== null
        ) {
          console.log(
            JSON.parse(localStorage.getItem("need_to_claim"))
          );

          localStorage.setItem(
            "need_to_claim_detail",
            JSON.stringify(
              JSON.parse(
                localStorage.getItem("need_to_claim")
              )[0]
            )
          );
          this.props.history.push("/won");
        } else {
          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"
        });
      });
  };

  render() {
    return (
      <div className="page-bg">
        <center>
          {this.state.loader && (
            <div>
              <CircularProgress
                size={100}
                style={{ color: "black", marginTop: "17%" }}
              />
            </div>
          )}
          {this.state.is_active && <Drawer prop={this.props} />}
        </center>
        {/* {this.state.is_active && <Drawer prop={this.props} />} */}
        {!this.state.loader && (
          <div className="welcome_img m_t20 text-center">
            <img
              src={api.image_path+"family_perk.jpg"}
              alt=""
              title=""
              className="max-w-200"
            />
          </div>
        )}
        <div className="inner_container2 m_t20 ">
          <Dialog
          style={{textTransform:"capitalize"}}
            actions={
              <FlatButton
                label="Close"
                labelStyle={{ color: "#000" }}
                onClick={this.handleClose.bind(this)}
              />
            }
            modal={false}
            open={this.state.open}
            onRequestClose={this.handleClose}
          >
            {this.state.alertmsg}
          </Dialog>
          {!this.state.loader && (
            <div className="white_container allo_pagewhitediv">
            <div className="new_member_orientation_contest_div">
              <div className=" text-center m_t10">
                <img
                  src={api.image_path+"new_member_preaks.jpg"}
                  alt=""
                  title=""
                  className="max-w-100 new_mamaber_contest_img"
                />
              </div>
             </div> 
              <div className="welcome_txt m_t10">
                <b>Perks</b>
              </div>
              <p className="page-txt">
               The first perks program exclusively for families.
              </p>
              <div className="m_t20">
                <ul className="dot_ul m-auto">
                  <li>
                    <a onClick={() => this.props.history.push("/new_member_orientation2")} />
                  </li>
                  <li>
                    <a onClick={() => this.props.history.push("/new_member_orientation2")} />
                  </li>
                  <li className="active">
                    <a onClick={() => this.props.history.push("/new_member_orientation3")} />
                  </li>
                </ul>
              </div>
              <div className="text-right prev-arrow">
                <a onClick={() => this.props.history.push("/new_member_orientation2")}>
                  <img
                    src={api.image_path+"prev_arrow.jpg"}
                    alt=""
                    title=""
                    width="30"
                  />
                </a>
              </div>
              <div className="text-right next-arrow">
                <a onClick={() => {this.state.past_member==true?this.props.history.push("/past_member_orientation1"): this.props.history.push("/new_member_orientation4")}}>
                  <img
                    src={api.image_path+"next_arrow.jpg"}
                    alt=""
                    title=""
                    width="30"
                  />
                </a>
              </div>
            </div>
          )}
          {!this.state.loader && (
            <div className="freebie_footer_div text-center">
              <a className="skip_link" onClick={() => {this.state.past_member==true?this.props.history.push("/past_member_orientation1"): this.props.history.push("/new_member_orientation4")}}>
                Skip
              </a>
            </div>
          )}
        </div>
      </div>
    );
  }
}
export default newMemberOrientation3;
