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/subscription_plans/subscription_plansSlice'
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";

import {hasPermission} from "../../helpers/userPermissions";



const EditSubscription_plans = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'plan_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    plan_tier: '',
    

    

    

    

    
    
    
    'price_monthly': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'price_yearly': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    max_users: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    max_branches: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    max_documents: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    ai_assistant_enabled: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    whatsapp_alerts_enabled: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    custom_rules_enabled: false,
    

    

    

    

    

    

    
    
    

    

    
    features_note: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_active: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

  const { subscription_plans } = useAppSelector((state) => state.subscription_plans)
  
  const { currentUser } = useAppSelector((state) => state.auth);
  

  const { subscription_plansId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  
    <FormField
        label="PlanName"
    >
        <Field
            name="plan_name"
            placeholder="PlanName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="PlanTier" labelFor="plan_tier">
        <Field name="plan_tier" id="plan_tier" component="select">
          
            <option value="starter">starter</option>
          
            <option value="professional">professional</option>
          
            <option value="enterprise">enterprise</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="PriceMonthly"
    >
        <Field
            type="number"
            name="price_monthly"
            placeholder="PriceMonthly"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="PriceYearly"
    >
        <Field
            type="number"
            name="price_yearly"
            placeholder="PriceYearly"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MaxUsers"
    >
        <Field
            type="number"
            name="max_users"
            placeholder="MaxUsers"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MaxBranches"
    >
        <Field
            type="number"
            name="max_branches"
            placeholder="MaxBranches"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MaxDocuments"
    >
        <Field
            type="number"
            name="max_documents"
            placeholder="MaxDocuments"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='AIAssistantEnabled' labelFor='ai_assistant_enabled'>
        <Field
            name='ai_assistant_enabled'
            id='ai_assistant_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='WhatsAppAlertsEnabled' labelFor='whatsapp_alerts_enabled'>
        <Field
            name='whatsapp_alerts_enabled'
            id='whatsapp_alerts_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='CustomRulesEnabled' labelFor='custom_rules_enabled'>
        <Field
            name='custom_rules_enabled'
            id='custom_rules_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  
    <FormField label='FeaturesNote' hasTextareaHeight>
        <Field
            name='features_note'
            id='features_note'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Active' labelFor='is_active'>
        <Field
            name='is_active'
            id='is_active'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

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

export default EditSubscription_plans
