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/compliance_rule_overrides/compliance_rule_overridesSlice'
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 EditCompliance_rule_overrides = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    company: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    template: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    branch: null,
    

    
    
    
    'override_title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    frequency: '',
    

    

    

    

    
    
    

    

    

    
    due_day_of_month: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    due_month_of_year: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    grace_days: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    risk_level: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    is_active: false,
    

    

    

    

    

    

    
    
    

    

    
    reason: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { compliance_rule_overridesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Company' labelFor='company'>
        <Field
            name='company'
            id='company'
            component={SelectField}
            options={initialValues.company}
            itemRef={'companies'}
      
        
      
        
      
        
      
        
      
        
            showField={'company_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Template' labelFor='template'>
        <Field
            name='template'
            id='template'
            component={SelectField}
            options={initialValues.template}
            itemRef={'compliance_templates'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'template_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Branch' labelFor='branch'>
        <Field
            name='branch'
            id='branch'
            component={SelectField}
            options={initialValues.branch}
            itemRef={'branches'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'branch_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="OverrideTitle"
    >
        <Field
            name="override_title"
            placeholder="OverrideTitle"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Frequency" labelFor="frequency">
        <Field name="frequency" id="frequency" component="select">
          
            <option value="inherit">inherit</option>
          
            <option value="one_time">one_time</option>
          
            <option value="monthly">monthly</option>
          
            <option value="quarterly">quarterly</option>
          
            <option value="half_yearly">half_yearly</option>
          
            <option value="yearly">yearly</option>
          
            <option value="event_based">event_based</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DueDayOfMonth"
    >
        <Field
            type="number"
            name="due_day_of_month"
            placeholder="DueDayOfMonth"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DueMonthOfYear"
    >
        <Field
            type="number"
            name="due_month_of_year"
            placeholder="DueMonthOfYear"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="GraceDays"
    >
        <Field
            type="number"
            name="grace_days"
            placeholder="GraceDays"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="RiskLevel" labelFor="risk_level">
        <Field name="risk_level" id="risk_level" component="select">
          
            <option value="inherit">inherit</option>
          
            <option value="low">low</option>
          
            <option value="medium">medium</option>
          
            <option value="high">high</option>
          
            <option value="critical">critical</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Active' labelFor='is_active'>
        <Field
            name='is_active'
            id='is_active'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

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

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

export default EditCompliance_rule_overrides
