import { mdiChartTimelineVariant, mdiUpload } from '@mdi/js'
import Head from 'next/head'
import React, { ReactElement, useEffect, useState } from 'react'
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import dayjs from "dayjs";

import CardBox from '../../components/CardBox'
import LayoutAuthenticated from '../../layouts/Authenticated'
import SectionMain from '../../components/SectionMain'
import SectionTitleLineWithButton from '../../components/SectionTitleLineWithButton'
import { getPageTitle } from '../../config'

import { Field, Form, Formik } from 'formik'
import FormField from '../../components/FormField'
import BaseDivider from '../../components/BaseDivider'
import BaseButtons from '../../components/BaseButtons'
import BaseButton from '../../components/BaseButton'
import FormCheckRadio from '../../components/FormCheckRadio'
import FormCheckRadioGroup from '../../components/FormCheckRadioGroup'
import FormFilePicker from '../../components/FormFilePicker'
import FormImagePicker from '../../components/FormImagePicker'
import { SelectField } from "../../components/SelectField";
import { SelectFieldMany } from "../../components/SelectFieldMany";
import { SwitchField } from '../../components/SwitchField'
import {RichTextField} from "../../components/RichTextField";

import { update, fetch } from '../../stores/onboarding_responses/onboarding_responsesSlice'
import { useAppDispatch, useAppSelector } from '../../stores/hooks'
import { useRouter } from 'next/router'
import {saveFile} from "../../helpers/fileSaver";
import dataFormatter from '../../helpers/dataFormatter';
import ImageField from "../../components/ImageField";

import {hasPermission} from "../../helpers/userPermissions";



const EditOnboarding_responses = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    

    

    

    

    

    

    

    

    
    step_name: '',
    

    

    

    

    
    
    

    
    learning_goals_text: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    learning_goal_primary: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    linkedin_connected: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    github_connected: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    
    submitted_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

    
    
    
  }
  const [initialValues, setInitialValues] = useState(initVals)

  const { onboarding_responses } = useAppSelector((state) => state.onboarding_responses)
  
  const { currentUser } = useAppSelector((state) => state.auth);
  

  const { onboarding_responsesId } = router.query

  useEffect(() => {
    dispatch(fetch({ id: onboarding_responsesId }))
  }, [onboarding_responsesId])

  useEffect(() => {
    if (typeof onboarding_responses === 'object') {
      setInitialValues(onboarding_responses)
    }
  }, [onboarding_responses])

  useEffect(() => {
      if (typeof onboarding_responses === 'object') {

          const newInitialVal = {...initVals};

          Object.keys(initVals).forEach(el => newInitialVal[el] = (onboarding_responses)[el])

          setInitialValues(newInitialVal);
      }
  }, [onboarding_responses])

  const handleSubmit = async (data) => {
    await dispatch(update({ id: onboarding_responsesId, data }))
    await router.push('/onboarding_responses/onboarding_responses-list')
  }

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit onboarding_responses')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit onboarding_responses'} main>
        {''}
        </SectionTitleLineWithButton>
        <CardBox>
          <Formik
            enableReinitialize
            initialValues={initialValues}
            onSubmit={(values) => handleSubmit(values)}
          >
            <Form>


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='User' labelFor='user'>
        <Field
            name='user'
            id='user'
            component={SelectField}
            options={initialValues.user}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Step" labelFor="step_name">
        <Field name="step_name" id="step_name" component="select">
          
            <option value="role">role</option>
          
            <option value="learning_goals">learning_goals</option>
          
            <option value="connect_accounts">connect_accounts</option>
          
            <option value="complete">complete</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="LearningGoalsText" hasTextareaHeight>
        <Field name="learning_goals_text" as="textarea" placeholder="LearningGoalsText" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="PrimaryLearningGoal" labelFor="learning_goal_primary">
        <Field name="learning_goal_primary" id="learning_goal_primary" component="select">
          
            <option value="apply_ai_at_work">apply_ai_at_work</option>
          
            <option value="career_change">career_change</option>
          
            <option value="productivity">productivity</option>
          
            <option value="leadership">leadership</option>
          
            <option value="build_ai_projects">build_ai_projects</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='LinkedInConnected' labelFor='linkedin_connected'>
        <Field
            name='linkedin_connected'
            id='linkedin_connected'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='GitHubConnected' labelFor='github_connected'>
        <Field
            name='github_connected'
            id='github_connected'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="SubmittedAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.submitted_at ?
                  new Date(
                      dayjs(initialValues.submitted_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'submitted_at': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='organizations' labelFor='organizations'>
        <Field
            name='organizations'
            id='organizations'
            component={SelectField}
            options={initialValues.organizations}
            itemRef={'organizations'}
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  


  

              <BaseDivider />
              <BaseButtons>
                <BaseButton type="submit" color="info" label="Submit" />
                <BaseButton type="reset" color="info" outline label="Reset" />
                <BaseButton type='reset' color='danger' outline label='Cancel' onClick={() => router.push('/onboarding_responses/onboarding_responses-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

EditOnboarding_responses.getLayout = function getLayout(page: ReactElement) {
  return (
      <LayoutAuthenticated
        
          permission={'UPDATE_ONBOARDING_RESPONSES'}
        
      >
          {page}
      </LayoutAuthenticated>
  )
}

export default EditOnboarding_responses
