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_lines/service_linesSlice'
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 EditService_lines = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    tenant: null,
    

    
    
    
    'name_fr': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'name_en': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    description_fr: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    description_en: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    category: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    pricing_model: '',
    

    

    

    

    
    
    
    'min_price': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'max_price': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'typical_timeline': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    active: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { service_linesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit service_lines')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit service_lines'} 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="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="Category" labelFor="category">
        <Field name="category" id="category" component="select">
          
            <option value="ai_ml">ai_ml</option>
          
            <option value="cybersecurity">cybersecurity</option>
          
            <option value="cloud">cloud</option>
          
            <option value="hardware">hardware</option>
          
            <option value="web_mobile">web_mobile</option>
          
            <option value="database">database</option>
          
        </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="Typicaltimeline"
    >
        <Field
            name="typical_timeline"
            placeholder="Typicaltimeline"
        />
    </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('/service_lines/service_lines-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditService_lines
