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/generation_requests/generation_requestsSlice'
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 EditGeneration_requests = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    workspace: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    project: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    song: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    requested_user: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    model: null,
    

    
    
    

    

    

    

    

    

    

    

    
    request_type: '',
    

    

    

    

    
    
    

    

    
    prompt_text: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    input_asset: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    target_genre: null,
    

    
    
    

    

    

    
    target_bpm: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'target_key': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    mood: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    
    queued_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    started_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    finished_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    
    error_message: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    compute_seconds: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'estimated_cost': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    output_asset: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { generation_requestsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Workspace' labelFor='workspace'>
        <Field
            name='workspace'
            id='workspace'
            component={SelectField}
            options={initialValues.workspace}
            itemRef={'workspaces'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Project' labelFor='project'>
        <Field
            name='project'
            id='project'
            component={SelectField}
            options={initialValues.project}
            itemRef={'projects'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Song' labelFor='song'>
        <Field
            name='song'
            id='song'
            component={SelectField}
            options={initialValues.song}
            itemRef={'songs'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='AIModel' labelFor='model'>
        <Field
            name='model'
            id='model'
            component={SelectField}
            options={initialValues.model}
            itemRef={'ai_models'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="RequestType" labelFor="request_type">
        <Field name="request_type" id="request_type" component="select">
          
            <option value="generate_beat_from_text">generate_beat_from_text</option>
          
            <option value="generate_beat_from_vocals">generate_beat_from_vocals</option>
          
            <option value="arrangement_builder">arrangement_builder</option>
          
            <option value="vocal_processing">vocal_processing</option>
          
            <option value="auto_mix">auto_mix</option>
          
            <option value="mastering">mastering</option>
          
            <option value="cover_art">cover_art</option>
          
            <option value="song_naming">song_naming</option>
          
            <option value="metadata_tagging">metadata_tagging</option>
          
            <option value="genre_suggestion">genre_suggestion</option>
          
            <option value="make_it_amapiano">make_it_amapiano</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  
    <FormField label='PromptText' hasTextareaHeight>
        <Field
            name='prompt_text'
            id='prompt_text'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='InputAsset' labelFor='input_asset'>
        <Field
            name='input_asset'
            id='input_asset'
            component={SelectField}
            options={initialValues.input_asset}
            itemRef={'assets'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  
    <FormField
        label="TargetBPM"
    >
        <Field
            type="number"
            name="target_bpm"
            placeholder="TargetBPM"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="TargetKey"
    >
        <Field
            name="target_key"
            placeholder="TargetKey"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Mood" labelFor="mood">
        <Field name="mood" id="mood" component="select">
          
            <option value="happy">happy</option>
          
            <option value="sad">sad</option>
          
            <option value="spiritual">spiritual</option>
          
            <option value="energetic">energetic</option>
          
            <option value="romantic">romantic</option>
          
            <option value="chill">chill</option>
          
            <option value="aggressive">aggressive</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="queued">queued</option>
          
            <option value="running">running</option>
          
            <option value="succeeded">succeeded</option>
          
            <option value="failed">failed</option>
          
            <option value="canceled">canceled</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  
    <FormField label="ErrorMessage" hasTextareaHeight>
        <Field name="error_message" as="textarea" placeholder="ErrorMessage" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ComputeSeconds"
    >
        <Field
            type="number"
            name="compute_seconds"
            placeholder="ComputeSeconds"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="EstimatedCost"
    >
        <Field
            type="number"
            name="estimated_cost"
            placeholder="EstimatedCost"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='OutputAsset' labelFor='output_asset'>
        <Field
            name='output_asset'
            id='output_asset'
            component={SelectField}
            options={initialValues.output_asset}
            itemRef={'assets'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            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('/generation_requests/generation_requests-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditGeneration_requests
