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/learner_profiles/learner_profilesSlice'
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";



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

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    

    

    

    

    

    

    

    

    
    main_goal: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    preferred_session_format: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    session_language_preference: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    budget_range: '',
    

    

    

    

    
    
    

    
    availability_json: '',
    

    

    

    

    

    

    

    

    

    

    

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

  const { learner_profiles } = useAppSelector((state) => state.learner_profiles)
  

  const { id } = router.query

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

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

  useEffect(() => {
      if (typeof learner_profiles === 'object') {
          const newInitialVal = {...initVals};
          Object.keys(initVals).forEach(el => newInitialVal[el] = (learner_profiles)[el])
          setInitialValues(newInitialVal);
      }
  }, [learner_profiles])

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit learner_profiles')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit learner_profiles'} 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="MainGoal" labelFor="main_goal">
        <Field name="main_goal" id="main_goal" component="select">
          
            <option value="admission_prep">admission_prep</option>
          
            <option value="career_switch">career_switch</option>
          
            <option value="skill_up">skill_up</option>
          
            <option value="exam_result">exam_result</option>
          
            <option value="study_abroad">study_abroad</option>
          
            <option value="personal_interest">personal_interest</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="PreferredSessionFormat" labelFor="preferred_session_format">
        <Field name="preferred_session_format" id="preferred_session_format" component="select">
          
            <option value="live_video">live_video</option>
          
            <option value="async_review">async_review</option>
          
            <option value="both">both</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="SessionLanguagePreference" labelFor="session_language_preference">
        <Field name="session_language_preference" id="session_language_preference" component="select">
          
            <option value="bangla">bangla</option>
          
            <option value="english">english</option>
          
            <option value="either">either</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="BudgetRangePerSession" labelFor="budget_range">
        <Field name="budget_range" id="budget_range" component="select">
          
            <option value="bdt_300_600">bdt_300_600</option>
          
            <option value="bdt_600_1200">bdt_600_1200</option>
          
            <option value="bdt_1200_2500">bdt_1200_2500</option>
          
            <option value="bdt_2500_plus">bdt_2500_plus</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="AvailabilityPreferencesJSON" hasTextareaHeight>
        <Field name="availability_json" as="textarea" placeholder="AvailabilityPreferencesJSON" />
    </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('/learner_profiles/learner_profiles-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditLearner_profilesPage
