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/user_profiles/user_profilesSlice'
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 EditUser_profilesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    
    'cpf': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    birth_date: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    marital_status: '',
    

    

    

    

    
    
    
    'spouse_cpf': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'spouse_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'mother_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'cep': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'street': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'house_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'address_complement': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'neighborhood': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'city': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'state_uf': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'phone': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'occupation_text': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'occupation_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'cbo_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'main_source_cnpj': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'voter_title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'voter_zone': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'voter_section': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'nit_pis_pasep': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'caepf_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Usuário' labelFor='user'>
        <Field
            name='user'
            id='user'
            component={SelectField}
            options={initialValues.user}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="CPF"
    >
        <Field
            name="cpf"
            placeholder="CPF"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="Datadenascimento"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.birth_date ?
                  new Date(
                      dayjs(initialValues.birth_date).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'birth_date': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Estadocivil" labelFor="marital_status">
        <Field name="marital_status" id="marital_status" component="select">
          
            <option value="solteiro">solteiro</option>
          
            <option value="casado">casado</option>
          
            <option value="divorciado">divorciado</option>
          
            <option value="viuvo">viuvo</option>
          
            <option value="uniao_estavel">uniao_estavel</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="CPFdocônjuge"
    >
        <Field
            name="spouse_cpf"
            placeholder="CPFdocônjuge"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Nomedocônjuge"
    >
        <Field
            name="spouse_name"
            placeholder="Nomedocônjuge"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Nomedamãe"
    >
        <Field
            name="mother_name"
            placeholder="Nomedamãe"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="CEP"
    >
        <Field
            name="cep"
            placeholder="CEP"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Logradouro"
    >
        <Field
            name="street"
            placeholder="Logradouro"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Número"
    >
        <Field
            name="house_number"
            placeholder="Número"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Complemento"
    >
        <Field
            name="address_complement"
            placeholder="Complemento"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Bairro"
    >
        <Field
            name="neighborhood"
            placeholder="Bairro"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="UF"
    >
        <Field
            name="state_uf"
            placeholder="UF"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Ocupação"
    >
        <Field
            name="occupation_text"
            placeholder="Ocupação"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Naturezadaocupação(código)"
    >
        <Field
            name="occupation_code"
            placeholder="Naturezadaocupação(código)"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Ocupaçãoprincipal(CBO)"
    >
        <Field
            name="cbo_code"
            placeholder="Ocupaçãoprincipal(CBO)"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="CNPJdafonteprincipal"
    >
        <Field
            name="main_source_cnpj"
            placeholder="CNPJdafonteprincipal"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Títulodeeleitor"
    >
        <Field
            name="voter_title"
            placeholder="Títulodeeleitor"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Zonaeleitoral"
    >
        <Field
            name="voter_zone"
            placeholder="Zonaeleitoral"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Seçãoeleitoral"
    >
        <Field
            name="voter_section"
            placeholder="Seçãoeleitoral"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="NIT/PIS/PASEP"
    >
        <Field
            name="nit_pis_pasep"
            placeholder="NIT/PIS/PASEP"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="CAEPF"
    >
        <Field
            name="caepf_number"
            placeholder="CAEPF"
        />
    </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('/user_profiles/user_profiles-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditUser_profilesPage
