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/trust_profiles/trust_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 EditTrust_profilesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    
    trust_score: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    trust_tier: '',
    

    

    

    

    
    
    

    

    

    

    

    
    score_last_calculated_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_human_verified: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_business_verified: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_identity_verified: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_tax_verified: false,
    

    

    

    

    

    

    
    
    

    
    risk_flags_summary: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  
    <FormField
        label="TrustScore"
    >
        <Field
            type="number"
            name="trust_score"
            placeholder="TrustScore"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="TrustTier" labelFor="trust_tier">
        <Field name="trust_tier" id="trust_tier" component="select">
          
            <option value="unverified">unverified</option>
          
            <option value="basic">basic</option>
          
            <option value="verified">verified</option>
          
            <option value="trusted">trusted</option>
          
            <option value="elite">elite</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='HumanVerified' labelFor='is_human_verified'>
        <Field
            name='is_human_verified'
            id='is_human_verified'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='BusinessVerified' labelFor='is_business_verified'>
        <Field
            name='is_business_verified'
            id='is_business_verified'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IdentityVerified' labelFor='is_identity_verified'>
        <Field
            name='is_identity_verified'
            id='is_identity_verified'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='TaxVerified' labelFor='is_tax_verified'>
        <Field
            name='is_tax_verified'
            id='is_tax_verified'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  
    <FormField label="RiskFlagsSummary" hasTextareaHeight>
        <Field name="risk_flags_summary" as="textarea" placeholder="RiskFlagsSummary" />
    </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('/trust_profiles/trust_profiles-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditTrust_profilesPage
