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/business_badges/business_badgesSlice'
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 EditBusiness_badges = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    business: null,
    

    
    
    

    

    

    

    

    

    

    

    
    badge_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    
    granted_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    revoked_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    created_at_ts: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    updated_at_ts: new Date(),
    

    

    

    

    

    

    

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

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

  const { business_badgesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Business' labelFor='business'>
        <Field
            name='business'
            id='business'
            component={SelectField}
            options={initialValues.business}
            itemRef={'businesses'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="BadgeType" labelFor="badge_type">
        <Field name="badge_type" id="badge_type" component="select">
          
            <option value="VERIFIED_BUSINESS">VERIFIED_BUSINESS</option>
          
            <option value="VERIFIED_LICENSE">VERIFIED_LICENSE</option>
          
            <option value="VERIFIED_INSURANCE">VERIFIED_INSURANCE</option>
          
            <option value="VERIFIED_PRICING">VERIFIED_PRICING</option>
          
            <option value="FAST_RESPONDER">FAST_RESPONDER</option>
          
            <option value="VERIFIED_JOBS">VERIFIED_JOBS</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="PENDING">PENDING</option>
          
            <option value="APPROVED">APPROVED</option>
          
            <option value="REJECTED">REJECTED</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  
    <FormField label="Notes" hasTextareaHeight>
        <Field name="notes" as="textarea" placeholder="Notes" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

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

export default EditBusiness_badges
