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/tenants/tenantsSlice'
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 EditTenants = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'subdomain': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    
    'tagline': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'primary_color': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'secondary_color': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    logo: [],
    

    

    
    
    

    

    

    

    

    

    

    

    
    default_language: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    primary_currency: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    secondary_currency: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    sso_enabled: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    
    suspended_on: new Date(),
    

    

    

    

    

    

    

    
    
    
    'data_retention_policy': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { tenantsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  
    <FormField
        label="Nom"
    >
        <Field
            name="name"
            placeholder="Nom"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Sous-domaine"
    >
        <Field
            name="subdomain"
            placeholder="Sous-domaine"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Statut" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="active">active</option>
          
            <option value="suspended">suspended</option>
          
            <option value="pending_setup">pending_setup</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Slogan"
    >
        <Field
            name="tagline"
            placeholder="Slogan"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Couleurprimaire"
    >
        <Field
            name="primary_color"
            placeholder="Couleurprimaire"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Couleursecondaire"
    >
        <Field
            name="secondary_color"
            placeholder="Couleursecondaire"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    
        <FormField>
            <Field
                label='Logo'
                color='info'
                icon={mdiUpload}
                path={'tenants/logo'}
                name='logo'
                id='logo'
                schema={{
                    size: undefined,
                    formats: undefined,
                }}
                component={FormImagePicker}
            ></Field>
        </FormField>
    

  



  

  

  

  

  

  

  

  
    <FormField label="Languepardefaut" labelFor="default_language">
        <Field name="default_language" id="default_language" component="select">
          
            <option value="fr">fr</option>
          
            <option value="en">en</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Deviseprincipale" labelFor="primary_currency">
        <Field name="primary_currency" id="primary_currency" component="select">
          
            <option value="USD">USD</option>
          
            <option value="CDF">CDF</option>
          
            <option value="EUR">EUR</option>
          
            <option value="GBP">GBP</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Devisesecondaire" labelFor="secondary_currency">
        <Field name="secondary_currency" id="secondary_currency" component="select">
          
            <option value="none">none</option>
          
            <option value="USD">USD</option>
          
            <option value="CDF">CDF</option>
          
            <option value="EUR">EUR</option>
          
            <option value="GBP">GBP</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='SSOactive' labelFor='sso_enabled'>
        <Field
            name='sso_enabled'
            id='sso_enabled'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  
    <FormField
        label="Politiquederetention"
    >
        <Field
            name="data_retention_policy"
            placeholder="Politiquederetention"
        />
    </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('/tenants/tenants-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditTenants
