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

    

    

    

    

    

    

    

    

    

    

    
    player_profile: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    npc: null,
    

    
    
    

    

    

    

    

    

    

    

    
    relationship_type: '',
    

    

    

    

    
    
    
    'friendship_points': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'trust_points': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    last_interaction_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_unlocked: false,
    

    

    

    

    

    

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

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

  const { npc_relationshipsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='PlayerProfile' labelFor='player_profile'>
        <Field
            name='player_profile'
            id='player_profile'
            component={SelectField}
            options={initialValues.player_profile}
            itemRef={'player_profiles'}
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'first_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='NPC' labelFor='npc'>
        <Field
            name='npc'
            id='npc'
            component={SelectField}
            options={initialValues.npc}
            itemRef={'npcs'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="RelationshipType" labelFor="relationship_type">
        <Field name="relationship_type" id="relationship_type" component="select">
          
            <option value="acquaintance">acquaintance</option>
          
            <option value="friend">friend</option>
          
            <option value="best_friend">best_friend</option>
          
            <option value="rival">rival</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="FriendshipPoints"
    >
        <Field
            type="number"
            name="friendship_points"
            placeholder="FriendshipPoints"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TrustPoints"
    >
        <Field
            type="number"
            name="trust_points"
            placeholder="TrustPoints"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

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

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

export default EditNpc_relationships
