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/rooms/roomsSlice'
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 EditRoomsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    team: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    conference_template: null,
    

    
    
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'room_key': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    room_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    access_level: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    e2ee_enabled: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    recording_enabled: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    transcription_enabled: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    canvas_enabled: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    chat_enabled: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_persistent: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    
    last_active_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Team' labelFor='team'>
        <Field
            name='team'
            id='team'
            component={SelectField}
            options={initialValues.team}
            itemRef={'teams'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='ConferenceTemplate' labelFor='conference_template'>
        <Field
            name='conference_template'
            id='conference_template'
            component={SelectField}
            options={initialValues.conference_template}
            itemRef={'conference_templates'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="RoomKey"
    >
        <Field
            name="room_key"
            placeholder="RoomKey"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="RoomType" labelFor="room_type">
        <Field name="room_type" id="room_type" component="select">
          
            <option value="meeting">meeting</option>
          
            <option value="huddle_always_on">huddle_always_on</option>
          
            <option value="classroom_course">classroom_course</option>
          
            <option value="breakout_parent">breakout_parent</option>
          
            <option value="breakout_child">breakout_child</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="AccessLevel" labelFor="access_level">
        <Field name="access_level" id="access_level" component="select">
          
            <option value="open">open</option>
          
            <option value="team_only">team_only</option>
          
            <option value="invite_only">invite_only</option>
          
            <option value="lobby_required">lobby_required</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='E2EEEnabled' labelFor='e2ee_enabled'>
        <Field
            name='e2ee_enabled'
            id='e2ee_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='RecordingEnabled' labelFor='recording_enabled'>
        <Field
            name='recording_enabled'
            id='recording_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='TranscriptionEnabled' labelFor='transcription_enabled'>
        <Field
            name='transcription_enabled'
            id='transcription_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='CanvasEnabled' labelFor='canvas_enabled'>
        <Field
            name='canvas_enabled'
            id='canvas_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='ChatEnabled' labelFor='chat_enabled'>
        <Field
            name='chat_enabled'
            id='chat_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsPersistent' labelFor='is_persistent'>
        <Field
            name='is_persistent'
            id='is_persistent'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="LastActiveAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.last_active_at ?
                  new Date(
                      dayjs(initialValues.last_active_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'last_active_at': date})}
          />
      </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('/rooms/rooms-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditRoomsPage
