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/default_activities_products/default_activities_productsSlice';
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 EditDefault_activities_products = () => {
  const router = useRouter();
  const dispatch = useAppDispatch();
  const initVals = {
    name: '',

    rate_type: '',

    standard_service: '',

    department: null,

    service_text: '',

    rate: '',

    service_id: '',

    service_code: '',

    unit_type: '',

    cost: '',

    qty: '',

    currency: '',

    rate_gbp: '',

    rate_eur: '',

    rate_usd: '',

    yes_fixed_text: false,

    no_fixed_text: false,
  };
  const [initialValues, setInitialValues] = useState(initVals);

  const { default_activities_products } = useAppSelector(
    (state) => state.default_activities_products,
  );

  const { default_activities_productsId } = router.query;

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

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

  useEffect(() => {
    if (typeof default_activities_products === 'object') {
      const newInitialVal = { ...initVals };

      Object.keys(initVals).forEach(
        (el) => (newInitialVal[el] = default_activities_products[el]),
      );

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit default_activities_products')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton
          icon={mdiChartTimelineVariant}
          title={'Edit default_activities_products'}
          main
        >
          {''}
        </SectionTitleLineWithButton>
        <CardBox>
          <Formik
            enableReinitialize
            initialValues={initialValues}
            onSubmit={(values) => handleSubmit(values)}
          >
            <Form>
              <FormField label='Name'>
                <Field name='name' placeholder='Name' />
              </FormField>

              <FormField label='Rate Type'>
                <Field name='rate_type' placeholder='Rate Type' />
              </FormField>

              <FormField label='Standard Service'>
                <Field name='standard_service' placeholder='Standard Service' />
              </FormField>

              <FormField label='Department' labelFor='department'>
                <Field
                  name='department'
                  id='department'
                  component={SelectField}
                  options={initialValues.department}
                  itemRef={'departments'}
                  showField={'id'}
                ></Field>
              </FormField>

              <FormField label='Service Text'>
                <Field name='service_text' placeholder='Service Text' />
              </FormField>

              <FormField label='Rate'>
                <Field type='number' name='rate' placeholder='Rate' />
              </FormField>

              <FormField label='Service Id'>
                <Field name='service_id' placeholder='Service Id' />
              </FormField>

              <FormField label='Service Code'>
                <Field name='service_code' placeholder='Service Code' />
              </FormField>

              <FormField label='Unit Type'>
                <Field name='unit_type' placeholder='Unit Type' />
              </FormField>

              <FormField label='Cost'>
                <Field type='number' name='cost' placeholder='Cost' />
              </FormField>

              <FormField label='Qty'>
                <Field type='number' name='qty' placeholder='Qty' />
              </FormField>

              <FormField label='Currency'>
                <Field name='currency' placeholder='Currency' />
              </FormField>

              <FormField label='Rate Gbp'>
                <Field type='number' name='rate_gbp' placeholder='Rate Gbp' />
              </FormField>

              <FormField label='Rate Eur'>
                <Field type='number' name='rate_eur' placeholder='Rate Eur' />
              </FormField>

              <FormField label='Rate Usd'>
                <Field type='number' name='rate_usd' placeholder='Rate Usd' />
              </FormField>

              <FormField label='Yes Fixed Text' labelFor='yes_fixed_text'>
                <Field
                  name='yes_fixed_text'
                  id='yes_fixed_text'
                  component={SwitchField}
                ></Field>
              </FormField>

              <FormField label='No Fixed Text' labelFor='no_fixed_text'>
                <Field
                  name='no_fixed_text'
                  id='no_fixed_text'
                  component={SwitchField}
                ></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(
                      '/default_activities_products/default_activities_products-list',
                    )
                  }
                />
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  );
};

EditDefault_activities_products.getLayout = function getLayout(
  page: ReactElement,
) {
  return (
    <LayoutAuthenticated permission={'UPDATE_DEFAULT_ACTIVITIES_PRODUCTS'}>
      {page}
    </LayoutAuthenticated>
  );
};

export default EditDefault_activities_products;
