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/tip_pages/tip_pagesSlice'
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 EditTip_pagesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    creator: null,
    

    
    
    
    'slug': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'headline': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    thank_you_message: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    background_type: '',
    

    

    

    

    
    
    
    'background_value': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'accent_color_hex': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    preset_amount_1: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    preset_amount_2: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    preset_amount_3: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    allow_custom_amount: false,
    

    

    

    

    

    

    
    
    

    

    

    
    minimum_amount: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    tts_enabled: false,
    

    

    

    

    

    

    
    
    
    'tts_voice': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    show_supporter_feed: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { id } = router.query

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

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

  useEffect(() => {
      if (typeof tip_pages === 'object') {
          const newInitialVal = {...initVals};
          Object.keys(initVals).forEach(el => newInitialVal[el] = (tip_pages)[el])
          setInitialValues(newInitialVal);
      }
  }, [tip_pages])

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

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


  

  

  

  

  

  

  

  

  

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

  

  

    

  



  
    <FormField
        label="TipPageSlug"
    >
        <Field
            name="slug"
            placeholder="TipPageSlug"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Headline"
    >
        <Field
            name="headline"
            placeholder="Headline"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='ThankYouMessage' hasTextareaHeight>
        <Field
            name='thank_you_message'
            id='thank_you_message'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="BackgroundType" labelFor="background_type">
        <Field name="background_type" id="background_type" component="select">
          
            <option value="color">color</option>
          
            <option value="gradient">gradient</option>
          
            <option value="image">image</option>
          
            <option value="video">video</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="BackgroundValue"
    >
        <Field
            name="background_value"
            placeholder="BackgroundValue"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="AccentColorHex"
    >
        <Field
            name="accent_color_hex"
            placeholder="AccentColorHex"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="PresetAmount1"
    >
        <Field
            type="number"
            name="preset_amount_1"
            placeholder="PresetAmount1"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="PresetAmount2"
    >
        <Field
            type="number"
            name="preset_amount_2"
            placeholder="PresetAmount2"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="PresetAmount3"
    >
        <Field
            type="number"
            name="preset_amount_3"
            placeholder="PresetAmount3"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='AllowCustomAmount' labelFor='allow_custom_amount'>
        <Field
            name='allow_custom_amount'
            id='allow_custom_amount'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  
    <FormField
        label="MinimumAmount"
    >
        <Field
            type="number"
            name="minimum_amount"
            placeholder="MinimumAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='TTSEnabled' labelFor='tts_enabled'>
        <Field
            name='tts_enabled'
            id='tts_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  
    <FormField
        label="TTSVoice"
    >
        <Field
            name="tts_voice"
            placeholder="TTSVoice"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='ShowSupporterFeed' labelFor='show_supporter_feed'>
        <Field
            name='show_supporter_feed'
            id='show_supporter_feed'
            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('/tip_pages/tip_pages-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditTip_pagesPage
