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/employees/employeesSlice'
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 EditEmployees = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'employee_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'first_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'last_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'preferred_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'email': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'phone': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'national_id': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    date_of_birth: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    gender: '',
    

    

    

    

    
    
    

    
    address: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    hire_date: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    termination_date: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    employment_status: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    company: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    department: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    job_title: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    work_location: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    manager: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    user_account: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    
    profile_photo: [],
    

    

    
    
    

    

    

    

    

    

    

    

    
    employment_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { employeesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  
    <FormField
        label="EmployeeNumber"
    >
        <Field
            name="employee_number"
            placeholder="EmployeeNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="FirstName"
    >
        <Field
            name="first_name"
            placeholder="FirstName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="LastName"
    >
        <Field
            name="last_name"
            placeholder="LastName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="PreferredName"
    >
        <Field
            name="preferred_name"
            placeholder="PreferredName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="WorkEmail"
    >
        <Field
            name="email"
            placeholder="WorkEmail"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Phone"
    >
        <Field
            name="phone"
            placeholder="Phone"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="NationalID"
    >
        <Field
            name="national_id"
            placeholder="NationalID"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Gender" labelFor="gender">
        <Field name="gender" id="gender" component="select">
          
            <option value="male">male</option>
          
            <option value="female">female</option>
          
            <option value="non_binary">non_binary</option>
          
            <option value="prefer_not_to_say">prefer_not_to_say</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="Address" hasTextareaHeight>
        <Field name="address" as="textarea" placeholder="Address" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="EmploymentStatus" labelFor="employment_status">
        <Field name="employment_status" id="employment_status" component="select">
          
            <option value="active">active</option>
          
            <option value="probation">probation</option>
          
            <option value="on_leave">on_leave</option>
          
            <option value="terminated">terminated</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Company' labelFor='company'>
        <Field
            name='company'
            id='company'
            component={SelectField}
            options={initialValues.company}
            itemRef={'companies'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Department' labelFor='department'>
        <Field
            name='department'
            id='department'
            component={SelectField}
            options={initialValues.department}
            itemRef={'departments'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='JobTitle' labelFor='job_title'>
        <Field
            name='job_title'
            id='job_title'
            component={SelectField}
            options={initialValues.job_title}
            itemRef={'job_titles'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='WorkLocation' labelFor='work_location'>
        <Field
            name='work_location'
            id='work_location'
            component={SelectField}
            options={initialValues.work_location}
            itemRef={'work_locations'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Manager' labelFor='manager'>
        <Field
            name='manager'
            id='manager'
            component={SelectField}
            options={initialValues.manager}
            itemRef={'employees'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'employee_number'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    
        <FormField>
            <Field
                label='ProfilePhoto'
                color='info'
                icon={mdiUpload}
                path={'employees/profile_photo'}
                name='profile_photo'
                id='profile_photo'
                schema={{
                    size: undefined,
                    formats: undefined,
                }}
                component={FormImagePicker}
            ></Field>
        </FormField>
    

  



  

  

  

  

  

  

  

  
    <FormField label="EmploymentType" labelFor="employment_type">
        <Field name="employment_type" id="employment_type" component="select">
          
            <option value="full_time">full_time</option>
          
            <option value="part_time">part_time</option>
          
            <option value="contractor">contractor</option>
          
            <option value="intern">intern</option>
          
        </Field>
    </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('/employees/employees-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditEmployees
