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

    

    

    

    

    

    

    

    

    

    

    
    firm: null,
    

    
    
    
    'business_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'legal_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tin_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'vat_registration_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    taxpayer_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    industry: '',
    

    

    

    

    
    
    
    'city': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    address: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'contact_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'contact_phone': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'contact_email': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    assigned_accountant: null,
    

    
    
    

    

    

    

    

    

    
    is_active: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    
    onboarded_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { merchantsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Firm' labelFor='firm'>
        <Field
            name='firm'
            id='firm'
            component={SelectField}
            options={initialValues.firm}
            itemRef={'firms'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="BusinessName"
    >
        <Field
            name="business_name"
            placeholder="BusinessName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="LegalName"
    >
        <Field
            name="legal_name"
            placeholder="LegalName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="TINNumber"
    >
        <Field
            name="tin_number"
            placeholder="TINNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="VATRegistrationNumber"
    >
        <Field
            name="vat_registration_number"
            placeholder="VATRegistrationNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="TaxpayerType" labelFor="taxpayer_type">
        <Field name="taxpayer_type" id="taxpayer_type" component="select">
          
            <option value="vat">vat</option>
          
            <option value="tot">tot</option>
          
            <option value="both">both</option>
          
            <option value="income_tax_only">income_tax_only</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Industry" labelFor="industry">
        <Field name="industry" id="industry" component="select">
          
            <option value="retail">retail</option>
          
            <option value="wholesale">wholesale</option>
          
            <option value="services">services</option>
          
            <option value="manufacturing">manufacturing</option>
          
            <option value="hospitality">hospitality</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="City"
    >
        <Field
            name="city"
            placeholder="City"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Address" hasTextareaHeight>
        <Field name="address" as="textarea" placeholder="Address" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ContactName"
    >
        <Field
            name="contact_name"
            placeholder="ContactName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ContactPhone"
    >
        <Field
            name="contact_phone"
            placeholder="ContactPhone"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ContactEmail"
    >
        <Field
            name="contact_email"
            placeholder="ContactEmail"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='AssignedAccountant' labelFor='assigned_accountant'>
        <Field
            name='assigned_accountant'
            id='assigned_accountant'
            component={SelectField}
            options={initialValues.assigned_accountant}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="OnboardedAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.onboarded_at ?
                  new Date(
                      dayjs(initialValues.onboarded_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'onboarded_at': date})}
          />
      </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('/merchants/merchants-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditMerchants
