// LIBRARIES
import React, { Component } from 'react'
import { connect } from "react-redux"
import { Formik } from 'formik'
import { message,notification } from 'antd';

// COMPONENTS
import HeaderForm from "components/Forms/HeaderForm"
import AddNotificationForm from './components/AddNotificationForm'

// HELPER FUNCTIONS
import { userDetailsSchema } from './validationSchema'
import { customAction } from "actions";
import { API_UNI_OIL } from 'utils/NotificationApi';


class CreateNotification extends Component {
  state = {
    userInfo: null,
    loading: false,
    schedule: 'false',
  }
 

  handleSubmit = async (values, actions) => {
    
    const { setErrors, setSubmitting } = actions;
    let { history } = this.props;
    let _self = this; 
    let params  = { ...values }

    console.log(params);

    // const response = await API_UNI_OIL.post('notification',params);
    //  if(response) {
    //   console.log(response);
    // }
    // this.setState({loading: true})
    // values.role = parseInt(values.role);
    // if(values.status) {
    //   values.status = values.status
    // } else { 
    //   values.status = "active"
    // }
    // if(values.username) {
    //   values.username = values.username.toLowerCase()
    // }
    
    this.props.customAction({
      type: "NOTIFICATION_CREATE_REQUEST",
      payload: {
        values,
        setSubmitting,
        setErrors,
        history,
        _self
      }
    });
  }
  
  handleAddUser =()=> {
    this.form.submitForm()
  }

  handleScheduleStatus = (e) => {
    this.setState({schedule:e})
  }

  render() {
    
    const { match, history } = this.props;
    const { notificationCreate } = this.props
    const { loading, schedule } = this.state;
 
    return (
      <div style={{ border:'1px solid #E6ECF5' , paddingBottom: '10px'}}>
        {/* <HeaderForm 
          loading={loading}
          disabled={isFormValid == false ? true : false}
          title="Add User"
          action={this.handleAddUser}
          actionBtnName="Submit"
          withCancelConfirm={{ message: 'Are you sure you want to discard changes?'}}
          cancel={()=> { this.props.history.push("/user-management")}}
          cancelBtnName="Cancel"
        /> */}
        <div>
          <h2 style={{margin: '25px 35px'}}>Notification Details</h2>
          <Formik
              initialValues={{
                subject: '',
                content: '',
                isScheduled: 'false',
                schedule: '',
                expiration: ''

              }}
              ref={node => (this.form = node)}
              enableReinitialize={true}
              validationSchema={userDetailsSchema}
              onSubmit={this.handleSubmit }
              render = {(props)=> 
                <AddNotificationForm 
                  {...props}
                  history={this.props.history}
                  loading={notificationCreate.createRequestPending || loading}
                  schedule={schedule}
                  handleScheduleStatus={this.handleScheduleStatus}
                />
              }
          />
        </div>
      </div>
    )
  }
}


CreateNotification = connect(
  state => ({
    //userInfo: state
    notificationCreate: state.notificationCreate,
  }),
  { customAction }
)(CreateNotification);

export default CreateNotification;