 // LIBRARIES
import React, { Component } from 'react';
import { Menu, Dropdown, Row, Col, Input, Button, Modal } from "antd"
import { Formik } from 'formik'
import { connect } from "react-redux"
import { customAction } from "actions";
import { message,notification } from 'antd';
import { apiFormValidation } from "utils/helper";
import { branchSchema } from '../../components/branchSchema'
import AddUserManagementForm from '../../components/branchesForm'
import {API_POST_BRANCH, API_UNI_OIL} from 'utils/StationApi'

  class CreateBranch extends Component {
    state= {
      updating: false,
      isGenerated: false,
      loading: false,
    }

    // handleSubmit = async (values, actions) => {
    //   const { setErrors } = actions;
    //   let { history } = this.props;
    //   let params  = { ...values }
    //   this.setState({ loading: true });

    //   try {
    //     const response = await API_UNI_OIL.post('branches/add',params);
    //     if(response) {
    //       message.success('New branch added.');
    //       this.setState({ loading: false });
    //       history.push({ pathname: '/branches' });
    //     }
    //    console.log(response)
    //   } catch (error) {
    //     // if (error.status === 422) {
    //     //   apiFormValidation({ data: error.data.data, setErrors });
    //     // }
    //     console.log(error)
    //     notification.error({
    //       message: 'Error',
    //       description: <div>
    //         Something went wrong creating new record..
    //         { error && error.data && error.data.data && error.data.data.fee_code
    //             && (<div>- {error.data.data.fee_code[0]} </div>) }
    //       </div>
    //     });
    //     this.setState({ loading: false });
    //   }
    // }

    handleSubmit = async (values, actions) => {

      const { setErrors, setSubmitting } = actions;
      let { history } = this.props;
      let _self = this;
      this.setState({loading: true})

      this.props.customAction({
        type: "BRANCHES_CREATE_REQUEST",
        payload: {
          values,
          setSubmitting,
          setErrors,
          history,
          _self
        }
      });
    }

    handleAddUser =()=> {
      this.form.submitForm()
    }

    render() {
      const { match, history } = this.props;
      let user = localStorage.getItem('user')
      const { branchesCreate } = this.props
      console.log(branchesCreate)
      const { loading, isGenerated } = this.state;
      return (
        <div>
          <Formik
            initialValues={{
              code:'',
              name: '',
              details: '',
              created_by: user
            }}
            ref={node => (this.form = node)}
            enableReinitialize={true}
            validationSchema={branchSchema}
            onSubmit={this.handleSubmit }
            render = {(props)=>
              <AddUserManagementForm
                {...props}
                title='Add Branch'
                history={this.props.history}
                loading={branchesCreate.createRequestPending || loading}
                // generatePassword={this.generatePassword}
                isGenerated={isGenerated}
                // copyActionHandler={this.copyActionHandler}
              />
            }
          />
        </div>
      );
    }
  }

CreateBranch = connect(
    state => ({
      //userInfo: state
      branchesCreate: state.branchesCreate,
    }),
    { customAction }
  )(CreateBranch);

  export default CreateBranch;