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/payroll_items/payroll_itemsSlice'
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 EditPayroll_itemsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    payroll_period: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    employee: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    outlet: null,
    

    
    
    

    

    

    
    workdays: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    sick_days: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    leave_days: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    absent_days: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    late_count: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    late_minutes_total: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'base_salary_monthly': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'daily_rate': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'base_salary_total': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'meal_transport_total': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'position_allowance_total': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'kpi_score': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'kpi_allowance_total': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'overtime_hours_total': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'overtime_rate': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'overtime_total': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'revenue_bonus_total': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'service_charge_total': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'deduction_total': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'take_home_pay': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    
    adjustment_notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='PayrollPeriod' labelFor='payroll_period'>
        <Field
            name='payroll_period'
            id='payroll_period'
            component={SelectField}
            options={initialValues.payroll_period}
            itemRef={'payroll_periods'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Outlet' labelFor='outlet'>
        <Field
            name='outlet'
            id='outlet'
            component={SelectField}
            options={initialValues.outlet}
            itemRef={'outlets'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  
    <FormField
        label="Workdays"
    >
        <Field
            type="number"
            name="workdays"
            placeholder="Workdays"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="SickDays"
    >
        <Field
            type="number"
            name="sick_days"
            placeholder="SickDays"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="LeaveDays"
    >
        <Field
            type="number"
            name="leave_days"
            placeholder="LeaveDays"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="AbsentDays"
    >
        <Field
            type="number"
            name="absent_days"
            placeholder="AbsentDays"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="LateCount"
    >
        <Field
            type="number"
            name="late_count"
            placeholder="LateCount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="LateMinutesTotal"
    >
        <Field
            type="number"
            name="late_minutes_total"
            placeholder="LateMinutesTotal"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="BaseSalary(Monthly)"
    >
        <Field
            type="number"
            name="base_salary_monthly"
            placeholder="BaseSalary(Monthly)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DailyRate"
    >
        <Field
            type="number"
            name="daily_rate"
            placeholder="DailyRate"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="BaseSalaryTotal"
    >
        <Field
            type="number"
            name="base_salary_total"
            placeholder="BaseSalaryTotal"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Meal+TransportTotal"
    >
        <Field
            type="number"
            name="meal_transport_total"
            placeholder="Meal+TransportTotal"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="PositionAllowanceTotal"
    >
        <Field
            type="number"
            name="position_allowance_total"
            placeholder="PositionAllowanceTotal"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="KPIScore"
    >
        <Field
            type="number"
            name="kpi_score"
            placeholder="KPIScore"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="KPIAllowanceTotal"
    >
        <Field
            type="number"
            name="kpi_allowance_total"
            placeholder="KPIAllowanceTotal"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="OvertimeHoursTotal"
    >
        <Field
            type="number"
            name="overtime_hours_total"
            placeholder="OvertimeHoursTotal"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="OvertimeRate"
    >
        <Field
            type="number"
            name="overtime_rate"
            placeholder="OvertimeRate"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="OvertimeTotal"
    >
        <Field
            type="number"
            name="overtime_total"
            placeholder="OvertimeTotal"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="RevenueBonusTotal"
    >
        <Field
            type="number"
            name="revenue_bonus_total"
            placeholder="RevenueBonusTotal"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ServiceChargeTotal"
    >
        <Field
            type="number"
            name="service_charge_total"
            placeholder="ServiceChargeTotal"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DeductionTotal"
    >
        <Field
            type="number"
            name="deduction_total"
            placeholder="DeductionTotal"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TakeHomePay"
    >
        <Field
            type="number"
            name="take_home_pay"
            placeholder="TakeHomePay"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="draft">draft</option>
          
            <option value="hr_reviewed">hr_reviewed</option>
          
            <option value="approved">approved</option>
          
            <option value="paid">paid</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

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

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

export default EditPayroll_itemsPage
