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

    

    

    

    

    

    

    

    

    

    

    
    license_customer: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    product: null,
    

    
    
    
    'license_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    license_type: '',
    

    

    

    

    
    
    

    

    

    

    

    
    issued_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    expires_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    
    'hardware_fingerprint': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'server_fingerprint': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'db_fingerprint': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    allowed_clients: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    allow_updates: false,
    

    

    

    

    

    

    
    
    

    
    last_error_message: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { license_keysId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Customer' labelFor='license_customer'>
        <Field
            name='license_customer'
            id='license_customer'
            component={SelectField}
            options={initialValues.license_customer}
            itemRef={'license_customers'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'customer_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Product' labelFor='product'>
        <Field
            name='product'
            id='product'
            component={SelectField}
            options={initialValues.product}
            itemRef={'license_products'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'product_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="LicenseCode"
    >
        <Field
            name="license_code"
            placeholder="LicenseCode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="LicenseType" labelFor="license_type">
        <Field name="license_type" id="license_type" component="select">
          
            <option value="software_lock">software_lock</option>
          
            <option value="network_dongle">network_dongle</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="active">active</option>
          
            <option value="revoked">revoked</option>
          
            <option value="expired">expired</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="HardwareFingerprint"
    >
        <Field
            name="hardware_fingerprint"
            placeholder="HardwareFingerprint"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ServerFingerprint"
    >
        <Field
            name="server_fingerprint"
            placeholder="ServerFingerprint"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="DatabaseFingerprint"
    >
        <Field
            name="db_fingerprint"
            placeholder="DatabaseFingerprint"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="AllowedClients"
    >
        <Field
            type="number"
            name="allowed_clients"
            placeholder="AllowedClients"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='AllowUpdates' labelFor='allow_updates'>
        <Field
            name='allow_updates'
            id='allow_updates'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  
    <FormField label="LastErrorMessage" hasTextareaHeight>
        <Field name="last_error_message" as="textarea" placeholder="LastErrorMessage" />
    </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_keys/license_keys-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditLicense_keys
