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/workflow_rules/workflow_rulesSlice'
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 EditWorkflow_rules = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    tenant: null,
    

    
    
    
    'l1_threshold_usd': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'l2_threshold_usd': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'l3_threshold_usd': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'l4_threshold_usd': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    sla_approval_hours: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    sla_procurement_hours: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    sla_reception_hours: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    rate_limit_profile: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    require_reject_reason: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    require_return_comment: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { workflow_rulesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Tenant' labelFor='tenant'>
        <Field
            name='tenant'
            id='tenant'
            component={SelectField}
            options={initialValues.tenant}
            itemRef={'tenants'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  
    <FormField
        label="SeuilL1USD"
    >
        <Field
            type="number"
            name="l1_threshold_usd"
            placeholder="SeuilL1USD"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="SeuilL2USD"
    >
        <Field
            type="number"
            name="l2_threshold_usd"
            placeholder="SeuilL2USD"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="SeuilL3USD"
    >
        <Field
            type="number"
            name="l3_threshold_usd"
            placeholder="SeuilL3USD"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="SeuilL4USD"
    >
        <Field
            type="number"
            name="l4_threshold_usd"
            placeholder="SeuilL4USD"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="SLAapprobationheures"
    >
        <Field
            type="number"
            name="sla_approval_hours"
            placeholder="SLAapprobationheures"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="SLAachatsheures"
    >
        <Field
            type="number"
            name="sla_procurement_hours"
            placeholder="SLAachatsheures"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="SLAreceptionheures"
    >
        <Field
            type="number"
            name="sla_reception_hours"
            placeholder="SLAreceptionheures"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Profillimitation" labelFor="rate_limit_profile">
        <Field name="rate_limit_profile" id="rate_limit_profile" component="select">
          
            <option value="standard">standard</option>
          
            <option value="strict">strict</option>
          
            <option value="custom">custom</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Motifrejetobligatoire' labelFor='require_reject_reason'>
        <Field
            name='require_reject_reason'
            id='require_reject_reason'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Commentaireretourobligatoire' labelFor='require_return_comment'>
        <Field
            name='require_return_comment'
            id='require_return_comment'
            component={SwitchField}
        ></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('/workflow_rules/workflow_rules-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditWorkflow_rules
