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/wallet_transactions/wallet_transactionsSlice'
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 EditWallet_transactions = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    

    

    

    

    

    

    

    

    
    currency: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    direction: '',
    

    

    

    

    
    
    

    

    

    
    amount: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    balance_after: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    reason: '',
    

    

    

    

    
    
    
    'reference': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    occurred_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    
    note: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { wallet_transactionsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit wallet_transactions')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit wallet_transactions'} 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="Currency" labelFor="currency">
        <Field name="currency" id="currency" component="select">
          
            <option value="DOODLECOIN">DOODLECOIN</option>
          
            <option value="SUPERCHARGED">SUPERCHARGED</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Direction" labelFor="direction">
        <Field name="direction" id="direction" component="select">
          
            <option value="CREDIT">CREDIT</option>
          
            <option value="DEBIT">DEBIT</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Amount"
    >
        <Field
            type="number"
            name="amount"
            placeholder="Amount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="BalanceAfter"
    >
        <Field
            type="number"
            name="balance_after"
            placeholder="BalanceAfter"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Reason" labelFor="reason">
        <Field name="reason" id="reason" component="select">
          
            <option value="CODE_REDEMPTION">CODE_REDEMPTION</option>
          
            <option value="REFERRAL_REWARD">REFERRAL_REWARD</option>
          
            <option value="STREAK_REWARD">STREAK_REWARD</option>
          
            <option value="CONVERSION">CONVERSION</option>
          
            <option value="MEMBERSHIP_PURCHASE">MEMBERSHIP_PURCHASE</option>
          
            <option value="PRODUCT_PURCHASE">PRODUCT_PURCHASE</option>
          
            <option value="ITEM_REDEMPTION">ITEM_REDEMPTION</option>
          
            <option value="ADMIN_ADJUSTMENT">ADMIN_ADJUSTMENT</option>
          
            <option value="OTHER">OTHER</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Reference"
    >
        <Field
            name="reference"
            placeholder="Reference"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

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

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

export default EditWallet_transactions
