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/track_contributors/track_contributorsSlice'
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 EditTrack_contributors = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    track: null,
    

    
    
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    role: '',
    

    

    

    

    
    
    
    'spotify_artist_url': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'apple_artist_url': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'society_identifier': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { track_contributorsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

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

  

  

    

  



  
    <FormField
        label="Contributorname"
    >
        <Field
            name="name"
            placeholder="Contributorname"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Role" labelFor="role">
        <Field name="role" id="role" component="select">
          
            <option value="primary_artist">primary_artist</option>
          
            <option value="featured_artist">featured_artist</option>
          
            <option value="producer">producer</option>
          
            <option value="composer">composer</option>
          
            <option value="lyricist">lyricist</option>
          
            <option value="mixer">mixer</option>
          
            <option value="mastering_engineer">mastering_engineer</option>
          
            <option value="session_musician">session_musician</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="SpotifyartistURL"
    >
        <Field
            name="spotify_artist_url"
            placeholder="SpotifyartistURL"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="AppleartistURL"
    >
        <Field
            name="apple_artist_url"
            placeholder="AppleartistURL"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



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

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

export default EditTrack_contributors
