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/validation_rules/validation_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 EditValidation_rules = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    rule_set: null,
    

    
    
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    severity: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    rule_type: '',
    

    

    

    

    
    
    
    'target_field': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    condition_expression: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    parameters: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    message_template: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_active: false,
    

    

    

    

    

    

    
    
    

    

    

    
    order_index: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { validation_rulesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='RuleSet' labelFor='rule_set'>
        <Field
            name='rule_set'
            id='rule_set'
            component={SelectField}
            options={initialValues.rule_set}
            itemRef={'validation_rule_sets'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="RuleName"
    >
        <Field
            name="name"
            placeholder="RuleName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Severity" labelFor="severity">
        <Field name="severity" id="severity" component="select">
          
            <option value="error">error</option>
          
            <option value="warning">warning</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="RuleType" labelFor="rule_type">
        <Field name="rule_type" id="rule_type" component="select">
          
            <option value="required">required</option>
          
            <option value="format">format</option>
          
            <option value="range">range</option>
          
            <option value="enum_in">enum_in</option>
          
            <option value="unique">unique</option>
          
            <option value="duplicate_in_file">duplicate_in_file</option>
          
            <option value="foreign_key">foreign_key</option>
          
            <option value="custom_expression">custom_expression</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="TargetField"
    >
        <Field
            name="target_field"
            placeholder="TargetField"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="ConditionExpression" hasTextareaHeight>
        <Field name="condition_expression" as="textarea" placeholder="ConditionExpression" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Parameters(JSON)" hasTextareaHeight>
        <Field name="parameters" as="textarea" placeholder="Parameters(JSON)" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="MessageTemplate" hasTextareaHeight>
        <Field name="message_template" as="textarea" placeholder="MessageTemplate" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  
    <FormField
        label="OrderIndex"
    >
        <Field
            type="number"
            name="order_index"
            placeholder="OrderIndex"
        />
    </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('/validation_rules/validation_rules-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditValidation_rules
