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/ad_creatives/ad_creativesSlice'
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 EditAd_creativesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    ad_project: null,
    

    
    
    
    'creative_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    creative_format: '',
    

    

    

    

    
    
    

    

    
    primary_text: '',
    

    

    

    

    

    

    

    

    

    

    
    
    
    'headline': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    description_text: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'call_to_action': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    creative_images: [],
    

    

    
    
    

    

    

    

    

    

    

    

    

    
    creative_files: [],
    

    

    

    
    
    

    
    targeting_notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='AdProject' labelFor='ad_project'>
        <Field
            name='ad_project'
            id='ad_project'
            component={SelectField}
            options={initialValues.ad_project}
            itemRef={'ad_projects'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'project_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="CreativeName"
    >
        <Field
            name="creative_name"
            placeholder="CreativeName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="CreativeFormat" labelFor="creative_format">
        <Field name="creative_format" id="creative_format" component="select">
          
            <option value="image">image</option>
          
            <option value="video">video</option>
          
            <option value="carousel">carousel</option>
          
            <option value="text">text</option>
          
            <option value="landing_page_section">landing_page_section</option>
          
            <option value="email">email</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  
    <FormField label='PrimaryText' hasTextareaHeight>
        <Field
            name='primary_text'
            id='primary_text'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="DescriptionText" hasTextareaHeight>
        <Field name="description_text" as="textarea" placeholder="DescriptionText" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="CallToAction"
    >
        <Field
            name="call_to_action"
            placeholder="CallToAction"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    
        <FormField>
            <Field
                label='CreativeImages'
                color='info'
                icon={mdiUpload}
                path={'ad_creatives/creative_images'}
                name='creative_images'
                id='creative_images'
                schema={{
                    size: undefined,
                    formats: undefined,
                }}
                component={FormImagePicker}
            ></Field>
        </FormField>
    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='CreativeFiles'
            color='info'
            icon={mdiUpload}
            path={'ad_creatives/creative_files'}
            name='creative_files'
            id='creative_files'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></Field>
        </FormField>
  



  

  
    <FormField label="TargetingNotes" hasTextareaHeight>
        <Field name="targeting_notes" as="textarea" placeholder="TargetingNotes" />
    </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('/ad_creatives/ad_creatives-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditAd_creativesPage
