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/feedback_entries/feedback_entriesSlice'
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 EditFeedback_entries = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    

    

    

    

    

    

    

    

    
    feedback_type: '',
    

    

    

    

    
    
    

    

    

    
    score: '',
    

    

    

    

    

    

    

    

    

    
    
    

    
    comment: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    source_surface: '',
    

    

    

    

    
    
    

    

    

    

    

    
    submitted_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    needs_follow_up: false,
    

    

    

    

    

    

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

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

  const { feedback_entriesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit feedback_entries')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit feedback_entries'} 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="FeedbackType" labelFor="feedback_type">
        <Field name="feedback_type" id="feedback_type" component="select">
          
            <option value="nps">nps</option>
          
            <option value="csat">csat</option>
          
            <option value="bug">bug</option>
          
            <option value="feature_request">feature_request</option>
          
            <option value="freeform">freeform</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Score"
    >
        <Field
            type="number"
            name="score"
            placeholder="Score"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Comment" hasTextareaHeight>
        <Field name="comment" as="textarea" placeholder="Comment" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="SourceSurface" labelFor="source_surface">
        <Field name="source_surface" id="source_surface" component="select">
          
            <option value="landing">landing</option>
          
            <option value="onboarding">onboarding</option>
          
            <option value="profile">profile</option>
          
            <option value="coach">coach</option>
          
            <option value="offer">offer</option>
          
            <option value="checkout">checkout</option>
          
            <option value="course">course</option>
          
            <option value="lesson">lesson</option>
          
            <option value="trophy_case">trophy_case</option>
          
            <option value="other">other</option>
          
        </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='NeedsFollowUp' labelFor='needs_follow_up'>
        <Field
            name='needs_follow_up'
            id='needs_follow_up'
            component={SwitchField}
        ></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('/feedback_entries/feedback_entries-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditFeedback_entries
