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/notifications/notificationsSlice'
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 EditNotifications = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    recipient: null,
    

    
    
    

    

    

    

    

    

    

    

    
    notification_type: '',
    

    

    

    

    
    
    
    'title_text': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    body_text: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    priority: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    post: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    comment: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    group: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    conversation: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    event: null,
    

    
    
    

    

    

    

    

    

    
    is_read: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    
    sent_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    read_at: new Date(),
    

    

    

    

    

    

    

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

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

  const { notificationsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="NotificationType" labelFor="notification_type">
        <Field name="notification_type" id="notification_type" component="select">
          
            <option value="like">like</option>
          
            <option value="comment">comment</option>
          
            <option value="follow">follow</option>
          
            <option value="mention">mention</option>
          
            <option value="share">share</option>
          
            <option value="message">message</option>
          
            <option value="group_invite">group_invite</option>
          
            <option value="group_request">group_request</option>
          
            <option value="event_invite">event_invite</option>
          
            <option value="live_started">live_started</option>
          
            <option value="system">system</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Title"
    >
        <Field
            name="title_text"
            placeholder="Title"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Body" hasTextareaHeight>
        <Field name="body_text" as="textarea" placeholder="Body" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Priority" labelFor="priority">
        <Field name="priority" id="priority" component="select">
          
            <option value="low">low</option>
          
            <option value="normal">normal</option>
          
            <option value="high">high</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Post' labelFor='post'>
        <Field
            name='post'
            id='post'
            component={SelectField}
            options={initialValues.post}
            itemRef={'posts'}
      
        
      
        
      
        
      
        
            showField={'caption'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Comment' labelFor='comment'>
        <Field
            name='comment'
            id='comment'
            component={SelectField}
            options={initialValues.comment}
            itemRef={'comments'}
      
        
      
        
      
        
      
        
      
        
            showField={'body'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Group' labelFor='group'>
        <Field
            name='group'
            id='group'
            component={SelectField}
            options={initialValues.group}
            itemRef={'groups'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name_text'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Conversation' labelFor='conversation'>
        <Field
            name='conversation'
            id='conversation'
            component={SelectField}
            options={initialValues.conversation}
            itemRef={'conversations'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title_text'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Event' labelFor='event'>
        <Field
            name='event'
            id='event'
            component={SelectField}
            options={initialValues.event}
            itemRef={'events'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title_text'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Read' labelFor='is_read'>
        <Field
            name='is_read'
            id='is_read'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

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

export default EditNotifications
