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/gdpr_consents/gdpr_consentsSlice'
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 EditGdpr_consents = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    
    terms_accepted: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    
    terms_accepted_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    privacy_policy_accepted: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    
    privacy_policy_accepted_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    marketing_emails_opt_in: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    
    marketing_opt_in_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    consent_source: '',
    

    

    

    

    
    
    
    'consent_version': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'ip_address': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'user_agent': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    lawful_basis: '',
    

    

    

    

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

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

  const { gdpr_consentsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  
    <FormField label='TermsAccepted' labelFor='terms_accepted'>
        <Field
            name='terms_accepted'
            id='terms_accepted'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='PrivacyPolicyAccepted' labelFor='privacy_policy_accepted'>
        <Field
            name='privacy_policy_accepted'
            id='privacy_policy_accepted'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='MarketingEmailsOptIn' labelFor='marketing_emails_opt_in'>
        <Field
            name='marketing_emails_opt_in'
            id='marketing_emails_opt_in'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ConsentSource" labelFor="consent_source">
        <Field name="consent_source" id="consent_source" component="select">
          
            <option value="web_guest_checkout">web_guest_checkout</option>
          
            <option value="account_settings">account_settings</option>
          
            <option value="admin_recorded">admin_recorded</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="ConsentVersion"
    >
        <Field
            name="consent_version"
            placeholder="ConsentVersion"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="IPAddress"
    >
        <Field
            name="ip_address"
            placeholder="IPAddress"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="UserAgent"
    >
        <Field
            name="user_agent"
            placeholder="UserAgent"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="LawfulBasis" labelFor="lawful_basis">
        <Field name="lawful_basis" id="lawful_basis" component="select">
          
            <option value="contract">contract</option>
          
            <option value="consent">consent</option>
          
            <option value="legitimate_interest">legitimate_interest</option>
          
            <option value="legal_obligation">legal_obligation</option>
          
        </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('/gdpr_consents/gdpr_consents-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditGdpr_consents
