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/payment_methods/payment_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 EditPayment_methods = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    
    provider: '',
    

    

    

    

    
    
    
    'display_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_enabled: false,
    

    

    

    

    

    

    
    
    

    

    
    instructions: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    qr_images: [],
    

    

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

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

  const { payment_methodsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  
    <FormField label="Provider" labelFor="provider">
        <Field name="provider" id="provider" component="select">
          
            <option value="transfermovil">transfermovil</option>
          
            <option value="enzona">enzona</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="DisplayName"
    >
        <Field
            name="display_name"
            placeholder="DisplayName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsEnabled' labelFor='is_enabled'>
        <Field
            name='is_enabled'
            id='is_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  
    <FormField label='PaymentInstructions' hasTextareaHeight>
        <Field
            name='instructions'
            id='instructions'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    
        <FormField>
            <Field
                label='QRImages'
                color='info'
                icon={mdiUpload}
                path={'payment_methods/qr_images'}
                name='qr_images'
                id='qr_images'
                schema={{
                    size: undefined,
                    formats: undefined,
                }}
                component={FormImagePicker}
            ></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('/payment_methods/payment_methods-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditPayment_methods
