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/loyalty_transactions/loyalty_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 EditLoyalty_transactions = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    loyalty_account: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    payment: null,
    

    
    
    

    

    

    

    

    

    

    

    
    transaction_type: '',
    

    

    

    

    
    
    

    

    

    
    points: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    transaction_time: new Date(),
    

    

    

    

    

    

    

    
    
    

    
    description: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { loyalty_transactionsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Comptefidélité' labelFor='loyalty_account'>
        <Field
            name='loyalty_account'
            id='loyalty_account'
            component={SelectField}
            options={initialValues.loyalty_account}
            itemRef={'loyalty_accounts'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'points_balance'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Paiement' labelFor='payment'>
        <Field
            name='payment'
            id='payment'
            component={SelectField}
            options={initialValues.payment}
            itemRef={'payments'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'provider_reference'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Type" labelFor="transaction_type">
        <Field name="transaction_type" id="transaction_type" component="select">
          
            <option value="gain">gain</option>
          
            <option value="utilisation">utilisation</option>
          
            <option value="ajustement">ajustement</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Points"
    >
        <Field
            type="number"
            name="points"
            placeholder="Points"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

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

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

export default EditLoyalty_transactions
