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/creator_profiles/creator_profilesSlice'
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 EditCreator_profiles = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    
    'handle': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    creator_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    is_verified: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_adult_creator: false,
    

    

    

    

    

    

    
    
    
    'slogan': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    about: '',
    

    

    

    

    

    

    

    

    

    

    
    
    
    'business_email': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'support_email': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'preferred_language': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'category_tags': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    profile_style: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    tip_page: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    merch_store: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { creator_profilesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

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

  

  

    

  



  
    <FormField
        label="Handle"
    >
        <Field
            name="handle"
            placeholder="Handle"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="CreatorType" labelFor="creator_type">
        <Field name="creator_type" id="creator_type" component="select">
          
            <option value="streamer">streamer</option>
          
            <option value="youtuber">youtuber</option>
          
            <option value="vtuber">vtuber</option>
          
            <option value="music_creator">music_creator</option>
          
            <option value="artist">artist</option>
          
            <option value="developer">developer</option>
          
            <option value="adult_creator">adult_creator</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsVerified' labelFor='is_verified'>
        <Field
            name='is_verified'
            id='is_verified'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsAdultCreator' labelFor='is_adult_creator'>
        <Field
            name='is_adult_creator'
            id='is_adult_creator'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  
    <FormField
        label="Slogan"
    >
        <Field
            name="slogan"
            placeholder="Slogan"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='About' hasTextareaHeight>
        <Field
            name='about'
            id='about'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="BusinessEmail"
    >
        <Field
            name="business_email"
            placeholder="BusinessEmail"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="SupportEmail"
    >
        <Field
            name="support_email"
            placeholder="SupportEmail"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="PreferredLanguage"
    >
        <Field
            name="preferred_language"
            placeholder="PreferredLanguage"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="CategoryTags(Comma-separated)"
    >
        <Field
            name="category_tags"
            placeholder="CategoryTags(Comma-separated)"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='ProfileStyle' labelFor='profile_style'>
        <Field
            name='profile_style'
            id='profile_style'
            component={SelectField}
            options={initialValues.profile_style}
            itemRef={'profile_styles'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'style_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='TipPage' labelFor='tip_page'>
        <Field
            name='tip_page'
            id='tip_page'
            component={SelectField}
            options={initialValues.tip_page}
            itemRef={'tip_pages'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'slug'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='MerchStore' labelFor='merch_store'>
        <Field
            name='merch_store'
            id='merch_store'
            component={SelectField}
            options={initialValues.merch_store}
            itemRef={'merch_stores'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'store_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></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('/creator_profiles/creator_profiles-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditCreator_profiles
