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/private_mailboxes/private_mailboxesSlice'
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";

import {hasPermission} from "../../helpers/userPermissions";



const EditPrivate_mailboxes = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    owner_creator: null,
    

    
    
    
    'public_label': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_enabled: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    allow_attachments: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    delivery_mode: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

  const { private_mailboxes } = useAppSelector((state) => state.private_mailboxes)
  
  const { currentUser } = useAppSelector((state) => state.auth);
  

  const { private_mailboxesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='OwnerCreator' labelFor='owner_creator'>
        <Field
            name='owner_creator'
            id='owner_creator'
            component={SelectField}
            options={initialValues.owner_creator}
            itemRef={'creator_profiles'}
      
        
      
        
      
        
      
        
      
        
            showField={'handle'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="PublicLabel"
    >
        <Field
            name="public_label"
            placeholder="PublicLabel"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsEnabled' labelFor='is_enabled'>
        <Field
            name='is_enabled'
            id='is_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='AllowAttachments' labelFor='allow_attachments'>
        <Field
            name='allow_attachments'
            id='allow_attachments'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="DeliveryMode" labelFor="delivery_mode">
        <Field name="delivery_mode" id="delivery_mode" component="select">
          
            <option value="inbox_only">inbox_only</option>
          
            <option value="forward_to_email">forward_to_email</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='organizations' labelFor='organizations'>
        <Field
            name='organizations'
            id='organizations'
            component={SelectField}
            options={initialValues.organizations}
            itemRef={'organizations'}
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></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('/private_mailboxes/private_mailboxes-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditPrivate_mailboxes
