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/attendance_logs/attendance_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";

import {hasPermission} from "../../helpers/userPermissions";



const EditAttendance_logs = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    employee: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    outlet: null,
    

    
    
    

    

    

    

    

    
    work_date: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    check_in_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    check_out_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'check_in_lat': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'check_in_lng': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'check_out_lat': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'check_out_lng': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    check_in_photo: [],
    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    check_out_photo: [],
    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    
    late_minutes: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    gps_valid: false,
    

    

    

    

    

    

    
    
    

    
    remarks: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { attendance_logsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit attendance_logs')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit attendance_logs'} 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={'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="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="CheckInTime"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.check_in_at ?
                  new Date(
                      dayjs(initialValues.check_in_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'check_in_at': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="CheckInLatitude"
    >
        <Field
            type="number"
            name="check_in_lat"
            placeholder="CheckInLatitude"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="CheckInLongitude"
    >
        <Field
            type="number"
            name="check_in_lng"
            placeholder="CheckInLongitude"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="CheckOutLatitude"
    >
        <Field
            type="number"
            name="check_out_lat"
            placeholder="CheckOutLatitude"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="CheckOutLongitude"
    >
        <Field
            type="number"
            name="check_out_lng"
            placeholder="CheckOutLongitude"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

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

  



  

  

  

  

  

  

  

  

  

  

  

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

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="hadir">hadir</option>
          
            <option value="telat">telat</option>
          
            <option value="tidak_hadir">tidak_hadir</option>
          
            <option value="sakit">sakit</option>
          
            <option value="izin">izin</option>
          
            <option value="dinas">dinas</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="LateMinutes"
    >
        <Field
            type="number"
            name="late_minutes"
            placeholder="LateMinutes"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='GPSValid' labelFor='gps_valid'>
        <Field
            name='gps_valid'
            id='gps_valid'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

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

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

export default EditAttendance_logs
