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/license_activations/license_activationsSlice'
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 EditLicense_activationsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    license_key: null,
    

    
    
    

    

    

    

    

    
    activated_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'activated_by': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    client_info: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'activation_token': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    result: '',
    

    

    

    

    
    
    

    
    failure_reason: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

  useEffect(() => {
      if (typeof license_activations === 'object') {
          const newInitialVal = {...initVals};
          Object.keys(initVals).forEach(el => newInitialVal[el] = (license_activations)[el])
          setInitialValues(newInitialVal);
      }
  }, [license_activations])

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='LicenseKey' labelFor='license_key'>
        <Field
            name='license_key'
            id='license_key'
            component={SelectField}
            options={initialValues.license_key}
            itemRef={'license_keys'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'license_code'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  
    <FormField
        label="ActivatedBy"
    >
        <Field
            name="activated_by"
            placeholder="ActivatedBy"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="ClientInfo" hasTextareaHeight>
        <Field name="client_info" as="textarea" placeholder="ClientInfo" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ActivationToken"
    >
        <Field
            name="activation_token"
            placeholder="ActivationToken"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Result" labelFor="result">
        <Field name="result" id="result" component="select">
          
            <option value="success">success</option>
          
            <option value="failed">failed</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="FailureReason" hasTextareaHeight>
        <Field name="failure_reason" as="textarea" placeholder="FailureReason" />
    </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('/license_activations/license_activations-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditLicense_activationsPage
