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/albums/albumsSlice'
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 EditAlbums = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    event: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    owner: null,
    

    
    
    
    'title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    description: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    cover_image: [],
    

    

    
    
    

    

    

    

    

    

    

    

    
    privacy: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    collaboration_mode: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    allow_downloads: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    allow_comments: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    allow_likes: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    allow_tagging: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    collaborators: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    access_rules: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    shares: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    stories: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { albumsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Event' labelFor='event'>
        <Field
            name='event'
            id='event'
            component={SelectField}
            options={initialValues.event}
            itemRef={'events'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  
    <FormField
        label="Title"
    >
        <Field
            name="title"
            placeholder="Title"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='Description' hasTextareaHeight>
        <Field
            name='description'
            id='description'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

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

  



  

  

  

  

  

  

  

  
    <FormField label="Privacy" labelFor="privacy">
        <Field name="privacy" id="privacy" component="select">
          
            <option value="public">public</option>
          
            <option value="unlisted">unlisted</option>
          
            <option value="private">private</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="CollaborationMode" labelFor="collaboration_mode">
        <Field name="collaboration_mode" id="collaboration_mode" component="select">
          
            <option value="owner_only">owner_only</option>
          
            <option value="members_can_add">members_can_add</option>
          
            <option value="anyone_with_link_can_add">anyone_with_link_can_add</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='AllowDownloads' labelFor='allow_downloads'>
        <Field
            name='allow_downloads'
            id='allow_downloads'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='AllowComments' labelFor='allow_comments'>
        <Field
            name='allow_comments'
            id='allow_comments'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='AllowLikes' labelFor='allow_likes'>
        <Field
            name='allow_likes'
            id='allow_likes'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='AllowTagging' labelFor='allow_tagging'>
        <Field
            name='allow_tagging'
            id='allow_tagging'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='Collaborators' labelFor='collaborators'>
          <Field
              name='collaborators'
              id='collaborators'
              component={SelectFieldMany}
              options={initialValues.collaborators}
              itemRef={'album_collaborators'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'role'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='AccessRules' labelFor='access_rules'>
          <Field
              name='access_rules'
              id='access_rules'
              component={SelectFieldMany}
              options={initialValues.access_rules}
              itemRef={'access_rules'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'permission'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='Shares' labelFor='shares'>
          <Field
              name='shares'
              id='shares'
              component={SelectFieldMany}
              options={initialValues.shares}
              itemRef={'shares'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'token'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='Stories' labelFor='stories'>
          <Field
              name='stories'
              id='stories'
              component={SelectFieldMany}
              options={initialValues.stories}
              itemRef={'stories'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'title'}
            
            
            
            
            
            
            
            
          ></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('/albums/albums-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditAlbums
