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/archive_items/archive_itemsSlice'
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 EditArchive_items = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    tenant: null,
    

    
    
    

    

    

    

    

    

    

    

    
    resource_type: '',
    

    

    

    

    
    
    
    'resource_reference': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    visibility_scope: '',
    

    

    

    

    
    
    
    'title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    keywords: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'amount_usd': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    
    archived_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    archived_by: null,
    

    
    
    

    

    

    

    

    

    

    

    

    
    files: [],
    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { archive_itemsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit archive_items')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit archive_items'} 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="Typeressource" labelFor="resource_type">
        <Field name="resource_type" id="resource_type" component="select">
          
            <option value="requisition">requisition</option>
          
            <option value="approval_step">approval_step</option>
          
            <option value="procurement_case">procurement_case</option>
          
            <option value="tender_event">tender_event</option>
          
            <option value="supplier">supplier</option>
          
            <option value="purchase_order">purchase_order</option>
          
            <option value="grn">grn</option>
          
            <option value="san">san</option>
          
            <option value="discrepancy">discrepancy</option>
          
            <option value="award">award</option>
          
            <option value="offer">offer</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Referenceressource"
    >
        <Field
            name="resource_reference"
            placeholder="Referenceressource"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Visibilite" labelFor="visibility_scope">
        <Field name="visibility_scope" id="visibility_scope" component="select">
          
            <option value="role">role</option>
          
            <option value="project">project</option>
          
            <option value="department">department</option>
          
            <option value="restricted">restricted</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Titrearchive"
    >
        <Field
            name="title"
            placeholder="Titrearchive"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Motscles" hasTextareaHeight>
        <Field name="keywords" as="textarea" placeholder="Motscles" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MontantUSD"
    >
        <Field
            type="number"
            name="amount_usd"
            placeholder="MontantUSD"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Statut" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="active">active</option>
          
            <option value="archived">archived</option>
          
            <option value="sealed">sealed</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='Fichiers'
            color='info'
            icon={mdiUpload}
            path={'archive_items/files'}
            name='files'
            id='files'
            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('/archive_items/archive_items-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditArchive_items
