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/products/productsSlice'
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 EditProductsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'sku': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    category: null,
    

    
    
    

    

    

    

    

    

    

    

    
    product_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    unit: '',
    

    

    

    

    
    
    
    'sale_price': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'purchase_price': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'vat_rate': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    low_stock_threshold: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    track_stock: false,
    

    

    

    

    

    

    
    
    

    
    description: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    images: [],
    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  
    <FormField
        label="SKU"
    >
        <Field
            name="sku"
            placeholder="SKU"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Nomduproduit"
    >
        <Field
            name="name"
            placeholder="Nomduproduit"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Catégorie' labelFor='category'>
        <Field
            name='category'
            id='category'
            component={SelectField}
            options={initialValues.category}
            itemRef={'categories'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Typedeproduit" labelFor="product_type">
        <Field name="product_type" id="product_type" component="select">
          
            <option value="live_quail">live_quail</option>
          
            <option value="eggs">eggs</option>
          
            <option value="meat">meat</option>
          
            <option value="breeding_supply">breeding_supply</option>
          
            <option value="feed">feed</option>
          
            <option value="service">service</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Unité" labelFor="unit">
        <Field name="unit" id="unit" component="select">
          
            <option value="piece">piece</option>
          
            <option value="kg">kg</option>
          
            <option value="g">g</option>
          
            <option value="tray">tray</option>
          
            <option value="box">box</option>
          
            <option value="liter">liter</option>
          
            <option value="bag">bag</option>
          
            <option value="service">service</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Prixdevente"
    >
        <Field
            type="number"
            name="sale_price"
            placeholder="Prixdevente"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Prixd'achat"
    >
        <Field
            type="number"
            name="purchase_price"
            placeholder="Prixd'achat"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TVA(%)"
    >
        <Field
            type="number"
            name="vat_rate"
            placeholder="TVA(%)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Seuilstockbas"
    >
        <Field
            type="number"
            name="low_stock_threshold"
            placeholder="Seuilstockbas"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Suividestock' labelFor='track_stock'>
        <Field
            name='track_stock'
            id='track_stock'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  
    <FormField label="Description" hasTextareaHeight>
        <Field name="description" as="textarea" placeholder="Description" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    
        <FormField>
            <Field
                label='Images'
                color='info'
                icon={mdiUpload}
                path={'products/images'}
                name='images'
                id='images'
                schema={{
                    size: undefined,
                    formats: undefined,
                }}
                component={FormImagePicker}
            ></Field>
        </FormField>
    

  



  

  

  

  

  

  

  

  
    <FormField label="Statut" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="active">active</option>
          
            <option value="inactive">inactive</option>
          
        </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('/products/products-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditProductsPage
