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/usage_limit_policies/usage_limit_policiesSlice'
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 EditUsage_limit_policies = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'policy_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    scope: '',
    

    

    

    

    
    
    

    

    

    
    daily_request_limit: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    monthly_request_limit: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'daily_cost_limit': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'monthly_cost_limit': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    hard_block_on_limit: false,
    

    

    

    

    

    

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

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

  const { usage_limit_policiesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  
    <FormField
        label="PolicyName"
    >
        <Field
            name="policy_name"
            placeholder="PolicyName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Scope" labelFor="scope">
        <Field name="scope" id="scope" component="select">
          
            <option value="free_user">free_user</option>
          
            <option value="member">member</option>
          
            <option value="admin">admin</option>
          
            <option value="custom">custom</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DailyRequestLimit"
    >
        <Field
            type="number"
            name="daily_request_limit"
            placeholder="DailyRequestLimit"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MonthlyRequestLimit"
    >
        <Field
            type="number"
            name="monthly_request_limit"
            placeholder="MonthlyRequestLimit"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DailyCostLimit"
    >
        <Field
            type="number"
            name="daily_cost_limit"
            placeholder="DailyCostLimit"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MonthlyCostLimit"
    >
        <Field
            type="number"
            name="monthly_cost_limit"
            placeholder="MonthlyCostLimit"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='HardBlockOnLimit' labelFor='hard_block_on_limit'>
        <Field
            name='hard_block_on_limit'
            id='hard_block_on_limit'
            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('/usage_limit_policies/usage_limit_policies-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditUsage_limit_policies
