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/pipeline_stages/pipeline_stagesSlice'
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";



const EditPipeline_stages = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    sort_order: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'win_probability': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_closed_won: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_closed_lost: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    pipeline: null,
    

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

  const { pipeline_stages } = useAppSelector((state) => state.pipeline_stages)
  

  const { pipeline_stagesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  
    <FormField
        label="StageName"
    >
        <Field
            name="name"
            placeholder="StageName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="SortOrder"
    >
        <Field
            type="number"
            name="sort_order"
            placeholder="SortOrder"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="WinProbability"
    >
        <Field
            type="number"
            name="win_probability"
            placeholder="WinProbability"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsClosedWon' labelFor='is_closed_won'>
        <Field
            name='is_closed_won'
            id='is_closed_won'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsClosedLost' labelFor='is_closed_lost'>
        <Field
            name='is_closed_lost'
            id='is_closed_lost'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Pipeline' labelFor='pipeline'>
        <Field
            name='pipeline'
            id='pipeline'
            component={SelectField}
            options={initialValues.pipeline}
            itemRef={'pipelines'}
      
        
      
        
      
        
      
        
      
        
      
        
            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('/pipeline_stages/pipeline_stages-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditPipeline_stages
