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/widget_catalog_items/widget_catalog_itemsSlice'
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 EditWidget_catalog_items = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'icon_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'author_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    widget_type: '',
    

    

    

    

    
    
    

    

    
    description: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_public: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_adult_only: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    bundle_files: [],
    

    

    

    
    
    

    
    schema_json: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { widget_catalog_itemsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


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

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="IconName"
    >
        <Field
            name="icon_name"
            placeholder="IconName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="AuthorName"
    >
        <Field
            name="author_name"
            placeholder="AuthorName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="WidgetType" labelFor="widget_type">
        <Field name="widget_type" id="widget_type" component="select">
          
            <option value="chat_box">chat_box</option>
          
            <option value="goal_meter">goal_meter</option>
          
            <option value="sub_bar">sub_bar</option>
          
            <option value="now_playing">now_playing</option>
          
            <option value="poll">poll</option>
          
            <option value="alert_box">alert_box</option>
          
            <option value="custom">custom</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  
    <FormField label='Description' hasTextareaHeight>
        <Field
            name='description'
            id='description'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsPublic' labelFor='is_public'>
        <Field
            name='is_public'
            id='is_public'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsAdultOnly' labelFor='is_adult_only'>
        <Field
            name='is_adult_only'
            id='is_adult_only'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='BundleFiles'
            color='info'
            icon={mdiUpload}
            path={'widget_catalog_items/bundle_files'}
            name='bundle_files'
            id='bundle_files'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></Field>
        </FormField>
  



  

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

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

export default EditWidget_catalog_items
