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 nocontest_screen_one extends Component {
  constructor(props) {
    super(props);
    if (
      localStorage.getItem("current_state") !== "no_contest")  {
        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")
        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]
            )
          );
          localStorage.setItem("current_state","won"),
          this.props.history.push("/won");
        } else {
          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+"familyoneContest.png"}
              alt=""
              title=""
              className="freebie3_img"
            />
          </div>
        )}
        <div className="inner_container2 m_t10 ">
          <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 nocontest_white_container">
            <div className="new_member_orientation_contest_div">
              <div className="text-center">
                <img
                  src={api.image_path+"new_member_conntest.jpg"}
                  alt=""
                  title=""
                  className="max-w-100 new_mamaber_contest_img"
                />
              </div>
             </div> 
             <div className="nocontest_freebietext_div">
              <div className="nocontest_txtdiv mt20">
                <b>Win contests curated for your family</b>
              </div>
              <p className="page-txt">
               Win contests curated for your family Every month we give away a minimum of $50,000 in prizes exclusively for Canadian families. 
              </p>
              <p className="page-txt nocontext_para2">
               Unfortunately there are no contests that match your family's needs right now. 
              </p>
              <p className="page-txt nocontext_para2">
               Try connecting with other family members by going to My Family in the side menu or check back later. We'll message you when there's a contest for you. 
              </p>
             </div> 
             <div
                  className="mainactive_later infobtn_div mt70 mb70 nocontest_btn"
                  style={{ cursor: "pointer" }}
                >
                <a
                    onClick={() => {localStorage.setItem("current_state","product_orientation"),this.props.history.push("/product_orientation")}}
                    className="active_later active_later_info"
                >
                    <span>
                      Next{" "}
                      <i>
                        <img
                          src={api.image_path+"next_arrowBkup.jpg"}
                          alt=""
                          title=""
                          width="30"
                          className="info_btnicon"
                        />
                      </i>
                    </span>
                </a>
              </div>
            </div>
          )}
        </div>
      </div>
    );
  }
}
export default nocontest_screen_one;
