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/proof_packets/proof_packetsSlice'
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 EditProof_packets = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    organization: null,
    

    
    
    
    'packet_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    packet_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    framework: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    workflow: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    sampled_subject: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    owner_user: null,
    

    
    
    

    

    

    

    

    

    

    

    
    packet_status: '',
    

    

    

    

    
    
    
    'readiness_score': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    generated_on: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    due_date: new Date(),
    

    

    

    

    

    

    

    
    
    

    
    summary: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    artifacts: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { proof_packetsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit proof_packets')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit proof_packets'} 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="PacketName"
    >
        <Field
            name="packet_name"
            placeholder="PacketName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="PacketType" labelFor="packet_type">
        <Field name="packet_type" id="packet_type" component="select">
          
            <option value="offboarding">offboarding</option>
          
            <option value="privileged_access">privileged_access</option>
          
            <option value="mfa">mfa</option>
          
            <option value="access_review">access_review</option>
          
            <option value="service_account">service_account</option>
          
            <option value="exception">exception</option>
          
            <option value="quarterly_summary">quarterly_summary</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Framework' labelFor='framework'>
        <Field
            name='framework'
            id='framework'
            component={SelectField}
            options={initialValues.framework}
            itemRef={'control_frameworks'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'framework_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Workflow' labelFor='workflow'>
        <Field
            name='workflow'
            id='workflow'
            component={SelectField}
            options={initialValues.workflow}
            itemRef={'identity_workflows'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'workflow_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='SampledSubject' labelFor='sampled_subject'>
        <Field
            name='sampled_subject'
            id='sampled_subject'
            component={SelectField}
            options={initialValues.sampled_subject}
            itemRef={'sampled_subjects'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'display_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Owner' labelFor='owner_user'>
        <Field
            name='owner_user'
            id='owner_user'
            component={SelectField}
            options={initialValues.owner_user}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="PacketStatus" labelFor="packet_status">
        <Field name="packet_status" id="packet_status" component="select">
          
            <option value="draft">draft</option>
          
            <option value="in_review">in_review</option>
          
            <option value="ready">ready</option>
          
            <option value="delivered">delivered</option>
          
            <option value="archived">archived</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ReadinessScore"
    >
        <Field
            type="number"
            name="readiness_score"
            placeholder="ReadinessScore"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  
    <FormField label="Summary" hasTextareaHeight>
        <Field name="summary" as="textarea" placeholder="Summary" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='Artifacts' labelFor='artifacts'>
          <Field
              name='artifacts'
              id='artifacts'
              component={SelectFieldMany}
              options={initialValues.artifacts}
              itemRef={'artifacts'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'artifact_name'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='MappedControls' labelFor='mapped_controls'>
          <Field
              name='mapped_controls'
              id='mapped_controls'
              component={SelectFieldMany}
              options={initialValues.mapped_controls}
              itemRef={'control_requirements'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'control_code'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></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('/proof_packets/proof_packets-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditProof_packets
