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/bid_evaluations/bid_evaluationsSlice'
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 EditBid_evaluations = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    bid: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    lead_evaluator_user: null,
    

    
    
    
    'technical_score': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'financial_score': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'total_score': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    recommendation: '',
    

    

    

    

    
    
    

    
    justification: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    evaluated_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { bid_evaluationsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Bid' labelFor='bid'>
        <Field
            name='bid'
            id='bid'
            component={SelectField}
            options={initialValues.bid}
            itemRef={'bids'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'bid_reference'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  
    <FormField
        label="TechnicalScore"
    >
        <Field
            type="number"
            name="technical_score"
            placeholder="TechnicalScore"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="FinancialScore"
    >
        <Field
            type="number"
            name="financial_score"
            placeholder="FinancialScore"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TotalScore"
    >
        <Field
            type="number"
            name="total_score"
            placeholder="TotalScore"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Recommendation" labelFor="recommendation">
        <Field name="recommendation" id="recommendation" component="select">
          
            <option value="award">award</option>
          
            <option value="reserve">reserve</option>
          
            <option value="reject">reject</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="Justification" hasTextareaHeight>
        <Field name="justification" as="textarea" placeholder="Justification" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="EvaluatedAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.evaluated_at ?
                  new Date(
                      dayjs(initialValues.evaluated_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'evaluated_at': date})}
          />
      </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('/bid_evaluations/bid_evaluations-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditBid_evaluations
