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/inquiries/inquiriesSlice'
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 EditInquiriesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    
    inquiry_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    
    'full_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'email': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'phone': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'company_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'country': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    buyer_type: '',
    

    

    

    

    
    
    

    

    
    message: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    requested_region: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    requested_lot: null,
    

    
    
    
    'target_volume_kg': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    incoterm: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    preferred_contact_method: '',
    

    

    

    

    
    
    

    

    

    

    

    
    submitted_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    assigned_to: null,
    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  
    <FormField label="InquiryType" labelFor="inquiry_type">
        <Field name="inquiry_type" id="inquiry_type" component="select">
          
            <option value="contact_message">contact_message</option>
          
            <option value="request_samples">request_samples</option>
          
            <option value="get_a_quote">get_a_quote</option>
          
            <option value="partner_with_us">partner_with_us</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="new">new</option>
          
            <option value="in_progress">in_progress</option>
          
            <option value="replied">replied</option>
          
            <option value="qualified">qualified</option>
          
            <option value="closed">closed</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="FullName"
    >
        <Field
            name="full_name"
            placeholder="FullName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Email"
    >
        <Field
            name="email"
            placeholder="Email"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Phone"
    >
        <Field
            name="phone"
            placeholder="Phone"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="CompanyName"
    >
        <Field
            name="company_name"
            placeholder="CompanyName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Country"
    >
        <Field
            name="country"
            placeholder="Country"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="BuyerType" labelFor="buyer_type">
        <Field name="buyer_type" id="buyer_type" component="select">
          
            <option value="roaster">roaster</option>
          
            <option value="importer">importer</option>
          
            <option value="distributor">distributor</option>
          
            <option value="specialty_buyer">specialty_buyer</option>
          
            <option value="wholesaler">wholesaler</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  
    <FormField label='Message' hasTextareaHeight>
        <Field
            name='message'
            id='message'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='RequestedRegion' labelFor='requested_region'>
        <Field
            name='requested_region'
            id='requested_region'
            component={SelectField}
            options={initialValues.requested_region}
            itemRef={'coffee_regions'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'region_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='RequestedLot' labelFor='requested_lot'>
        <Field
            name='requested_lot'
            id='requested_lot'
            component={SelectField}
            options={initialValues.requested_lot}
            itemRef={'coffee_lots'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'lot_code'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  
    <FormField
        label="TargetVolume(kg)"
    >
        <Field
            type="number"
            name="target_volume_kg"
            placeholder="TargetVolume(kg)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Incoterm" labelFor="incoterm">
        <Field name="incoterm" id="incoterm" component="select">
          
            <option value="fob">fob</option>
          
            <option value="cif">cif</option>
          
            <option value="cfr">cfr</option>
          
            <option value="exw">exw</option>
          
            <option value="dap">dap</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="PreferredContactMethod" labelFor="preferred_contact_method">
        <Field name="preferred_contact_method" id="preferred_contact_method" component="select">
          
            <option value="email">email</option>
          
            <option value="phone">phone</option>
          
            <option value="whatsapp">whatsapp</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='AssignedTo' labelFor='assigned_to'>
        <Field
            name='assigned_to'
            id='assigned_to'
            component={SelectField}
            options={initialValues.assigned_to}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></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('/inquiries/inquiries-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditInquiriesPage
