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/service_listings/service_listingsSlice'
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";



const EditService_listingsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    freelancer: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    category: null,
    

    
    
    
    'title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    description: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    pricing_model: '',
    

    

    

    

    
    
    
    'starting_price': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    delivery_days: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    gallery_images: [],
    

    

    
    
    

    

    

    

    

    

    

    

    

    
    sample_files: [],
    

    

    

    
    
    

    

    

    

    

    

    
    is_featured: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    tags: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

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

  const { service_listings } = useAppSelector((state) => state.service_listings)
  

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Category' labelFor='category'>
        <Field
            name='category'
            id='category'
            component={SelectField}
            options={initialValues.category}
            itemRef={'service_categories'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="Title"
    >
        <Field
            name="title"
            placeholder="Title"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

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

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="PricingModel" labelFor="pricing_model">
        <Field name="pricing_model" id="pricing_model" component="select">
          
            <option value="fixed">fixed</option>
          
            <option value="hourly">hourly</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="StartingPrice"
    >
        <Field
            type="number"
            name="starting_price"
            placeholder="StartingPrice"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DeliveryDays"
    >
        <Field
            type="number"
            name="delivery_days"
            placeholder="DeliveryDays"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="draft">draft</option>
          
            <option value="published">published</option>
          
            <option value="paused">paused</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

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

  



  

  

  

  

  

  

  

  

  

  

  

    

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



  

  

  

  

  

  

  

  

  
    <FormField label='IsFeatured' labelFor='is_featured'>
        <Field
            name='is_featured'
            id='is_featured'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='Tags' labelFor='tags'>
          <Field
              name='tags'
              id='tags'
              component={SelectFieldMany}
              options={initialValues.tags}
              itemRef={'tags'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'name'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='IncludedSkills' labelFor='included_skills'>
          <Field
              name='included_skills'
              id='included_skills'
              component={SelectFieldMany}
              options={initialValues.included_skills}
              itemRef={'skills'}
            
            
            
            
            
            
            
            
              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('/service_listings/service_listings-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditService_listingsPage
