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

    

    

    

    

    

    

    

    

    

    

    
    facta_query: null,
    

    
    
    

    

    
    executive_summary: '',
    

    

    

    

    

    

    

    

    

    

    
    
    
    'truth_score_percent': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    verification_status: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    citations: [],
    
    
    

    

    
    ready_to_cite_text: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    neutral_perspective: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    pro_perspective: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    contra_perspective: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    generated_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='FactaQuery' labelFor='facta_query'>
        <Field
            name='facta_query'
            id='facta_query'
            component={SelectField}
            options={initialValues.facta_query}
            itemRef={'facta_queries'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'query_text'}
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  
    <FormField label='ExecutiveSummary' hasTextareaHeight>
        <Field
            name='executive_summary'
            id='executive_summary'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TruthScorePercent"
    >
        <Field
            type="number"
            name="truth_score_percent"
            placeholder="TruthScorePercent"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="VerificationStatus" labelFor="verification_status">
        <Field name="verification_status" id="verification_status" component="select">
          
            <option value="verified">verified</option>
          
            <option value="partially_verified">partially_verified</option>
          
            <option value="unverified">unverified</option>
          
            <option value="conflicting">conflicting</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='Citations' labelFor='citations'>
          <Field
              name='citations'
              id='citations'
              component={SelectFieldMany}
              options={initialValues.citations}
              itemRef={'citations'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'source_title'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  
    <FormField label='ReadyToCiteText' hasTextareaHeight>
        <Field
            name='ready_to_cite_text'
            id='ready_to_cite_text'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='NeutralPerspective' hasTextareaHeight>
        <Field
            name='neutral_perspective'
            id='neutral_perspective'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='ProPerspective' hasTextareaHeight>
        <Field
            name='pro_perspective'
            id='pro_perspective'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='ContraPerspective' hasTextareaHeight>
        <Field
            name='contra_perspective'
            id='contra_perspective'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="GeneratedAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.generated_at ?
                  new Date(
                      dayjs(initialValues.generated_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'generated_at': date})}
          />
      </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('/facta_answers/facta_answers-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditFacta_answersPage
