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/notification_templates/notification_templatesSlice'
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 EditNotification_templates = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    company: null,
    

    
    
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    channel: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    event_key: '',
    

    

    

    

    
    
    
    'subject_template': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    body_template: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_active: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { notification_templatesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Company' labelFor='company'>
        <Field
            name='company'
            id='company'
            component={SelectField}
            options={initialValues.company}
            itemRef={'companies'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="TemplateName"
    >
        <Field
            name="name"
            placeholder="TemplateName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Channel" labelFor="channel">
        <Field name="channel" id="channel" component="select">
          
            <option value="email">email</option>
          
            <option value="whatsapp">whatsapp</option>
          
            <option value="sms">sms</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="EventKey" labelFor="event_key">
        <Field name="event_key" id="event_key" component="select">
          
            <option value="booking_created">booking_created</option>
          
            <option value="payment_succeeded">payment_succeeded</option>
          
            <option value="booking_confirmed">booking_confirmed</option>
          
            <option value="booking_cancelled">booking_cancelled</option>
          
            <option value="pickup_reminder">pickup_reminder</option>
          
            <option value="dropoff_reminder">dropoff_reminder</option>
          
            <option value="checkin_completed">checkin_completed</option>
          
            <option value="invoice_issued">invoice_issued</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="SubjectTemplate"
    >
        <Field
            name="subject_template"
            placeholder="SubjectTemplate"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='BodyTemplate' hasTextareaHeight>
        <Field
            name='body_template'
            id='body_template'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsActive' labelFor='is_active'>
        <Field
            name='is_active'
            id='is_active'
            component={SwitchField}
        ></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('/notification_templates/notification_templates-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditNotification_templates
