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

    

    

    

    

    

    

    

    

    

    

    
    merchant: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    tax_period: null,
    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    
    'gross_sales_amount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tot_rate_percent': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tot_payable_amount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    generated_by: null,
    

    
    
    

    

    

    

    

    
    generated_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    reviewed_by: null,
    

    
    
    

    

    

    

    

    
    reviewed_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    pdf_file: [],
    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    excel_file: [],
    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    revenue_office_export_file: [],
    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { id } = router.query

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

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

  useEffect(() => {
      if (typeof tot_declarations === 'object') {
          const newInitialVal = {...initVals};
          Object.keys(initVals).forEach(el => newInitialVal[el] = (tot_declarations)[el])
          setInitialValues(newInitialVal);
      }
  }, [tot_declarations])

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Merchant' labelFor='merchant'>
        <Field
            name='merchant'
            id='merchant'
            component={SelectField}
            options={initialValues.merchant}
            itemRef={'merchants'}
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'business_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='TaxPeriod' labelFor='tax_period'>
        <Field
            name='tax_period'
            id='tax_period'
            component={SelectField}
            options={initialValues.tax_period}
            itemRef={'tax_periods'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'status'}
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="draft">draft</option>
          
            <option value="generated">generated</option>
          
            <option value="reviewed">reviewed</option>
          
            <option value="submitted">submitted</option>
          
            <option value="accepted">accepted</option>
          
            <option value="rejected">rejected</option>
          
            <option value="amended">amended</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="GrossSalesAmount"
    >
        <Field
            type="number"
            name="gross_sales_amount"
            placeholder="GrossSalesAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TOTRatePercent"
    >
        <Field
            type="number"
            name="tot_rate_percent"
            placeholder="TOTRatePercent"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TOTPayableAmount"
    >
        <Field
            type="number"
            name="tot_payable_amount"
            placeholder="TOTPayableAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  
    <FormField label="Notes" hasTextareaHeight>
        <Field name="notes" as="textarea" placeholder="Notes" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='PDFFile'
            color='info'
            icon={mdiUpload}
            path={'tot_declarations/pdf_file'}
            name='pdf_file'
            id='pdf_file'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></Field>
        </FormField>
  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='ExcelFile'
            color='info'
            icon={mdiUpload}
            path={'tot_declarations/excel_file'}
            name='excel_file'
            id='excel_file'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></Field>
        </FormField>
  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='RevenueOfficeExportFile'
            color='info'
            icon={mdiUpload}
            path={'tot_declarations/revenue_office_export_file'}
            name='revenue_office_export_file'
            id='revenue_office_export_file'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></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('/tot_declarations/tot_declarations-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditTot_declarationsPage
