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/organization_settings/organization_settingsSlice'
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 EditOrganization_settingsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    organization: null,
    

    
    
    

    

    

    

    

    

    

    

    
    default_retention_period: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    require_mfa_for_all_users: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    enable_global_search: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    enable_control_framework_library: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    date_format: '',
    

    

    

    

    
    
    
    'branding_primary_color': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    branding_logo: [],
    

    

    
    
    
    'notification_email_from_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'notification_email_from_address': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'slack_webhook_url': '',
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit organization_settings')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit organization_settings'} 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="DefaultRetentionPeriod" labelFor="default_retention_period">
        <Field name="default_retention_period" id="default_retention_period" component="select">
          
            <option value="30_days">30_days</option>
          
            <option value="90_days">90_days</option>
          
            <option value="180_days">180_days</option>
          
            <option value="1_year">1_year</option>
          
            <option value="3_years">3_years</option>
          
            <option value="7_years">7_years</option>
          
            <option value="indefinite">indefinite</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='RequireMFAForAllUsers' labelFor='require_mfa_for_all_users'>
        <Field
            name='require_mfa_for_all_users'
            id='require_mfa_for_all_users'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='EnableGlobalSearch' labelFor='enable_global_search'>
        <Field
            name='enable_global_search'
            id='enable_global_search'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='EnableControlFrameworkLibrary' labelFor='enable_control_framework_library'>
        <Field
            name='enable_control_framework_library'
            id='enable_control_framework_library'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="DateFormat" labelFor="date_format">
        <Field name="date_format" id="date_format" component="select">
          
            <option value="yyyy_mm_dd">yyyy_mm_dd</option>
          
            <option value="mm_dd_yyyy">mm_dd_yyyy</option>
          
            <option value="dd_mm_yyyy">dd_mm_yyyy</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="BrandingPrimaryColor"
    >
        <Field
            name="branding_primary_color"
            placeholder="BrandingPrimaryColor"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    
        <FormField>
            <Field
                label='BrandingLogo'
                color='info'
                icon={mdiUpload}
                path={'organization_settings/branding_logo'}
                name='branding_logo'
                id='branding_logo'
                schema={{
                    size: undefined,
                    formats: undefined,
                }}
                component={FormImagePicker}
            ></Field>
        </FormField>
    

  



  
    <FormField
        label="NotificationEmailFromName"
    >
        <Field
            name="notification_email_from_name"
            placeholder="NotificationEmailFromName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="NotificationEmailFromAddress"
    >
        <Field
            name="notification_email_from_address"
            placeholder="NotificationEmailFromAddress"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



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

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

export default EditOrganization_settingsPage
