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

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'product_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'brand': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    category: null,
    

    
    
    

    
    short_description: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    long_description: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    images: [],
    

    

    
    
    

    

    

    

    

    

    

    

    

    
    spec_sheet: [],
    

    

    

    
    
    
    'pack_size': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    units_per_case: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'unit_weight_lbs': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'case_weight_lbs': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'uom': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    moq_cases: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    temperature_zone: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    product_status: '',
    

    

    

    

    
    
    

    
    allergens: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    certifications: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_sample_eligible: false,
    

    

    

    

    

    

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

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

  const { productsId } = router.query

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

  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: productsId, 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="Productname"
    >
        <Field
            name="product_name"
            placeholder="Productname"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Brand"
    >
        <Field
            name="brand"
            placeholder="Brand"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Category' labelFor='category'>
        <Field
            name='category'
            id='category'
            component={SelectField}
            options={initialValues.category}
            itemRef={'product_categories'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'category_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  
    <FormField label="Shortdescription" hasTextareaHeight>
        <Field name="short_description" as="textarea" placeholder="Shortdescription" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='Longdescription' hasTextareaHeight>
        <Field
            name='long_description'
            id='long_description'
            component={RichTextField}
        ></Field>
    </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>
          <Field
            label='Specsheet'
            color='info'
            icon={mdiUpload}
            path={'products/spec_sheet'}
            name='spec_sheet'
            id='spec_sheet'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></Field>
        </FormField>
  



  
    <FormField
        label="Packsize"
    >
        <Field
            name="pack_size"
            placeholder="Packsize"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Unitspercase"
    >
        <Field
            type="number"
            name="units_per_case"
            placeholder="Unitspercase"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Unitweight(lbs)"
    >
        <Field
            type="number"
            name="unit_weight_lbs"
            placeholder="Unitweight(lbs)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Caseweight(lbs)"
    >
        <Field
            type="number"
            name="case_weight_lbs"
            placeholder="Caseweight(lbs)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Unitofmeasure"
    >
        <Field
            name="uom"
            placeholder="Unitofmeasure"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MOQ(cases)"
    >
        <Field
            type="number"
            name="moq_cases"
            placeholder="MOQ(cases)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Temperaturezone" labelFor="temperature_zone">
        <Field name="temperature_zone" id="temperature_zone" component="select">
          
            <option value="ambient">ambient</option>
          
            <option value="refrigerated">refrigerated</option>
          
            <option value="frozen">frozen</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Productstatus" labelFor="product_status">
        <Field name="product_status" id="product_status" component="select">
          
            <option value="active">active</option>
          
            <option value="discontinued">discontinued</option>
          
            <option value="seasonal">seasonal</option>
          
            <option value="out_of_stock">out_of_stock</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="Allergens" hasTextareaHeight>
        <Field name="allergens" as="textarea" placeholder="Allergens" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Certifications" hasTextareaHeight>
        <Field name="certifications" as="textarea" placeholder="Certifications" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Sampleeligible' labelFor='is_sample_eligible'>
        <Field
            name='is_sample_eligible'
            id='is_sample_eligible'
            component={SwitchField}
        ></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>
    </>
  )
}

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

export default EditProducts
