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/return_items/return_itemsSlice'
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";

import {hasPermission} from "../../helpers/userPermissions";



const EditReturn_items = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    return: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    sale_item: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    product: null,
    

    
    
    
    'quantity': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'unit_price': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'vat_rate': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'vat_amount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'refund_amount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    condition_notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

  const { return_items } = useAppSelector((state) => state.return_items)
  
  const { currentUser } = useAppSelector((state) => state.auth);
  

  const { return_itemsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Return' labelFor='return'>
        <Field
            name='return'
            id='return'
            component={SelectField}
            options={initialValues.return}
            itemRef={'returns'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'return_number'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='OriginalSaleItem' labelFor='sale_item'>
        <Field
            name='sale_item'
            id='sale_item'
            component={SelectField}
            options={initialValues.sale_item}
            itemRef={'sale_items'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'product_name_snapshot'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Product' labelFor='product'>
        <Field
            name='product'
            id='product'
            component={SelectField}
            options={initialValues.product}
            itemRef={'products'}
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

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

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="UnitPrice"
    >
        <Field
            type="number"
            name="unit_price"
            placeholder="UnitPrice"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="VATRate"
    >
        <Field
            type="number"
            name="vat_rate"
            placeholder="VATRate"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="VATAmount"
    >
        <Field
            type="number"
            name="vat_amount"
            placeholder="VATAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="RefundAmount"
    >
        <Field
            type="number"
            name="refund_amount"
            placeholder="RefundAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="ConditionNotes" hasTextareaHeight>
        <Field name="condition_notes" as="textarea" placeholder="ConditionNotes" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='organizations' labelFor='organizations'>
        <Field
            name='organizations'
            id='organizations'
            component={SelectField}
            options={initialValues.organizations}
            itemRef={'organizations'}
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></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('/return_items/return_items-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditReturn_items
