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/leave_applications/leave_applicationsSlice'
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 EditLeave_applications = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    employee: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    leaveType: null,
    

    
    
    

    

    

    

    

    
    dateFrom: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    dateTo: new Date(),
    

    

    

    

    

    

    

    
    
    
    'totalDays': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    reason: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    
    managerNote: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    hrNote: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    approvedByUser: null,
    

    
    
    

    

    

    

    

    
    approvedAt: new Date(),
    

    

    

    

    

    

    

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

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

  const { leave_applicationsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='LeaveType' labelFor='leaveType'>
        <Field
            name='leaveType'
            id='leaveType'
            component={SelectField}
            options={initialValues.leaveType}
            itemRef={'leave_types'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TotalDays"
    >
        <Field
            type="number"
            name="totalDays"
            placeholder="TotalDays"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Reason" hasTextareaHeight>
        <Field name="reason" as="textarea" placeholder="Reason" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="APPLIED">APPLIED</option>
          
            <option value="PENDING_MANAGER">PENDING_MANAGER</option>
          
            <option value="PENDING_HR">PENDING_HR</option>
          
            <option value="APPROVED">APPROVED</option>
          
            <option value="REJECTED">REJECTED</option>
          
            <option value="CANCELLED">CANCELLED</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="ManagerNote" hasTextareaHeight>
        <Field name="managerNote" as="textarea" placeholder="ManagerNote" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="HRNote" hasTextareaHeight>
        <Field name="hrNote" as="textarea" placeholder="HRNote" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

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

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

export default EditLeave_applications
