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 { SelectField } from "../../components/SelectField";
import { SelectFieldMany } from "../../components/SelectFieldMany";
import { SwitchField } from '../../components/SwitchField'
import {RichTextField} from "../../components/RichTextField";

import { update, fetch } from '../../stores/room_types/room_typesSlice'
import { useAppDispatch, useAppSelector } from '../../stores/hooks'
import { useRouter } from 'next/router'

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

    property: null,

    'name': '',

    description: '',

    capacity: '',

    gender_policy: '',

    'amenities_json': '',

    'base_price': '',

    is_private: false,

    properties: null,

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

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

  const { room_typesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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

    <FormField label='Property' labelFor='property'>
        <Field
            name='property'
            id='property'
            component={SelectField}
            options={initialValues.property}
            itemRef={'properties'}

            showField={'name'}

        ></Field>
    </FormField>

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

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

    <FormField
        label="Capacity"
    >
        <Field
            type="number"
            name="capacity"
            placeholder="Capacity"
        />
    </FormField>

    <FormField label="GenderPolicy" labelFor="gender_policy">
        <Field name="gender_policy" id="gender_policy" component="select">

            <option value="mixed">mixed</option>

            <option value="female">female</option>

            <option value="male">male</option>

        </Field>
    </FormField>

    <FormField
        label="AmenitiesJSON"
    >
        <Field
            name="amenities_json"
            placeholder="AmenitiesJSON"
        />
    </FormField>

    <FormField
        label="BasePrice"
    >
        <Field
            type="number"
            name="base_price"
            placeholder="BasePrice"
        />
    </FormField>

    <FormField label='IsPrivate' labelFor='is_private'>
        <Field
            name='is_private'
            id='is_private'
            component={SwitchField}
        ></Field>
    </FormField>

    <FormField label='properties' labelFor='properties'>
        <Field
            name='properties'
            id='properties'
            component={SelectField}
            options={initialValues.properties}
            itemRef={'properties'}

            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('/room_types/room_types-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

EditRoom_types.getLayout = function getLayout(page: ReactElement) {
  return (
      <LayoutAuthenticated>
          {page}
      </LayoutAuthenticated>
  )
}

export default EditRoom_types
