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/maintenance_tickets/maintenance_ticketsSlice'
import { useAppDispatch, useAppSelector } from '../../stores/hooks'
import { useRouter } from 'next/router'
import dataFormatter from '../../helpers/dataFormatter';

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

    property: null,

    room: null,

    bed: null,

    priority: '',

    status: '',

    'title': '',

    description: '',

    'photos_json': '',

    created_by_user: null,

    properties: null,

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

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

  const { id } = router.query

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

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit maintenance_tickets')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit maintenance_tickets'} 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='Room' labelFor='room'>
        <Field
            name='room'
            id='room'
            component={SelectField}
            options={initialValues.room}
            itemRef={'rooms'}

            showField={'name'}

        ></Field>
    </FormField>

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

            showField={'code'}

        ></Field>
    </FormField>

    <FormField label="Priority" labelFor="priority">
        <Field name="priority" id="priority" component="select">

            <option value="low">low</option>

            <option value="medium">medium</option>

            <option value="high">high</option>

            <option value="urgent">urgent</option>

        </Field>
    </FormField>

    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">

            <option value="open">open</option>

            <option value="in_progress">in_progress</option>

            <option value="resolved">resolved</option>

        </Field>
    </FormField>

    <FormField
        label="Title"
    >
        <Field
            name="title"
            placeholder="Title"
        />
    </FormField>

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

    <FormField
        label="PhotosJSON"
    >
        <Field
            name="photos_json"
            placeholder="PhotosJSON"
        />
    </FormField>

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

            showField={'firstName'}

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

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

export default EditMaintenance_ticketsPage
