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/business_plan_versions/business_plan_versionsSlice'
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 EditBusiness_plan_versions = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    project: null,
    

    
    
    
    'title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    audience: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    stage: '',
    

    

    

    

    
    
    

    

    
    executive_summary: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    full_plan: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    assumptions: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    exports: [],
    

    

    

    
    
    

    

    

    

    

    
    generated_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    generated_by_run: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { business_plan_versionsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit business_plan_versions')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit business_plan_versions'} 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="Audience" labelFor="audience">
        <Field name="audience" id="audience" component="select">
          
            <option value="investor_vc">investor_vc</option>
          
            <option value="bank_lender">bank_lender</option>
          
            <option value="strategic_partner">strategic_partner</option>
          
            <option value="seller_owner">seller_owner</option>
          
            <option value="internal">internal</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Stage" labelFor="stage">
        <Field name="stage" id="stage" component="select">
          
            <option value="draft">draft</option>
          
            <option value="review">review</option>
          
            <option value="final">final</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  
    <FormField label='ExecutiveSummary' hasTextareaHeight>
        <Field
            name='executive_summary'
            id='executive_summary'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='FullPlan' hasTextareaHeight>
        <Field
            name='full_plan'
            id='full_plan'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='Assumptions' hasTextareaHeight>
        <Field
            name='assumptions'
            id='assumptions'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='Exports'
            color='info'
            icon={mdiUpload}
            path={'business_plan_versions/exports'}
            name='exports'
            id='exports'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></Field>
        </FormField>
  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='GeneratedByRun' labelFor='generated_by_run'>
        <Field
            name='generated_by_run'
            id='generated_by_run'
            component={SelectField}
            options={initialValues.generated_by_run}
            itemRef={'ai_runs'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'run_type'}
        
      
        
      
        
      
        
      
        
      
        
      
        ></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('/business_plan_versions/business_plan_versions-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditBusiness_plan_versions
