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_enabled: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    email_enabled: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    sms_enabled: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    theme_preference: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    language_preference: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    show_sensitive_content: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    allow_message_requests: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    read_receipts_enabled: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    activity_status_visible: false,
    

    

    

    

    

    

    
    
    
  }
  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='PushNotificationsEnabled' labelFor='push_enabled'>
        <Field
            name='push_enabled'
            id='push_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='EmailNotificationsEnabled' labelFor='email_enabled'>
        <Field
            name='email_enabled'
            id='email_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='SMSNotificationsEnabled' labelFor='sms_enabled'>
        <Field
            name='sms_enabled'
            id='sms_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ThemePreference" labelFor="theme_preference">
        <Field name="theme_preference" id="theme_preference" component="select">
          
            <option value="system">system</option>
          
            <option value="light">light</option>
          
            <option value="dark">dark</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="LanguagePreference" labelFor="language_preference">
        <Field name="language_preference" id="language_preference" component="select">
          
            <option value="en">en</option>
          
            <option value="es">es</option>
          
            <option value="fr">fr</option>
          
            <option value="de">de</option>
          
            <option value="pt">pt</option>
          
            <option value="hi">hi</option>
          
            <option value="ja">ja</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='ShowSensitiveContent' labelFor='show_sensitive_content'>
        <Field
            name='show_sensitive_content'
            id='show_sensitive_content'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='AllowMessageRequests' labelFor='allow_message_requests'>
        <Field
            name='allow_message_requests'
            id='allow_message_requests'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='ReadReceiptsEnabled' labelFor='read_receipts_enabled'>
        <Field
            name='read_receipts_enabled'
            id='read_receipts_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='ActivityStatusVisible' labelFor='activity_status_visible'>
        <Field
            name='activity_status_visible'
            id='activity_status_visible'
            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('/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
