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 freebie3 extends Component {
  constructor(props) {
    super(props);
    if (
      localStorage.getItem("current_state") !== "freebie3" )  {
        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
    };
    this.handleClick = this.handleClick.bind(this);
    this.handleClose.bind(this);
  }
  handleClose = () => {
    this.setState({ open: false });
  };
  handleClick = () => {
    var self = this;
    self.setState({ loader: true });

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

    console.log("update", update);
    axios
      .request({
        url: api.react_request,
        method: "POST",
        data: JSON.stringify(update)
      })
      .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("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.is_active && <Drawer prop={this.props} />}</center>
        {!this.state.loader && (
          <div className="welcome_img freebie1-p text-center">
            <img
              src={api.image_path+"family_freebibes.jpg"}
              alt="fgjgf"
              title=""
              className="max-w-150"
            />
          </div>
        )}
        <div className="inner_container2 m_t20 ">
          <div className="container-fluid">
            {this.state.loader && (
              <div style={{margintop:20}}>
              <br/><br/><br/><br/><br/><br/>
              <CircularProgress
                size={100}
                style={{ color: "black", marginTop: "17%" }}
              />
             </div> 
            )}
          </div>
          <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 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", "freebie2"),this.props.history.push("/freebie2"))}>
                  <img
                    src={api.image_path+"prev_arrow.jpg"}
                    alt=""
                    title=""
                    width="30"
                  />
                </a>
              </div>
              <div className="welcome_img text-center">
                <img src={api.image_path+"gift3.jpg"} alt="" title="" className="freebie2_img" />
              </div>
              <div className="welcome_txt2 text-center mt20">
                Share and<br />
                Gain <b>EXTRA</b>
              </div>
              <p className="subtitle m_t10">
              Some brands turn to us with exclusive offers looking to connect with our most engaging members, so while sharing is nice, it can pay off too! 
              </p>
              <p className="m_t10 page-txt">
              The share buttons for each freebie program will be presented after redeeming.
              </p>
              <div className="pt80">
                <ul className="dot_ul m-auto">
                  <li>
                    <a onClick={() => (localStorage.setItem("current_state", "freebie2"),this.props.history.push("/freebie2"))} />
                  </li>
                  <li>
                    <a onClick={() => (localStorage.setItem("current_state", "freebie2"),this.props.history.push("/freebie2"))} />
                  </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 freebie3;
