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/vendor_compliance_documents/vendor_compliance_documentsSlice'
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 EditVendor_compliance_documents = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    vendor: null,
    

    
    
    

    

    

    

    

    

    

    

    
    document_type: '',
    

    

    

    

    
    
    
    'reference_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    issued_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    expires_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    
    review_comment: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    file: [],
    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { vendor_compliance_documentsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Vendor' labelFor='vendor'>
        <Field
            name='vendor'
            id='vendor'
            component={SelectField}
            options={initialValues.vendor}
            itemRef={'vendors'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="DocumentType" labelFor="document_type">
        <Field name="document_type" id="document_type" component="select">
          
            <option value="rccm">rccm</option>
          
            <option value="tax_clearance">tax_clearance</option>
          
            <option value="cnss">cnss</option>
          
            <option value="business_license">business_license</option>
          
            <option value="bank_letter">bank_letter</option>
          
            <option value="id">id</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="ReferenceNumber"
    >
        <Field
            name="reference_number"
            placeholder="ReferenceNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="valid">valid</option>
          
            <option value="expiring">expiring</option>
          
            <option value="expired">expired</option>
          
            <option value="rejected">rejected</option>
          
            <option value="pending_review">pending_review</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="ReviewComment" hasTextareaHeight>
        <Field name="review_comment" as="textarea" placeholder="ReviewComment" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='File'
            color='info'
            icon={mdiUpload}
            path={'vendor_compliance_documents/file'}
            name='file'
            id='file'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></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('/vendor_compliance_documents/vendor_compliance_documents-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditVendor_compliance_documents
