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/learning_signals/learning_signalsSlice'
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 EditLearning_signals = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    campaign: null,
    

    
    
    

    

    

    

    

    

    

    

    
    signal_type: '',
    

    

    

    

    
    
    

    

    

    

    

    
    signal_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'signal_title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    signal_details: '',
    

    

    

    

    

    

    

    

    

    

    
    
    
    'confidence_score': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    applied_to_brain: false,
    

    

    

    

    

    

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

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

  const { learning_signalsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Campaign' labelFor='campaign'>
        <Field
            name='campaign'
            id='campaign'
            component={SelectField}
            options={initialValues.campaign}
            itemRef={'campaigns'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'campaign_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="SignalType" labelFor="signal_type">
        <Field name="signal_type" id="signal_type" component="select">
          
            <option value="metric_import">metric_import</option>
          
            <option value="ab_result">ab_result</option>
          
            <option value="audience_comment">audience_comment</option>
          
            <option value="competitor_change">competitor_change</option>
          
            <option value="seasonal_pattern">seasonal_pattern</option>
          
            <option value="manual_insight">manual_insight</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  
    <FormField
        label="SignalTitle"
    >
        <Field
            name="signal_title"
            placeholder="SignalTitle"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='SignalDetails' hasTextareaHeight>
        <Field
            name='signal_details'
            id='signal_details'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ConfidenceScore"
    >
        <Field
            type="number"
            name="confidence_score"
            placeholder="ConfidenceScore"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='AppliedToBrain' labelFor='applied_to_brain'>
        <Field
            name='applied_to_brain'
            id='applied_to_brain'
            component={SwitchField}
        ></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('/learning_signals/learning_signals-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditLearning_signals
