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/media_items/media_itemsSlice'
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 EditMedia_items = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    album: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    uploader: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    storage_object: null,
    

    
    
    

    

    

    

    

    

    

    

    
    media_type: '',
    

    

    

    

    
    
    
    'title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    caption: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    captured_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    uploaded_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    
    width: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    height: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'duration_seconds': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'camera_make': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'camera_model': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'gps_latitude': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'gps_longitude': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    visibility: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    is_duplicate: false,
    

    

    

    

    

    

    
    
    
    'duplicate_group_key': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    ai_tags: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    user_tags: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    likes: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    comments: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    saves: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    mentions: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    face_detections: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    watermarked_downloads: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { media_itemsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Album' labelFor='album'>
        <Field
            name='album'
            id='album'
            component={SelectField}
            options={initialValues.album}
            itemRef={'albums'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='StorageObject' labelFor='storage_object'>
        <Field
            name='storage_object'
            id='storage_object'
            component={SelectField}
            options={initialValues.storage_object}
            itemRef={'storage_objects'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'object_key'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="MediaType" labelFor="media_type">
        <Field name="media_type" id="media_type" component="select">
          
            <option value="photo">photo</option>
          
            <option value="video">video</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Title"
    >
        <Field
            name="title"
            placeholder="Title"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='Caption' hasTextareaHeight>
        <Field
            name='caption'
            id='caption'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Width"
    >
        <Field
            type="number"
            name="width"
            placeholder="Width"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Height"
    >
        <Field
            type="number"
            name="height"
            placeholder="Height"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DurationSeconds"
    >
        <Field
            type="number"
            name="duration_seconds"
            placeholder="DurationSeconds"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="CameraMake"
    >
        <Field
            name="camera_make"
            placeholder="CameraMake"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="CameraModel"
    >
        <Field
            name="camera_model"
            placeholder="CameraModel"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="GPSLatitude"
    >
        <Field
            type="number"
            name="gps_latitude"
            placeholder="GPSLatitude"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="GPSLongitude"
    >
        <Field
            type="number"
            name="gps_longitude"
            placeholder="GPSLongitude"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Visibility" labelFor="visibility">
        <Field name="visibility" id="visibility" component="select">
          
            <option value="visible">visible</option>
          
            <option value="hidden">hidden</option>
          
            <option value="flagged">flagged</option>
          
            <option value="deleted">deleted</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsDuplicate' labelFor='is_duplicate'>
        <Field
            name='is_duplicate'
            id='is_duplicate'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  
    <FormField
        label="DuplicateGroupKey"
    >
        <Field
            name="duplicate_group_key"
            placeholder="DuplicateGroupKey"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='AITags' labelFor='ai_tags'>
          <Field
              name='ai_tags'
              id='ai_tags'
              component={SelectFieldMany}
              options={initialValues.ai_tags}
              itemRef={'media_ai_tags'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'source'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='UserTags' labelFor='user_tags'>
          <Field
              name='user_tags'
              id='user_tags'
              component={SelectFieldMany}
              options={initialValues.user_tags}
              itemRef={'media_user_tags'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'tagged_by'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='Likes' labelFor='likes'>
          <Field
              name='likes'
              id='likes'
              component={SelectFieldMany}
              options={initialValues.likes}
              itemRef={'likes'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'liked_at'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='Comments' labelFor='comments'>
          <Field
              name='comments'
              id='comments'
              component={SelectFieldMany}
              options={initialValues.comments}
              itemRef={'comments'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'content'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='Saves' labelFor='saves'>
          <Field
              name='saves'
              id='saves'
              component={SelectFieldMany}
              options={initialValues.saves}
              itemRef={'favourites'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'saved_at'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='Mentions' labelFor='mentions'>
          <Field
              name='mentions'
              id='mentions'
              component={SelectFieldMany}
              options={initialValues.mentions}
              itemRef={'mentions'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'mentioned_user'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='FaceDetections' labelFor='face_detections'>
          <Field
              name='face_detections'
              id='face_detections'
              component={SelectFieldMany}
              options={initialValues.face_detections}
              itemRef={'face_detections'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'confidence'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='WatermarkedDownloads' labelFor='watermarked_downloads'>
          <Field
              name='watermarked_downloads'
              id='watermarked_downloads'
              component={SelectFieldMany}
              options={initialValues.watermarked_downloads}
              itemRef={'download_jobs'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'status'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></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('/media_items/media_items-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditMedia_items
