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/lead_requests/lead_requestsSlice'
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 EditLead_requests = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    
    request_type: '',
    

    

    

    

    
    
    
    'full_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'company_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'email': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'phone': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    customer_type: '',
    

    

    

    

    
    
    

    
    message_ar: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    interested_stone: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    interested_service: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    related_project: null,
    

    
    
    

    

    

    

    

    

    

    

    

    
    reference_files: [],
    

    

    

    
    
    

    

    

    

    

    

    

    

    
    lead_status: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    assigned_to: null,
    

    
    
    

    

    

    

    

    
    preferred_contact_time: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    last_contacted_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    
    internal_notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { lead_requestsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  
    <FormField label="RequestType" labelFor="request_type">
        <Field name="request_type" id="request_type" component="select">
          
            <option value="quote">quote</option>
          
            <option value="callback">callback</option>
          
            <option value="information">information</option>
          
            <option value="appointment">appointment</option>
          
            <option value="custom_order">custom_order</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="CustomerType" labelFor="customer_type">
        <Field name="customer_type" id="customer_type" component="select">
          
            <option value="architect">architect</option>
          
            <option value="interior_designer">interior_designer</option>
          
            <option value="contractor">contractor</option>
          
            <option value="developer">developer</option>
          
            <option value="individual">individual</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="MessageArabic" hasTextareaHeight>
        <Field name="message_ar" as="textarea" placeholder="MessageArabic" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='InterestedStone' labelFor='interested_stone'>
        <Field
            name='interested_stone'
            id='interested_stone'
            component={SelectField}
            options={initialValues.interested_stone}
            itemRef={'stones'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name_ar'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='InterestedService' labelFor='interested_service'>
        <Field
            name='interested_service'
            id='interested_service'
            component={SelectField}
            options={initialValues.interested_service}
            itemRef={'services'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name_ar'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='RelatedProject' labelFor='related_project'>
        <Field
            name='related_project'
            id='related_project'
            component={SelectField}
            options={initialValues.related_project}
            itemRef={'projects'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name_ar'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

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



  

  

  

  

  

  

  

  
    <FormField label="LeadStatus" labelFor="lead_status">
        <Field name="lead_status" id="lead_status" component="select">
          
            <option value="new">new</option>
          
            <option value="qualified">qualified</option>
          
            <option value="in_progress">in_progress</option>
          
            <option value="quoted">quoted</option>
          
            <option value="won">won</option>
          
            <option value="lost">lost</option>
          
            <option value="closed">closed</option>
          
        </Field>
    </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>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  
    <FormField label="InternalNotes" hasTextareaHeight>
        <Field name="internal_notes" as="textarea" placeholder="InternalNotes" />
    </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('/lead_requests/lead_requests-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditLead_requests
