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/deal_packages/deal_packagesSlice'
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 EditDeal_packages = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    project: null,
    

    
    
    
    'title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    deal_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    
    'target_price': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'down_payment': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'seller_note_amount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'interest_rate': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    term_months: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    
    structure_overview: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    risk_factors: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    next_steps: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    partner_targets: [],
    
    
    

    

    

    

    

    
    created_at_time: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { deal_packagesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Project' labelFor='project'>
        <Field
            name='project'
            id='project'
            component={SelectField}
            options={initialValues.project}
            itemRef={'projects'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="DealType" labelFor="deal_type">
        <Field name="deal_type" id="deal_type" component="select">
          
            <option value="vendor_finance">vendor_finance</option>
          
            <option value="management_buyout">management_buyout</option>
          
            <option value="asset_purchase">asset_purchase</option>
          
            <option value="share_purchase">share_purchase</option>
          
            <option value="joint_venture">joint_venture</option>
          
            <option value="strategic_partnership">strategic_partnership</option>
          
            <option value="licensing">licensing</option>
          
            <option value="distribution">distribution</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="draft">draft</option>
          
            <option value="in_review">in_review</option>
          
            <option value="ready">ready</option>
          
            <option value="sent">sent</option>
          
            <option value="archived">archived</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TargetPrice"
    >
        <Field
            type="number"
            name="target_price"
            placeholder="TargetPrice"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DownPayment"
    >
        <Field
            type="number"
            name="down_payment"
            placeholder="DownPayment"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="SellerNoteAmount"
    >
        <Field
            type="number"
            name="seller_note_amount"
            placeholder="SellerNoteAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="InterestRate"
    >
        <Field
            type="number"
            name="interest_rate"
            placeholder="InterestRate"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TermMonths"
    >
        <Field
            type="number"
            name="term_months"
            placeholder="TermMonths"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='StructureOverview' hasTextareaHeight>
        <Field
            name='structure_overview'
            id='structure_overview'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='RiskFactors' hasTextareaHeight>
        <Field
            name='risk_factors'
            id='risk_factors'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='NextSteps' hasTextareaHeight>
        <Field
            name='next_steps'
            id='next_steps'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

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

    

  



  

  

  

  

  

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

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

export default EditDeal_packages
