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/conferences/conferencesSlice'
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 EditConferences = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    room: null,
    

    
    
    
    'conference_key': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    conference_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    conference_status: '',
    

    

    

    

    
    
    

    

    

    

    

    
    scheduled_start_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    scheduled_end_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    actual_start_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    actual_end_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    e2ee_enabled: false,
    

    

    

    

    

    

    
    
    

    

    

    
    participant_limit: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    region_preference: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { conferencesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Room' labelFor='room'>
        <Field
            name='room'
            id='room'
            component={SelectField}
            options={initialValues.room}
            itemRef={'rooms'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="ConferenceKey"
    >
        <Field
            name="conference_key"
            placeholder="ConferenceKey"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ConferenceType" labelFor="conference_type">
        <Field name="conference_type" id="conference_type" component="select">
          
            <option value="scheduled">scheduled</option>
          
            <option value="ad_hoc">ad_hoc</option>
          
            <option value="always_on">always_on</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ConferenceStatus" labelFor="conference_status">
        <Field name="conference_status" id="conference_status" component="select">
          
            <option value="created">created</option>
          
            <option value="running">running</option>
          
            <option value="ended">ended</option>
          
            <option value="failed">failed</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="ScheduledStartAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.scheduled_start_at ?
                  new Date(
                      dayjs(initialValues.scheduled_start_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'scheduled_start_at': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="ScheduledEndAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.scheduled_end_at ?
                  new Date(
                      dayjs(initialValues.scheduled_end_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'scheduled_end_at': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="ActualStartAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.actual_start_at ?
                  new Date(
                      dayjs(initialValues.actual_start_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'actual_start_at': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="ActualEndAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.actual_end_at ?
                  new Date(
                      dayjs(initialValues.actual_end_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'actual_end_at': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  
    <FormField
        label="ParticipantLimit"
    >
        <Field
            type="number"
            name="participant_limit"
            placeholder="ParticipantLimit"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="RegionPreference" labelFor="region_preference">
        <Field name="region_preference" id="region_preference" component="select">
          
            <option value="auto">auto</option>
          
            <option value="us">us</option>
          
            <option value="eu">eu</option>
          
            <option value="apac">apac</option>
          
            <option value="sa">sa</option>
          
            <option value="me">me</option>
          
        </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('/conferences/conferences-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditConferences
