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

    

    

    

    

    

    

    

    

    

    

    
    mind_map: null,
    

    
    
    
    'title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    node_type: '',
    

    

    

    

    
    
    

    

    
    content: '',
    

    

    

    

    

    

    

    

    

    

    
    
    
    'x_position': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'y_position': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    confidence_level: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    ai_generated: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    evidence_items: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { map_nodesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit map_nodes')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit map_nodes'} 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="Title"
    >
        <Field
            name="title"
            placeholder="Title"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="NodeType" labelFor="node_type">
        <Field name="node_type" id="node_type" component="select">
          
            <option value="idea">idea</option>
          
            <option value="problem">problem</option>
          
            <option value="solution">solution</option>
          
            <option value="customer_segment">customer_segment</option>
          
            <option value="value_proposition">value_proposition</option>
          
            <option value="channel">channel</option>
          
            <option value="revenue_stream">revenue_stream</option>
          
            <option value="cost_structure">cost_structure</option>
          
            <option value="key_activity">key_activity</option>
          
            <option value="key_resource">key_resource</option>
          
            <option value="key_partner">key_partner</option>
          
            <option value="competitor">competitor</option>
          
            <option value="assumption">assumption</option>
          
            <option value="risk">risk</option>
          
            <option value="metric">metric</option>
          
            <option value="milestone">milestone</option>
          
            <option value="task">task</option>
          
            <option value="note">note</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  
    <FormField label='Content' hasTextareaHeight>
        <Field
            name='content'
            id='content'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="XPosition"
    >
        <Field
            type="number"
            name="x_position"
            placeholder="XPosition"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="YPosition"
    >
        <Field
            type="number"
            name="y_position"
            placeholder="YPosition"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ConfidenceLevel" labelFor="confidence_level">
        <Field name="confidence_level" id="confidence_level" component="select">
          
            <option value="low">low</option>
          
            <option value="medium">medium</option>
          
            <option value="high">high</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='AIGenerated' labelFor='ai_generated'>
        <Field
            name='ai_generated'
            id='ai_generated'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='EvidenceItems' labelFor='evidence_items'>
          <Field
              name='evidence_items'
              id='evidence_items'
              component={SelectFieldMany}
              options={initialValues.evidence_items}
              itemRef={'node_evidence_items'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'relevance'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></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_nodes/map_nodes-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditMap_nodes
