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 JobLogPayPreview from '../../components/JobLogPayPreview'
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/job_logs/job_logsSlice'
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 EditJob_logsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    
    work_date: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    employee: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    customer: null,
    

    
    
    
    'hours_conducted': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'client_paid': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    workersCompClass: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    pay_type: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    vehicle: null,
    

    
    
    

    

    

    
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    

    

    

    

    

    

    

    

    

    
    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    
    notes_to_admin: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Customer' labelFor='customer'>
        <Field
            name='customer'
            id='customer'
            component={SelectField}
            options={initialValues.customer}
            itemRef={'customers'}
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  
    <FormField
        label="HoursConducted"
    >
        <Field
            type="number"
            name="hours_conducted"
            placeholder="HoursConducted"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ClientPaid"
    >
        <Field
            type="number"
            name="client_paid"
            placeholder="ClientPaid"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Workmans Comp Class" labelFor="workersCompClassId">
      <Field name="workersCompClass" id="workersCompClass" component={SelectField} options={initialValues.workersCompClass} itemRef={'workers_comp_classes'} showField={'name'}></Field>
  </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='PayType' labelFor='pay_type'>
        <Field
            name='pay_type'
            id='pay_type'
            component={SelectField}
            options={initialValues.pay_type}
            itemRef={'pay_types'}
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>

    <JobLogPayPreview />
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Vehicle' labelFor='vehicle'>
        <Field
            name='vehicle'
            id='vehicle'
            component={SelectField}
            options={initialValues.vehicle}
            itemRef={'vehicles'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="submitted">submitted</option>
          
            <option value="approved">approved</option>
          
            <option value="needs_correction">needs_correction</option>
          
            <option value="paid">paid</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

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

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

export default EditJob_logsPage
