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/user_settings/user_settingsSlice'
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 EditUser_settingsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    

    

    

    

    

    

    
    push_new_match: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    push_new_message: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    push_profile_like: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    push_daily_spark: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    push_premium: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    privacy_show_distance: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    privacy_show_online_status: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    privacy_allow_discovery: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    
    updated_on: new Date(),
    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit user_settings')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit user_settings'} 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='Push:NewMatch' labelFor='push_new_match'>
        <Field
            name='push_new_match'
            id='push_new_match'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Push:NewMessage' labelFor='push_new_message'>
        <Field
            name='push_new_message'
            id='push_new_message'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Push:ProfileLike' labelFor='push_profile_like'>
        <Field
            name='push_profile_like'
            id='push_profile_like'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Push:DailySpark' labelFor='push_daily_spark'>
        <Field
            name='push_daily_spark'
            id='push_daily_spark'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Push:Premium' labelFor='push_premium'>
        <Field
            name='push_premium'
            id='push_premium'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Privacy:ShowDistance' labelFor='privacy_show_distance'>
        <Field
            name='privacy_show_distance'
            id='privacy_show_distance'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Privacy:ShowOnlineStatus' labelFor='privacy_show_online_status'>
        <Field
            name='privacy_show_online_status'
            id='privacy_show_online_status'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Privacy:AllowDiscovery' labelFor='privacy_allow_discovery'>
        <Field
            name='privacy_allow_discovery'
            id='privacy_allow_discovery'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

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

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

export default EditUser_settingsPage
