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

    

    

    

    

    

    

    

    

    

    

    
    quest: null,
    

    
    
    

    

    

    

    

    

    

    

    
    objective_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    target_item: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    target_npc: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    target_location: null,
    

    
    
    

    

    

    
    target_quantity: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    sequence_order: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_optional: false,
    

    

    

    

    

    

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

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

  const { quest_objectivesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Quest' labelFor='quest'>
        <Field
            name='quest'
            id='quest'
            component={SelectField}
            options={initialValues.quest}
            itemRef={'quests'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ObjectiveType" labelFor="objective_type">
        <Field name="objective_type" id="objective_type" component="select">
          
            <option value="deliver_item">deliver_item</option>
          
            <option value="collect_item">collect_item</option>
          
            <option value="talk_to_npc">talk_to_npc</option>
          
            <option value="visit_location">visit_location</option>
          
            <option value="catch_fish">catch_fish</option>
          
            <option value="harvest_crop">harvest_crop</option>
          
            <option value="craft_item">craft_item</option>
          
            <option value="build_upgrade">build_upgrade</option>
          
            <option value="sell_item">sell_item</option>
          
            <option value="buy_item">buy_item</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='TargetItem' labelFor='target_item'>
        <Field
            name='target_item'
            id='target_item'
            component={SelectField}
            options={initialValues.target_item}
            itemRef={'items'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='TargetLocation' labelFor='target_location'>
        <Field
            name='target_location'
            id='target_location'
            component={SelectField}
            options={initialValues.target_location}
            itemRef={'locations'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  
    <FormField
        label="TargetQuantity"
    >
        <Field
            type="number"
            name="target_quantity"
            placeholder="TargetQuantity"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="SequenceOrder"
    >
        <Field
            type="number"
            name="sequence_order"
            placeholder="SequenceOrder"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsOptional' labelFor='is_optional'>
        <Field
            name='is_optional'
            id='is_optional'
            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('/quest_objectives/quest_objectives-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditQuest_objectives
