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/stock_movements/stock_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 EditStock_movementsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    stock_item: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    restaurant: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    performed_by_user: null,
    

    
    
    

    

    

    

    

    

    

    

    
    movement_type: '',
    

    

    

    

    
    
    
    'quantity': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    movement_time: new Date(),
    

    

    

    

    

    

    

    
    
    

    
    reason: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Articlestock' labelFor='stock_item'>
        <Field
            name='stock_item'
            id='stock_item'
            component={SelectField}
            options={initialValues.stock_item}
            itemRef={'stock_items'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Restaurant' labelFor='restaurant'>
        <Field
            name='restaurant'
            id='restaurant'
            component={SelectField}
            options={initialValues.restaurant}
            itemRef={'restaurants'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Effectuépar' labelFor='performed_by_user'>
        <Field
            name='performed_by_user'
            id='performed_by_user'
            component={SelectField}
            options={initialValues.performed_by_user}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Typemouvement" labelFor="movement_type">
        <Field name="movement_type" id="movement_type" component="select">
          
            <option value="entree">entree</option>
          
            <option value="sortie">sortie</option>
          
            <option value="ajustement">ajustement</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Quantité"
    >
        <Field
            type="number"
            name="quantity"
            placeholder="Quantité"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

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

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

export default EditStock_movementsPage
