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/booking_discounts/booking_discountsSlice'
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 EditBooking_discounts = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    booking: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    promo_code: null,
    

    
    
    

    

    

    

    

    

    

    

    
    discount_type: '',
    

    

    

    

    
    
    
    'discount_value': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'discount_amount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'currency': '',
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { booking_discountsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Booking' labelFor='booking'>
        <Field
            name='booking'
            id='booking'
            component={SelectField}
            options={initialValues.booking}
            itemRef={'bookings'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'booking_code'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='PromoCode' labelFor='promo_code'>
        <Field
            name='promo_code'
            id='promo_code'
            component={SelectField}
            options={initialValues.promo_code}
            itemRef={'promo_codes'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'code'}
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="DiscountType" labelFor="discount_type">
        <Field name="discount_type" id="discount_type" component="select">
          
            <option value="percent">percent</option>
          
            <option value="fixed_amount">fixed_amount</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DiscountValue"
    >
        <Field
            type="number"
            name="discount_value"
            placeholder="DiscountValue"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DiscountAmount"
    >
        <Field
            type="number"
            name="discount_amount"
            placeholder="DiscountAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Currency"
    >
        <Field
            name="currency"
            placeholder="Currency"
        />
    </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('/booking_discounts/booking_discounts-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditBooking_discounts
