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/bank_files/bank_filesSlice'
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 EditBank_files = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    payrollRun: null,
    

    
    
    

    

    

    

    

    

    

    

    
    bankCode: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    fileFormat: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    filePath: [],
    

    

    

    
    
    
    'totalAmount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    recordCount: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    generatedByUser: null,
    

    
    
    

    

    

    

    

    
    generatedAt: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    submittedAt: new Date(),
    

    

    

    

    

    

    

    
    
    
    'fileHash': '',
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { bank_filesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='PayrollRun' labelFor='payrollRun'>
        <Field
            name='payrollRun'
            id='payrollRun'
            component={SelectField}
            options={initialValues.payrollRun}
            itemRef={'payroll_runs'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'status'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="BankCode" labelFor="bankCode">
        <Field name="bankCode" id="bankCode" component="select">
          
            <option value="BDO">BDO</option>
          
            <option value="BPI">BPI</option>
          
            <option value="METROBANK">METROBANK</option>
          
            <option value="PNB">PNB</option>
          
            <option value="RCBC">RCBC</option>
          
            <option value="UNIONBANK">UNIONBANK</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="FileFormat" labelFor="fileFormat">
        <Field name="fileFormat" id="fileFormat" component="select">
          
            <option value="CSV">CSV</option>
          
            <option value="TXT">TXT</option>
          
            <option value="XLSX">XLSX</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

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



  

  

  

  
    <FormField
        label="TotalAmount"
    >
        <Field
            type="number"
            name="totalAmount"
            placeholder="TotalAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="RecordCount"
    >
        <Field
            type="number"
            name="recordCount"
            placeholder="RecordCount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



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

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

export default EditBank_files
