import React ,{ PureComponent } from 'react';
import { Col, Container, Row,Button,ButtonToolbar,Breadcrumb,
  BreadcrumbItem,
  Card,
  CardBody,
  CardHeader,
  CardTitle,FormGroup
  } from 'reactstrap';
import { Link } from "react-router-dom";
import Header from "../../components/Header";
import HeaderTitle from "../../components/HeaderTitle";
import * as myConstClass from '../../constant.js';
import $ from 'jquery';
import { ValidatorForm, TextValidator } from 'react-material-ui-form-validator';
import { toastr } from "react-redux-toastr";



class Add extends PureComponent {

    constructor(props) {//alert('in..');
      super(props);
      this.state = {
        
        user_id:'',
        posts: [],
        branch:[],
        codes:[],
        formData: {
          sapcode: '',
          firstname: '',
          middlename: '',
          lastname: '',
          email:'',
          
  
      },
      submitted: false,
      }
      //this.handleSubmit = this.handleSubmit.bind(this);
    }

    componentDidMount(){
   
        const answer_array = this.props.location.pathname.split('/');
        //alert(answer_array['3']);
        if(answer_array['3']){
          
          this.state.user_id = answer_array['3'];
          fetch(myConstClass.BASE_URL+'/user/getAdminInfo/'+answer_array['3'])
      .then(response => response.json())
      .then(response =>{this.setState({formData: response})}).catch(err => console.log(err))
          
        }else{
          this.state.user_id = '';
        }
    
        
      if(!localStorage.getItem('session_id')){
        console.log('History:',this.props);      
        this.props.history.push('/');
      }
    
      }
      handleChange = (event) => {//alert('2');
      const { formData } = this.state;
      formData[event.target.name] = event.target.value;
      this.setState({ formData });
  }


  
handleSubmit = () => {
 
    this.setState({ submitted: true }, () => {
      
        setTimeout(() => this.setState({ submitted: false }), 5000);
        fetch(myConstClass.BASE_URL+`user/add_admin/`+this.state.user_id, {
                method: 'POST',
                body: JSON.stringify($('#pform').serializeArray()),
              }).then(response =>  response.json()).then((response) => {
                if(response == true){
                 // toast('User Added Successfully..!', {containerId: 'B',type: toast.TYPE.SUCCESS})
                  this.props.history.push('/pages/hradmin/');
                }else{
                 // toast('User Insertion Failed..!', {containerId: 'B',type: toast.TYPE.ERROR})
                }
              })
              .catch(); 
    });
  }


}