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/reviews/reviewsSlice'
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 EditReviews = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    booking: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    tutor: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    learner: null,
    

    
    
    
    'rating': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    communication_rating: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    knowledge_rating: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    punctuality_rating: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    value_rating: '',
    

    

    

    

    

    

    

    

    

    
    
    

    
    review_text: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    would_recommend: false,
    

    

    

    

    

    

    
    
    

    
    tutor_response: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    tutor_response_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    
    published_at: new Date(),
    

    

    

    

    

    

    

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

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

  const { reviewsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  
    <FormField
        label="OverallRating"
    >
        <Field
            type="number"
            name="rating"
            placeholder="OverallRating"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="CommunicationRating"
    >
        <Field
            type="number"
            name="communication_rating"
            placeholder="CommunicationRating"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="KnowledgeRating"
    >
        <Field
            type="number"
            name="knowledge_rating"
            placeholder="KnowledgeRating"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="PunctualityRating"
    >
        <Field
            type="number"
            name="punctuality_rating"
            placeholder="PunctualityRating"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ValueRating"
    >
        <Field
            type="number"
            name="value_rating"
            placeholder="ValueRating"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="ReviewText" hasTextareaHeight>
        <Field name="review_text" as="textarea" placeholder="ReviewText" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='WouldRecommend' labelFor='would_recommend'>
        <Field
            name='would_recommend'
            id='would_recommend'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  
    <FormField label="TutorResponse" hasTextareaHeight>
        <Field name="tutor_response" as="textarea" placeholder="TutorResponse" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ModerationStatus" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="pending">pending</option>
          
            <option value="published">published</option>
          
            <option value="removed">removed</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="PublishedAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.published_at ?
                  new Date(
                      dayjs(initialValues.published_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'published_at': date})}
          />
      </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('/reviews/reviews-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditReviews
