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

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    

    

    

    

    

    

    

    

    
    export_type: '',
    

    

    

    

    
    
    

    

    

    

    

    
    requested_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    completed_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    range_start: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    range_end: new Date(),
    

    

    

    

    

    

    

    
    
    

    
    included_sections: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    chief_complaint: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    questions_for_doctor: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    
    error_message: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { export_jobsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='User' labelFor='user'>
        <Field
            name='user'
            id='user'
            component={SelectField}
            options={initialValues.user}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ExportType" labelFor="export_type">
        <Field name="export_type" id="export_type" component="select">
          
            <option value="doctor_report_pdf">doctor_report_pdf</option>
          
            <option value="lab_results_pdf">lab_results_pdf</option>
          
            <option value="medication_card_pdf">medication_card_pdf</option>
          
            <option value="emergency_card_pdf">emergency_card_pdf</option>
          
            <option value="vaccination_record_pdf">vaccination_record_pdf</option>
          
            <option value="fitness_report_pdf">fitness_report_pdf</option>
          
            <option value="nutrition_report_pdf">nutrition_report_pdf</option>
          
            <option value="menstrual_cycle_report_pdf">menstrual_cycle_report_pdf</option>
          
            <option value="mental_health_report_pdf">mental_health_report_pdf</option>
          
            <option value="sleep_report_pdf">sleep_report_pdf</option>
          
            <option value="comprehensive_health_summary_pdf">comprehensive_health_summary_pdf</option>
          
            <option value="raw_backup_json">raw_backup_json</option>
          
            <option value="section_csv">section_csv</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  
    <FormField label="IncludedSections" hasTextareaHeight>
        <Field name="included_sections" as="textarea" placeholder="IncludedSections" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="ChiefComplaint" hasTextareaHeight>
        <Field name="chief_complaint" as="textarea" placeholder="ChiefComplaint" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="QuestionsforDoctor" hasTextareaHeight>
        <Field name="questions_for_doctor" as="textarea" placeholder="QuestionsforDoctor" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="queued">queued</option>
          
            <option value="running">running</option>
          
            <option value="completed">completed</option>
          
            <option value="failed">failed</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="ErrorMessage" hasTextareaHeight>
        <Field name="error_message" as="textarea" placeholder="ErrorMessage" />
    </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('/export_jobs/export_jobs-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditExport_jobs
