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/payments/paymentsSlice'
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 EditPaymentsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    contract: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    payer: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    payee: null,
    

    
    
    

    

    

    

    

    

    

    

    
    payment_type: '',
    

    

    

    

    
    
    
    'amount_gross': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'platform_fee_client_rate': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'platform_fee_freelancer_rate': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'platform_fee_amount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'amount_net': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    provider: '',
    

    

    

    

    
    
    
    'provider_reference': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    initiated_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    paid_at: new Date(),
    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Contract' labelFor='contract'>
        <Field
            name='contract'
            id='contract'
            component={SelectField}
            options={initialValues.contract}
            itemRef={'contracts'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'terms'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="PaymentType" labelFor="payment_type">
        <Field name="payment_type" id="payment_type" component="select">
          
            <option value="escrow_deposit">escrow_deposit</option>
          
            <option value="milestone_release">milestone_release</option>
          
            <option value="refund">refund</option>
          
            <option value="payout">payout</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="AmountGross"
    >
        <Field
            type="number"
            name="amount_gross"
            placeholder="AmountGross"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="PlatformFeeClientRate"
    >
        <Field
            type="number"
            name="platform_fee_client_rate"
            placeholder="PlatformFeeClientRate"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="PlatformFeeFreelancerRate"
    >
        <Field
            type="number"
            name="platform_fee_freelancer_rate"
            placeholder="PlatformFeeFreelancerRate"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="PlatformFeeAmount"
    >
        <Field
            type="number"
            name="platform_fee_amount"
            placeholder="PlatformFeeAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="AmountNet"
    >
        <Field
            type="number"
            name="amount_net"
            placeholder="AmountNet"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="pending">pending</option>
          
            <option value="paid">paid</option>
          
            <option value="failed">failed</option>
          
            <option value="refunded">refunded</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Provider" labelFor="provider">
        <Field name="provider" id="provider" component="select">
          
            <option value="simulated">simulated</option>
          
            <option value="stripe">stripe</option>
          
            <option value="paypal">paypal</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="ProviderReference"
    >
        <Field
            name="provider_reference"
            placeholder="ProviderReference"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

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

export default EditPaymentsPage
