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/courier_profiles/courier_profilesSlice'
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 EditCourier_profiles = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    

    

    

    

    

    

    

    

    
    vehicle_type: '',
    

    

    

    

    
    
    
    'government_id_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    verification_files: [],
    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    profile_images: [],
    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    is_available: false,
    

    

    

    

    

    

    
    
    
    'rating_average': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    rating_count: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    current_address: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { courier_profilesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="VehicleType" labelFor="vehicle_type">
        <Field name="vehicle_type" id="vehicle_type" component="select">
          
            <option value="walking">walking</option>
          
            <option value="bicycle">bicycle</option>
          
            <option value="motorbike">motorbike</option>
          
            <option value="car">car</option>
          
            <option value="van">van</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="GovernmentIDNumber"
    >
        <Field
            name="government_id_number"
            placeholder="GovernmentIDNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='VerificationFiles'
            color='info'
            icon={mdiUpload}
            path={'courier_profiles/verification_files'}
            name='verification_files'
            id='verification_files'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></Field>
        </FormField>
  



  

  

  

  

  

  

  

  

  

  

  

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

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="pending">pending</option>
          
            <option value="active">active</option>
          
            <option value="suspended">suspended</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsAvailable' labelFor='is_available'>
        <Field
            name='is_available'
            id='is_available'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  
    <FormField
        label="RatingAverage"
    >
        <Field
            type="number"
            name="rating_average"
            placeholder="RatingAverage"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="RatingCount"
    >
        <Field
            type="number"
            name="rating_count"
            placeholder="RatingCount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='CurrentAddress' labelFor='current_address'>
        <Field
            name='current_address'
            id='current_address'
            component={SelectField}
            options={initialValues.current_address}
            itemRef={'addresses'}
      
        
      
        
      
        
      
        
      
        
            showField={'label'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></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('/courier_profiles/courier_profiles-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditCourier_profiles
