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/discovery_preferences/discovery_preferencesSlice'
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 EditDiscovery_preferences = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    

    

    

    
    min_age: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    max_age: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    max_distance_km: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    min_height_cm: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    max_height_cm: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    dating_intention: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    preferred_gender: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    verified_only: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    online_only: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    advanced_filters_enabled: false,
    

    

    

    

    

    

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

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

  const { discovery_preferencesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit discovery_preferences')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit discovery_preferences'} 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="MinimumAge"
    >
        <Field
            type="number"
            name="min_age"
            placeholder="MinimumAge"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MaximumAge"
    >
        <Field
            type="number"
            name="max_age"
            placeholder="MaximumAge"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MaximumDistance(km)"
    >
        <Field
            type="number"
            name="max_distance_km"
            placeholder="MaximumDistance(km)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MinimumHeight(cm)"
    >
        <Field
            type="number"
            name="min_height_cm"
            placeholder="MinimumHeight(cm)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MaximumHeight(cm)"
    >
        <Field
            type="number"
            name="max_height_cm"
            placeholder="MaximumHeight(cm)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="DatingIntention" labelFor="dating_intention">
        <Field name="dating_intention" id="dating_intention" component="select">
          
            <option value="chat">chat</option>
          
            <option value="casual">casual</option>
          
            <option value="relationship">relationship</option>
          
            <option value="marriage">marriage</option>
          
            <option value="friends">friends</option>
          
            <option value="not_sure">not_sure</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="PreferredGender" labelFor="preferred_gender">
        <Field name="preferred_gender" id="preferred_gender" component="select">
          
            <option value="women">women</option>
          
            <option value="men">men</option>
          
            <option value="everyone">everyone</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='VerifiedOnly' labelFor='verified_only'>
        <Field
            name='verified_only'
            id='verified_only'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='OnlineOnly' labelFor='online_only'>
        <Field
            name='online_only'
            id='online_only'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='AdvancedFiltersEnabled' labelFor='advanced_filters_enabled'>
        <Field
            name='advanced_filters_enabled'
            id='advanced_filters_enabled'
            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('/discovery_preferences/discovery_preferences-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditDiscovery_preferences
