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/shipping_methods/shipping_methodsSlice'
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 EditShipping_methods = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    carrier: null,
    

    
    
    
    'method_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'base_cost': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'free_shipping_threshold': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    service_level: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    active: false,
    

    

    

    

    

    

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

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

  const { shipping_methodsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Carrier' labelFor='carrier'>
        <Field
            name='carrier'
            id='carrier'
            component={SelectField}
            options={initialValues.carrier}
            itemRef={'shipping_carriers'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'carrier_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="MethodName"
    >
        <Field
            name="method_name"
            placeholder="MethodName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="BaseCost"
    >
        <Field
            type="number"
            name="base_cost"
            placeholder="BaseCost"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="FreeShippingThreshold"
    >
        <Field
            type="number"
            name="free_shipping_threshold"
            placeholder="FreeShippingThreshold"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ServiceLevel" labelFor="service_level">
        <Field name="service_level" id="service_level" component="select">
          
            <option value="same_day">same_day</option>
          
            <option value="next_day">next_day</option>
          
            <option value="standard">standard</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Active' labelFor='active'>
        <Field
            name='active'
            id='active'
            component={SwitchField}
        ></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('/shipping_methods/shipping_methods-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditShipping_methods
