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/places/placesSlice'
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 EditPlacesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    category: null,
    

    
    
    

    
    short_description: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    full_description: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    
    address: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'city': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'province': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'postal_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'latitude': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'longitude': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'phone_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'whatsapp_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'email': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'website_url': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'google_maps_url': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    photos: [],
    

    

    
    
    

    

    

    

    

    

    

    

    
    price_level: '',
    

    

    

    

    
    
    
    'average_price': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'rating_average': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    rating_count: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    is_verified: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    owner: null,
    

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

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

  const { id } = router.query

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

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

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

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

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


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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  
    <FormField label="ShortDescription" hasTextareaHeight>
        <Field name="short_description" as="textarea" placeholder="ShortDescription" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='FullDescription' hasTextareaHeight>
        <Field
            name='full_description'
            id='full_description'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Address" hasTextareaHeight>
        <Field name="address" as="textarea" placeholder="Address" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="City"
    >
        <Field
            name="city"
            placeholder="City"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Province"
    >
        <Field
            name="province"
            placeholder="Province"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="PostalCode"
    >
        <Field
            name="postal_code"
            placeholder="PostalCode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Latitude"
    >
        <Field
            type="number"
            name="latitude"
            placeholder="Latitude"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Longitude"
    >
        <Field
            type="number"
            name="longitude"
            placeholder="Longitude"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="PhoneNumber"
    >
        <Field
            name="phone_number"
            placeholder="PhoneNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="WhatsAppNumber"
    >
        <Field
            name="whatsapp_number"
            placeholder="WhatsAppNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Email"
    >
        <Field
            name="email"
            placeholder="Email"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="WebsiteURL"
    >
        <Field
            name="website_url"
            placeholder="WebsiteURL"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="GoogleMapsURL"
    >
        <Field
            name="google_maps_url"
            placeholder="GoogleMapsURL"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

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

  



  

  

  

  

  

  

  

  
    <FormField label="PriceLevel" labelFor="price_level">
        <Field name="price_level" id="price_level" component="select">
          
            <option value="budget">budget</option>
          
            <option value="midrange">midrange</option>
          
            <option value="premium">premium</option>
          
            <option value="unknown">unknown</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="AveragePrice"
    >
        <Field
            type="number"
            name="average_price"
            placeholder="AveragePrice"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="RatingAverage"
    >
        <Field
            type="number"
            name="rating_average"
            placeholder="RatingAverage"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="RatingCount"
    >
        <Field
            type="number"
            name="rating_count"
            placeholder="RatingCount"
        />
    </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="archived">archived</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Owner' labelFor='owner'>
        <Field
            name='owner'
            id='owner'
            component={SelectField}
            options={initialValues.owner}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></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('/places/places-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditPlacesPage
