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/insight_snapshots/insight_snapshotsSlice'
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 EditInsight_snapshotsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    owner: null,
    

    
    
    

    

    

    

    

    

    

    

    
    content_type: '',
    

    

    

    

    
    
    
    'content_ref': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    window_start_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    window_end_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    
    impressions: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    reach: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    profile_visits: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    likes: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    comments: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    saves: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    shares: '',
    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Owner' labelFor='owner'>
        <Field
            name='owner'
            id='owner'
            component={SelectField}
            options={initialValues.owner}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ContentType" labelFor="content_type">
        <Field name="content_type" id="content_type" component="select">
          
            <option value="post">post</option>
          
            <option value="story">story</option>
          
            <option value="reel">reel</option>
          
            <option value="profile">profile</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="ContentReference"
    >
        <Field
            name="content_ref"
            placeholder="ContentReference"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Impressions"
    >
        <Field
            type="number"
            name="impressions"
            placeholder="Impressions"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Reach"
    >
        <Field
            type="number"
            name="reach"
            placeholder="Reach"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ProfileVisits"
    >
        <Field
            type="number"
            name="profile_visits"
            placeholder="ProfileVisits"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Likes"
    >
        <Field
            type="number"
            name="likes"
            placeholder="Likes"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Comments"
    >
        <Field
            type="number"
            name="comments"
            placeholder="Comments"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Saves"
    >
        <Field
            type="number"
            name="saves"
            placeholder="Saves"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Shares"
    >
        <Field
            type="number"
            name="shares"
            placeholder="Shares"
        />
    </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('/insight_snapshots/insight_snapshots-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditInsight_snapshotsPage
