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/ai_models/ai_modelsSlice'
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 EditAi_models = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    model_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    
    'version': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'framework': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'artifact_uri': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'threshold': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    trained_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    activated_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    
    description: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { ai_modelsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ModelType" labelFor="model_type">
        <Field name="model_type" id="model_type" component="select">
          
            <option value="anomaly_detection">anomaly_detection</option>
          
            <option value="malware_detection">malware_detection</option>
          
            <option value="phishing_detection">phishing_detection</option>
          
            <option value="ransomware_detection">ransomware_detection</option>
          
            <option value="risk_scoring">risk_scoring</option>
          
            <option value="recommendation">recommendation</option>
          
            <option value="soc_agent_policy">soc_agent_policy</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="draft">draft</option>
          
            <option value="staging">staging</option>
          
            <option value="active">active</option>
          
            <option value="deprecated">deprecated</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Version"
    >
        <Field
            name="version"
            placeholder="Version"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Framework"
    >
        <Field
            name="framework"
            placeholder="Framework"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ArtifactURI"
    >
        <Field
            name="artifact_uri"
            placeholder="ArtifactURI"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Threshold"
    >
        <Field
            type="number"
            name="threshold"
            placeholder="Threshold"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

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

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

export default EditAi_models
