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/cash_movements/cash_movementsSlice'
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 EditCash_movementsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    
    movement_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    direction: '',
    

    

    

    

    
    
    
    'amount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    method: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    source_type: '',
    

    

    

    

    
    
    
    'reference': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    payment: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    expense: null,
    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    recorded_by_user: null,
    

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

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

  const { id } = router.query

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

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

  useEffect(() => {
      if (typeof cash_movements === 'object') {
          const newInitialVal = {...initVals};
          Object.keys(initVals).forEach(el => newInitialVal[el] = (cash_movements)[el])
          setInitialValues(newInitialVal);
      }
  }, [cash_movements])

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

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


  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Sens" labelFor="direction">
        <Field name="direction" id="direction" component="select">
          
            <option value="in">in</option>
          
            <option value="out">out</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

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

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Méthode" labelFor="method">
        <Field name="method" id="method" component="select">
          
            <option value="cash">cash</option>
          
            <option value="bank_transfer">bank_transfer</option>
          
            <option value="orange_money">orange_money</option>
          
            <option value="wave">wave</option>
          
            <option value="cheque">cheque</option>
          
            <option value="card">card</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Origine" labelFor="source_type">
        <Field name="source_type" id="source_type" component="select">
          
            <option value="payment">payment</option>
          
            <option value="expense">expense</option>
          
            <option value="adjustment">adjustment</option>
          
            <option value="opening_balance">opening_balance</option>
          
            <option value="transfer">transfer</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Référence"
    >
        <Field
            name="reference"
            placeholder="Référence"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Dépenseliée' labelFor='expense'>
        <Field
            name='expense'
            id='expense'
            component={SelectField}
            options={initialValues.expense}
            itemRef={'expenses'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'reference'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

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

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Enregistrépar' labelFor='recorded_by_user'>
        <Field
            name='recorded_by_user'
            id='recorded_by_user'
            component={SelectField}
            options={initialValues.recorded_by_user}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></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('/cash_movements/cash_movements-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditCash_movementsPage
