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/contacts/contactsSlice'
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 EditContactsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    organization: null,
    

    
    
    

    

    

    

    

    

    

    

    
    contact_type: '',
    

    

    

    

    
    
    
    'display_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'company_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'first_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'last_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'email': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'phone': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tin': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'vrn': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'billing_address_line1': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'billing_address_line2': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'billing_city': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'billing_state_region': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'billing_postal_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'billing_country': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'shipping_address_line1': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'shipping_address_line2': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'shipping_city': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'shipping_state_region': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'shipping_postal_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'shipping_country': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
    {hasPermission(currentUser, 'READ_ORGANIZATIONS') && 
    <FormField label='Organization' labelFor='organization'>
        <Field
            name='organization'
            id='organization'
            component={SelectField}
            options={initialValues.organization}
            itemRef={'organizations'}
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
     } 

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ContactType" labelFor="contact_type">
        <Field name="contact_type" id="contact_type" component="select">
          
            <option value="customer">customer</option>
          
            <option value="vendor">vendor</option>
          
            <option value="lead">lead</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="DisplayName"
    >
        <Field
            name="display_name"
            placeholder="DisplayName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="CompanyName"
    >
        <Field
            name="company_name"
            placeholder="CompanyName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="FirstName"
    >
        <Field
            name="first_name"
            placeholder="FirstName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="LastName"
    >
        <Field
            name="last_name"
            placeholder="LastName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Phone"
    >
        <Field
            name="phone"
            placeholder="Phone"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="TIN"
    >
        <Field
            name="tin"
            placeholder="TIN"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="VRN"
    >
        <Field
            name="vrn"
            placeholder="VRN"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="BillingAddressLine1"
    >
        <Field
            name="billing_address_line1"
            placeholder="BillingAddressLine1"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="BillingAddressLine2"
    >
        <Field
            name="billing_address_line2"
            placeholder="BillingAddressLine2"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="BillingCity"
    >
        <Field
            name="billing_city"
            placeholder="BillingCity"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="BillingStateRegion"
    >
        <Field
            name="billing_state_region"
            placeholder="BillingStateRegion"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="BillingPostalCode"
    >
        <Field
            name="billing_postal_code"
            placeholder="BillingPostalCode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="BillingCountry"
    >
        <Field
            name="billing_country"
            placeholder="BillingCountry"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ShippingAddressLine1"
    >
        <Field
            name="shipping_address_line1"
            placeholder="ShippingAddressLine1"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ShippingAddressLine2"
    >
        <Field
            name="shipping_address_line2"
            placeholder="ShippingAddressLine2"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ShippingCity"
    >
        <Field
            name="shipping_city"
            placeholder="ShippingCity"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ShippingStateRegion"
    >
        <Field
            name="shipping_state_region"
            placeholder="ShippingStateRegion"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ShippingPostalCode"
    >
        <Field
            name="shipping_postal_code"
            placeholder="ShippingPostalCode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ShippingCountry"
    >
        <Field
            name="shipping_country"
            placeholder="ShippingCountry"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="active">active</option>
          
            <option value="inactive">inactive</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  
    <FormField label='Notes' hasTextareaHeight>
        <Field
            name='notes'
            id='notes'
            component={RichTextField}
        ></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('/contacts/contacts-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditContactsPage
