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/artist_profiles/artist_profilesSlice'
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 EditArtist_profiles = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    
    'artist_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    profile_type: '',
    

    

    

    

    
    
    
    'legal_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'country': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'city': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tax_residency': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'payout_currency': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    press_photos: [],
    

    

    
    
    

    

    
    bio: '',
    

    

    

    

    

    

    

    

    

    

    
    
    
    'website_url': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'vk_url': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'youtube_url': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tiktok_url': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'instagram_url': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { artist_profilesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

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

  

  

    

  



  
    <FormField
        label="Artistname"
    >
        <Field
            name="artist_name"
            placeholder="Artistname"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Profiletype" labelFor="profile_type">
        <Field name="profile_type" id="profile_type" component="select">
          
            <option value="solo_artist">solo_artist</option>
          
            <option value="group">group</option>
          
            <option value="label">label</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Legalname"
    >
        <Field
            name="legal_name"
            placeholder="Legalname"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Taxresidency"
    >
        <Field
            name="tax_residency"
            placeholder="Taxresidency"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Payoutcurrency"
    >
        <Field
            name="payout_currency"
            placeholder="Payoutcurrency"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

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

  



  

  

  
    <FormField label='Bio' hasTextareaHeight>
        <Field
            name='bio'
            id='bio'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="WebsiteURL"
    >
        <Field
            name="website_url"
            placeholder="WebsiteURL"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="VKURL"
    >
        <Field
            name="vk_url"
            placeholder="VKURL"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="YouTubeURL"
    >
        <Field
            name="youtube_url"
            placeholder="YouTubeURL"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="TikTokURL"
    >
        <Field
            name="tiktok_url"
            placeholder="TikTokURL"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



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

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

export default EditArtist_profiles
