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

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

    property: null,

    room: null,

    due_date: new Date(),

    task_type: '',

    status: '',

    assignee_user: null,

    notes: '',

    properties: null,

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

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

  const { id } = router.query

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

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit housekeeping_tasks')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit housekeeping_tasks'} 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="DueDate"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.due_date ?
                  new Date(
                      dayjs(initialValues.due_date).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'due_date': date})}
          />
      </FormField>

    <FormField label="TaskType" labelFor="task_type">
        <Field name="task_type" id="task_type" component="select">

            <option value="turnover">turnover</option>

            <option value="deep_clean">deep_clean</option>

            <option value="linen">linen</option>

            <option value="inspection">inspection</option>

        </Field>
    </FormField>

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

            <option value="to_do">to_do</option>

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

            <option value="done">done</option>

        </Field>
    </FormField>

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

            showField={'firstName'}

        ></Field>
    </FormField>

    <FormField label="Notes" hasTextareaHeight>
        <Field name="notes" as="textarea" placeholder="Notes" />
    </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('/housekeeping_tasks/housekeeping_tasks-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditHousekeeping_tasksPage
