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/map_edges/map_edgesSlice'
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 EditMap_edgesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    mind_map: null,
    

    
    
    
    'from_node_key': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'to_node_key': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    relation_type: '',
    

    

    

    

    
    
    

    

    
    reasoning: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    strength: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    ai_generated: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='MindMap' labelFor='mind_map'>
        <Field
            name='mind_map'
            id='mind_map'
            component={SelectField}
            options={initialValues.mind_map}
            itemRef={'mind_maps'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="FromNodeKey"
    >
        <Field
            name="from_node_key"
            placeholder="FromNodeKey"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ToNodeKey"
    >
        <Field
            name="to_node_key"
            placeholder="ToNodeKey"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="RelationType" labelFor="relation_type">
        <Field name="relation_type" id="relation_type" component="select">
          
            <option value="supports">supports</option>
          
            <option value="causes">causes</option>
          
            <option value="enables">enables</option>
          
            <option value="depends_on">depends_on</option>
          
            <option value="contradicts">contradicts</option>
          
            <option value="validates">validates</option>
          
            <option value="mitigates">mitigates</option>
          
            <option value="implements">implements</option>
          
            <option value="precedes">precedes</option>
          
            <option value="related_to">related_to</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  
    <FormField label='Reasoning' hasTextareaHeight>
        <Field
            name='reasoning'
            id='reasoning'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Strength" labelFor="strength">
        <Field name="strength" id="strength" component="select">
          
            <option value="weak">weak</option>
          
            <option value="medium">medium</option>
          
            <option value="strong">strong</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='AIGenerated' labelFor='ai_generated'>
        <Field
            name='ai_generated'
            id='ai_generated'
            component={SwitchField}
        ></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('/map_edges/map_edges-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditMap_edgesPage
