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/services/servicesSlice'
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 EditServices = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    tenant: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    service_line: null,
    

    
    
    
    'name_fr': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'name_en': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    description_fr: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    description_en: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    deliverables: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    pricing_model: '',
    

    

    

    

    
    
    
    'min_price': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'max_price': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    typical_duration_weeks: '',
    

    

    

    

    

    

    

    

    

    
    
    

    
    required_skills: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    active: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { servicesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Tenant' labelFor='tenant'>
        <Field
            name='tenant'
            id='tenant'
            component={SelectField}
            options={initialValues.tenant}
            itemRef={'tenants'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Serviceline' labelFor='service_line'>
        <Field
            name='service_line'
            id='service_line'
            component={SelectField}
            options={initialValues.service_line}
            itemRef={'service_lines'}
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name_fr'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="Name(FR)"
    >
        <Field
            name="name_fr"
            placeholder="Name(FR)"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Name(EN)"
    >
        <Field
            name="name_en"
            placeholder="Name(EN)"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='Description(FR)' hasTextareaHeight>
        <Field
            name='description_fr'
            id='description_fr'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='Description(EN)' hasTextareaHeight>
        <Field
            name='description_en'
            id='description_en'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='Deliverables' hasTextareaHeight>
        <Field
            name='deliverables'
            id='deliverables'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Pricingmodel" labelFor="pricing_model">
        <Field name="pricing_model" id="pricing_model" component="select">
          
            <option value="project_fixed">project_fixed</option>
          
            <option value="time_material">time_material</option>
          
            <option value="recurring">recurring</option>
          
            <option value="hybrid">hybrid</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Minprice"
    >
        <Field
            type="number"
            name="min_price"
            placeholder="Minprice"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Maxprice"
    >
        <Field
            type="number"
            name="max_price"
            placeholder="Maxprice"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Typicalduration(weeks)"
    >
        <Field
            type="number"
            name="typical_duration_weeks"
            placeholder="Typicalduration(weeks)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Requiredskills(commaseparated)" hasTextareaHeight>
        <Field name="required_skills" as="textarea" placeholder="Requiredskills(commaseparated)" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Active' labelFor='active'>
        <Field
            name='active'
            id='active'
            component={SwitchField}
        ></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('/services/services-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditServices
