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/profile_styles/profile_stylesSlice'
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 EditProfile_styles = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    owner_user: null,
    

    
    
    
    'style_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'username_color_hex': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'bio_font_family': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    panel_order_json: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    avatar_frame_images: [],
    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    banner_background_images: [],
    

    

    
    
    

    

    

    

    

    

    

    

    
    profile_layout: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    show_pronouns_badge: false,
    

    

    

    

    

    

    
    
    
    'pronouns_text': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    custom_css: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { profile_stylesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='OwnerUser' labelFor='owner_user'>
        <Field
            name='owner_user'
            id='owner_user'
            component={SelectField}
            options={initialValues.owner_user}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="StyleName"
    >
        <Field
            name="style_name"
            placeholder="StyleName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="UsernameColorHex"
    >
        <Field
            name="username_color_hex"
            placeholder="UsernameColorHex"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="BioFontFamily"
    >
        <Field
            name="bio_font_family"
            placeholder="BioFontFamily"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="PanelOrderJSON" hasTextareaHeight>
        <Field name="panel_order_json" as="textarea" placeholder="PanelOrderJSON" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    
        <FormField>
            <Field
                label='AvatarFrameImages'
                color='info'
                icon={mdiUpload}
                path={'profile_styles/avatar_frame_images'}
                name='avatar_frame_images'
                id='avatar_frame_images'
                schema={{
                    size: undefined,
                    formats: undefined,
                }}
                component={FormImagePicker}
            ></Field>
        </FormField>
    

  



  

  

  

  

  

  

  

  

  

  

  

    
        <FormField>
            <Field
                label='BannerBackgroundImages'
                color='info'
                icon={mdiUpload}
                path={'profile_styles/banner_background_images'}
                name='banner_background_images'
                id='banner_background_images'
                schema={{
                    size: undefined,
                    formats: undefined,
                }}
                component={FormImagePicker}
            ></Field>
        </FormField>
    

  



  

  

  

  

  

  

  

  
    <FormField label="ProfileLayout" labelFor="profile_layout">
        <Field name="profile_layout" id="profile_layout" component="select">
          
            <option value="classic">classic</option>
          
            <option value="modern">modern</option>
          
            <option value="minimal">minimal</option>
          
            <option value="streamer">streamer</option>
          
            <option value="storefront">storefront</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='ShowPronounsBadge' labelFor='show_pronouns_badge'>
        <Field
            name='show_pronouns_badge'
            id='show_pronouns_badge'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  
    <FormField
        label="PronounsText"
    >
        <Field
            name="pronouns_text"
            placeholder="PronounsText"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

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

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

export default EditProfile_styles
