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/analytics_daily/analytics_dailySlice'
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 EditAnalytics_daily = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    artist_profile: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    platform: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    release: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    track: null,
    

    
    
    

    

    

    

    

    
    metric_date: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    
    streams: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    unique_listeners: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    saves: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    likes: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    shares: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'country': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'city': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'traffic_source': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { analytics_dailyId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Artistprofile' labelFor='artist_profile'>
        <Field
            name='artist_profile'
            id='artist_profile'
            component={SelectField}
            options={initialValues.artist_profile}
            itemRef={'artist_profiles'}
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'artist_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Platform' labelFor='platform'>
        <Field
            name='platform'
            id='platform'
            component={SelectField}
            options={initialValues.platform}
            itemRef={'distribution_platforms'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Release' labelFor='release'>
        <Field
            name='release'
            id='release'
            component={SelectField}
            options={initialValues.release}
            itemRef={'releases'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Track' labelFor='track'>
        <Field
            name='track'
            id='track'
            component={SelectField}
            options={initialValues.track}
            itemRef={'tracks'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Streams"
    >
        <Field
            type="number"
            name="streams"
            placeholder="Streams"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Uniquelisteners"
    >
        <Field
            type="number"
            name="unique_listeners"
            placeholder="Uniquelisteners"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

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

  

  

  

  

  

  

  

    

  



  

  

  

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

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Shares"
    >
        <Field
            type="number"
            name="shares"
            placeholder="Shares"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Country"
    >
        <Field
            name="country"
            placeholder="Country"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="City"
    >
        <Field
            name="city"
            placeholder="City"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Trafficsource"
    >
        <Field
            name="traffic_source"
            placeholder="Trafficsource"
        />
    </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('/analytics_daily/analytics_daily-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditAnalytics_daily
