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/dynamic_pricing_rules/dynamic_pricing_rulesSlice'
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 EditDynamic_pricing_rulesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    hotel: null,
    

    
    
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    room_type: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    rate_plan: null,
    

    
    
    

    

    

    

    

    

    

    

    
    rule_type: '',
    

    

    

    

    
    
    
    'adjustment_percent': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'adjustment_amount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    min_occupancy_percent: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    max_occupancy_percent: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    min_days_before: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    max_days_before: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'applies_days_of_week': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    active: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Hotel' labelFor='hotel'>
        <Field
            name='hotel'
            id='hotel'
            component={SelectField}
            options={initialValues.hotel}
            itemRef={'hotels'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='RoomType' labelFor='room_type'>
        <Field
            name='room_type'
            id='room_type'
            component={SelectField}
            options={initialValues.room_type}
            itemRef={'room_types'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='RatePlan' labelFor='rate_plan'>
        <Field
            name='rate_plan'
            id='rate_plan'
            component={SelectField}
            options={initialValues.rate_plan}
            itemRef={'rate_plans'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="RuleType" labelFor="rule_type">
        <Field name="rule_type" id="rule_type" component="select">
          
            <option value="occupancy_based">occupancy_based</option>
          
            <option value="days_before_arrival">days_before_arrival</option>
          
            <option value="day_of_week">day_of_week</option>
          
            <option value="length_of_stay">length_of_stay</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="AdjustmentPercent"
    >
        <Field
            type="number"
            name="adjustment_percent"
            placeholder="AdjustmentPercent"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="AdjustmentAmount"
    >
        <Field
            type="number"
            name="adjustment_amount"
            placeholder="AdjustmentAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MinimumOccupancyPercent"
    >
        <Field
            type="number"
            name="min_occupancy_percent"
            placeholder="MinimumOccupancyPercent"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MaximumOccupancyPercent"
    >
        <Field
            type="number"
            name="max_occupancy_percent"
            placeholder="MaximumOccupancyPercent"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MinimumDaysBeforeArrival"
    >
        <Field
            type="number"
            name="min_days_before"
            placeholder="MinimumDaysBeforeArrival"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MaximumDaysBeforeArrival"
    >
        <Field
            type="number"
            name="max_days_before"
            placeholder="MaximumDaysBeforeArrival"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="AppliesDaysOfWeek"
    >
        <Field
            name="applies_days_of_week"
            placeholder="AppliesDaysOfWeek"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Active' labelFor='active'>
        <Field
            name='active'
            id='active'
            component={SwitchField}
        ></Field>
    </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('/dynamic_pricing_rules/dynamic_pricing_rules-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditDynamic_pricing_rulesPage
