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 product_orientation extends Component {
  constructor(props) {
    super(props);
    if (
      localStorage.getItem("current_state") !== "product_orientation")  {
        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,
      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");
        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_t10 text-center">
            <img
              src={api.image_path + "familyone_logo.png"}
              alt=""
              title=""
              className="product_orientation_images"
            />
          </div>
        )}
        <div className="inner_container2 m_t10 po_innercontainer">
          <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 po_white_container_div wc_fixheightdiv">
              <div className="product_orientation_div">
                <p>Exclusive</p>
                <p className="product_orientation_div_para">
                  PRODUCTS<span className="product_orientation_div_span">
                    !
                  </span>
                </p>
              </div>
              <div className="welcome_txt ">
                <div className="text-center product_orientation_img_div">
                  <img
                    src={api.image_path + "product_image.png"}
                    alt=""
                    title=""
                    className="product_orientation_img"
                  />
                </div>
              </div>
              <div className="product_orientation_footer_div allo_pagewhitediv">
                <p className="page-txt po_para">
                  These products are exclusive from family.one designed by our
                  Canadian team based on experience with our members .
                </p>
                <p className="page-txt product_orientation_footer_para po_para1">
                  Please note that if any of these products are made available
                  as prizes for contests and you are selected as a winner, we'll
                  refund the cost of the product in your order.
                </p>
              </div>
              <div className="m_t10">
                <ul className="dot_ul m-auto">
                  <li className="active">
                    <a onClick={() => {}} />
                  </li>
                  <li>
                    <a
                      onClick={() => {
                        localStorage.setItem(
                          "current_state",
                          "product_orientation1"
                        ),
                          this.props.history.push("/product_orientation1");
                      }}
                    />
                  </li>
                  <li>
                    <a
                      onClick={() => {
                        localStorage.setItem(
                          "current_state",
                          "product_orientation1"
                        ),
                          this.props.history.push("/product_orientation1");
                      }}
                    />
                  </li>
                </ul>
              </div>
              {
                localStorage.getItem("contest_id") == null ||
                localStorage.getItem("contest_id") == "null" ||
                localStorage.getItem("contest_id") == "" ||
                localStorage.getItem("contest_id") == "undefined" ||
                (typeof localStorage.getItem("contest_id") == undefined && (
                  <div className="text-right prev-arrow">
                    <a
                      onClick={() => {
                        localStorage.setItem("current_state", "contest"),
                          this.props.history.push(
                            "/Contest/" +
                              localStorage.getItem("contest_id") +
                              "/" +
                              localStorage.getItem("user_id")
                          );
                      }}
                    >
                      <img
                        src={api.image_path + "prev_arrow.jpg"}
                        alt=""
                        title=""
                        width="30"
                      />
                    </a>
                  </div>
                ))}
              <div className="text-right next-arrow">
                <a
                  onClick={() => {
                    localStorage.setItem(
                      "current_state",
                      "product_orientation1"
                    ),
                      this.props.history.push("/product_orientation1");
                  }}
                >
                  <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={() => {
                  console.log("local", localStorage.getItem("activity"));
                  if (
                    typeof cookie.load("access_token") !== undefined &&
                    cookie.load("access_token") !== "undefined" &&
                    cookie.load("access_token") !== "null" &&
                    cookie.load("access_token") !== null &&
                    cookie.load("access_token")
                  ) {
                    localStorage.getItem("perks") == 0
                      ? localStorage.setItem("current_state", "perks")
                      : localStorage.setItem("current_state", "menu_page");
                    this.props.history.push({
                      pathname:
                        localStorage.getItem("perks") == 0
                          ? "/perks"
                          : "/menu_page"
                    });
                  } else {
                    if (localStorage.getItem("activity") === 1) {
                      console.log("activity done");
                      localStorage.setItem("current_state", "remind_user"),
                        this.props.history.push("/remind_user");
                    } else {
                      console.log("activity not done");
                      localStorage.setItem("current_state", "remind_user1"),
                        this.props.history.push("/remind_user1");
                    }
                  }
                }}
              >
                Skip
              </a>
            </div>
          )}
        </div>
      </div>
    );
  }
}
export default product_orientation;
