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/community_reports/community_reportsSlice'
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 EditCommunity_reports = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    reporter: null,
    

    
    
    

    

    

    

    

    

    

    

    
    content_type: '',
    

    

    

    

    
    
    
    'content_id': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'reason': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    details: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    resolved_by: null,
    

    
    
    

    

    

    

    

    
    resolved_at: new Date(),
    

    

    

    

    

    

    

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

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

  const { community_reportsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ContentType" labelFor="content_type">
        <Field name="content_type" id="content_type" component="select">
          
            <option value="post">post</option>
          
            <option value="comment">comment</option>
          
            <option value="user_profile">user_profile</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="ContentID"
    >
        <Field
            name="content_id"
            placeholder="ContentID"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Reason"
    >
        <Field
            name="reason"
            placeholder="Reason"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Details" hasTextareaHeight>
        <Field name="details" as="textarea" placeholder="Details" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="open">open</option>
          
            <option value="reviewing">reviewing</option>
          
            <option value="resolved">resolved</option>
          
            <option value="dismissed">dismissed</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

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

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

export default EditCommunity_reports
