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/releases/releasesSlice'
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 EditReleasesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    artist_profile: null,
    

    
    
    
    'title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    release_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    primary_genre: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    secondary_genre: null,
    

    
    
    
    'language': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    cover_art: [],
    

    

    
    
    

    

    

    

    

    

    

    

    

    
    booklet_files: [],
    

    

    

    
    
    
    'label_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'copyright_notice': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'production_notice': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'upc_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    upc_source: '',
    

    

    

    

    
    
    

    

    

    

    

    
    release_date: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    original_release_date: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    moderation_status: '',
    

    

    

    

    
    
    

    
    moderation_notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    explicit_content: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    license_accepted: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    content_id_enabled: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    content_match_enabled: false,
    

    

    

    

    

    

    
    
    
    'internal_catalog_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    selected_platforms: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { id } = router.query

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

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit releases')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit releases'} 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="Releasetitle"
    >
        <Field
            name="title"
            placeholder="Releasetitle"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Releasetype" labelFor="release_type">
        <Field name="release_type" id="release_type" component="select">
          
            <option value="single">single</option>
          
            <option value="ep">ep</option>
          
            <option value="album">album</option>
          
            <option value="compilation">compilation</option>
          
            <option value="podcast">podcast</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Primarygenre' labelFor='primary_genre'>
        <Field
            name='primary_genre'
            id='primary_genre'
            component={SelectField}
            options={initialValues.primary_genre}
            itemRef={'genres'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Secondarygenre' labelFor='secondary_genre'>
        <Field
            name='secondary_genre'
            id='secondary_genre'
            component={SelectField}
            options={initialValues.secondary_genre}
            itemRef={'genres'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="Language"
    >
        <Field
            name="language"
            placeholder="Language"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

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

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='Bookletfiles'
            color='info'
            icon={mdiUpload}
            path={'releases/booklet_files'}
            name='booklet_files'
            id='booklet_files'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></Field>
        </FormField>
  



  
    <FormField
        label="Labelname"
    >
        <Field
            name="label_name"
            placeholder="Labelname"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Copyrightnotice"
    >
        <Field
            name="copyright_notice"
            placeholder="Copyrightnotice"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Productionnotice"
    >
        <Field
            name="production_notice"
            placeholder="Productionnotice"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="UPC/EANcode"
    >
        <Field
            name="upc_code"
            placeholder="UPC/EANcode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="UPCsource" labelFor="upc_source">
        <Field name="upc_source" id="upc_source" component="select">
          
            <option value="auto">auto</option>
          
            <option value="manual">manual</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Moderationstatus" labelFor="moderation_status">
        <Field name="moderation_status" id="moderation_status" component="select">
          
            <option value="draft">draft</option>
          
            <option value="submitted">submitted</option>
          
            <option value="in_review">in_review</option>
          
            <option value="changes_requested">changes_requested</option>
          
            <option value="approved">approved</option>
          
            <option value="published">published</option>
          
            <option value="rejected">rejected</option>
          
            <option value="takedown_requested">takedown_requested</option>
          
            <option value="takedown_completed">takedown_completed</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="Moderationnotes" hasTextareaHeight>
        <Field name="moderation_notes" as="textarea" placeholder="Moderationnotes" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Explicitcontent' labelFor='explicit_content'>
        <Field
            name='explicit_content'
            id='explicit_content'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Licenseaccepted' labelFor='license_accepted'>
        <Field
            name='license_accepted'
            id='license_accepted'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='ContentIDenabled' labelFor='content_id_enabled'>
        <Field
            name='content_id_enabled'
            id='content_id_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Contentmatchenabled' labelFor='content_match_enabled'>
        <Field
            name='content_match_enabled'
            id='content_match_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  
    <FormField
        label="Internalcatalogcode"
    >
        <Field
            name="internal_catalog_code"
            placeholder="Internalcatalogcode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

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

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

export default EditReleasesPage
