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/scientific_articles/scientific_articlesSlice'
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 EditScientific_articles = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'title_text': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    abstract_text: '',
    

    

    

    

    

    

    

    

    

    

    
    
    
    'journal': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    published_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'doi': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'url': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    keywords: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    validation_status: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    validated_by_user: null,
    

    
    
    

    

    
    validation_notes: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    pdf_file: [],
    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    accounts: null,
    

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

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

  const { scientific_articlesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='Abstract' hasTextareaHeight>
        <Field
            name='abstract_text'
            id='abstract_text'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Journal"
    >
        <Field
            name="journal"
            placeholder="Journal"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  
    <FormField
        label="DOI"
    >
        <Field
            name="doi"
            placeholder="DOI"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="URL"
    >
        <Field
            name="url"
            placeholder="URL"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Keywords" hasTextareaHeight>
        <Field name="keywords" as="textarea" placeholder="Keywords" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Validationstatus" labelFor="validation_status">
        <Field name="validation_status" id="validation_status" component="select">
          
            <option value="pending">pending</option>
          
            <option value="validated">validated</option>
          
            <option value="rejected">rejected</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  
    <FormField label='Validationnotes' hasTextareaHeight>
        <Field
            name='validation_notes'
            id='validation_notes'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

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



  

  

  

  

  

  

  

  

  

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

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

export default EditScientific_articles
