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/message_recipients/message_recipientsSlice'
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 EditMessage_recipients = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    organization: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    message: null,
    

    
    
    

    

    

    

    

    

    

    

    
    recipient_type: '',
    

    

    

    

    
    
    
    'recipient_label': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'destination': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    delivery_status: '',
    

    

    

    

    
    
    

    

    

    

    

    
    delivered_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    read_at: new Date(),
    

    

    

    

    

    

    

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

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

  const { message_recipientsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
    {hasPermission(currentUser, 'READ_ORGANIZATIONS') && 
    <FormField label='Organization' labelFor='organization'>
        <Field
            name='organization'
            id='organization'
            component={SelectField}
            options={initialValues.organization}
            itemRef={'organizations'}
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
     } 

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Message' labelFor='message'>
        <Field
            name='message'
            id='message'
            component={SelectField}
            options={initialValues.message}
            itemRef={'messages'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'subject'}
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="RecipientType" labelFor="recipient_type">
        <Field name="recipient_type" id="recipient_type" component="select">
          
            <option value="user">user</option>
          
            <option value="student">student</option>
          
            <option value="guardian">guardian</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="RecipientLabel"
    >
        <Field
            name="recipient_label"
            placeholder="RecipientLabel"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Destination"
    >
        <Field
            name="destination"
            placeholder="Destination"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="DeliveryStatus" labelFor="delivery_status">
        <Field name="delivery_status" id="delivery_status" component="select">
          
            <option value="pending">pending</option>
          
            <option value="sent">sent</option>
          
            <option value="delivered">delivered</option>
          
            <option value="failed">failed</option>
          
            <option value="read">read</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

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

export default EditMessage_recipients
