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/custom_artwork_requests/custom_artwork_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";



const EditCustom_artwork_requests = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    source_images: [],
    

    

    
    
    

    

    

    

    

    

    

    

    

    
    pattern_files: [],
    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    pattern_preview_images: [],
    

    

    
    
    

    

    

    

    

    

    

    

    
    selected_size: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    selected_difficulty: '',
    

    

    

    

    
    
    

    

    

    
    estimated_color_count: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'estimated_price': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    instructions: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'ai_model_version': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    submitted_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    processed_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    approved_at: new Date(),
    

    

    

    

    

    

    

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

  const { custom_artwork_requests } = useAppSelector((state) => state.custom_artwork_requests)
  

  const { custom_artwork_requestsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit custom_artwork_requests')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit custom_artwork_requests'} 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="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="uploaded">uploaded</option>
          
            <option value="processing">processing</option>
          
            <option value="ready_for_selection">ready_for_selection</option>
          
            <option value="awaiting_payment">awaiting_payment</option>
          
            <option value="in_production">in_production</option>
          
            <option value="completed">completed</option>
          
            <option value="rejected">rejected</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

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

  



  

  

  

  

  

  

  

  

  

  

  

    

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



  

  

  

  

  

  

  

  

  

  

  

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

  



  

  

  

  

  

  

  

  
    <FormField label="SelectedSize" labelFor="selected_size">
        <Field name="selected_size" id="selected_size" component="select">
          
            <option value="20x30">20x30</option>
          
            <option value="30x40">30x40</option>
          
            <option value="40x50">40x50</option>
          
            <option value="50x70">50x70</option>
          
            <option value="60x80">60x80</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="SelectedDifficulty" labelFor="selected_difficulty">
        <Field name="selected_difficulty" id="selected_difficulty" component="select">
          
            <option value="easy">easy</option>
          
            <option value="medium">medium</option>
          
            <option value="hard">hard</option>
          
            <option value="expert">expert</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="EstimatedColorCount"
    >
        <Field
            type="number"
            name="estimated_color_count"
            placeholder="EstimatedColorCount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="EstimatedPrice"
    >
        <Field
            type="number"
            name="estimated_price"
            placeholder="EstimatedPrice"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Instructions" hasTextareaHeight>
        <Field name="instructions" as="textarea" placeholder="Instructions" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="AIModelVersion"
    >
        <Field
            name="ai_model_version"
            placeholder="AIModelVersion"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="ApprovedAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.approved_at ?
                  new Date(
                      dayjs(initialValues.approved_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'approved_at': date})}
          />
      </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('/custom_artwork_requests/custom_artwork_requests-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditCustom_artwork_requests
