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/titles/titlesSlice'
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 EditTitles = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    
    title_type: '',
    

    

    

    

    
    
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'original_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tagline': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    overview: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    release_date: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    first_air_date: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    last_air_date: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    content_rating: '',
    

    

    

    

    
    
    
    'original_language': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    runtime_minutes: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    number_of_seasons: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    number_of_episodes: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'average_rating': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    rating_count: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'popularity_score': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'imdb_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    poster_images: [],
    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    backdrop_images: [],
    

    

    
    
    

    

    

    

    

    

    
    is_trending: false,
    

    

    

    

    

    

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

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

  const { titlesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  
    <FormField label="Type" labelFor="title_type">
        <Field name="title_type" id="title_type" component="select">
          
            <option value="movie">movie</option>
          
            <option value="tv_show">tv_show</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Name"
    >
        <Field
            name="name"
            placeholder="Name"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="OriginalName"
    >
        <Field
            name="original_name"
            placeholder="OriginalName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Tagline"
    >
        <Field
            name="tagline"
            placeholder="Tagline"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='Overview' hasTextareaHeight>
        <Field
            name='overview'
            id='overview'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ContentRating" labelFor="content_rating">
        <Field name="content_rating" id="content_rating" component="select">
          
            <option value="g">g</option>
          
            <option value="pg">pg</option>
          
            <option value="pg_13">pg_13</option>
          
            <option value="r">r</option>
          
            <option value="nc_17">nc_17</option>
          
            <option value="tv_y">tv_y</option>
          
            <option value="tv_y7">tv_y7</option>
          
            <option value="tv_g">tv_g</option>
          
            <option value="tv_pg">tv_pg</option>
          
            <option value="tv_14">tv_14</option>
          
            <option value="tv_ma">tv_ma</option>
          
            <option value="unrated">unrated</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="OriginalLanguage"
    >
        <Field
            name="original_language"
            placeholder="OriginalLanguage"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="RuntimeMinutes"
    >
        <Field
            type="number"
            name="runtime_minutes"
            placeholder="RuntimeMinutes"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="NumberofSeasons"
    >
        <Field
            type="number"
            name="number_of_seasons"
            placeholder="NumberofSeasons"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="NumberofEpisodes"
    >
        <Field
            type="number"
            name="number_of_episodes"
            placeholder="NumberofEpisodes"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="AverageRating"
    >
        <Field
            type="number"
            name="average_rating"
            placeholder="AverageRating"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="RatingCount"
    >
        <Field
            type="number"
            name="rating_count"
            placeholder="RatingCount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="PopularityScore"
    >
        <Field
            type="number"
            name="popularity_score"
            placeholder="PopularityScore"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="IMDBCode"
    >
        <Field
            name="imdb_code"
            placeholder="IMDBCode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    
        <FormField>
            <Field
                label='PosterImages'
                color='info'
                icon={mdiUpload}
                path={'titles/poster_images'}
                name='poster_images'
                id='poster_images'
                schema={{
                    size: undefined,
                    formats: undefined,
                }}
                component={FormImagePicker}
            ></Field>
        </FormField>
    

  



  

  

  

  

  

  

  

  

  

  

  

    
        <FormField>
            <Field
                label='BackdropImages'
                color='info'
                icon={mdiUpload}
                path={'titles/backdrop_images'}
                name='backdrop_images'
                id='backdrop_images'
                schema={{
                    size: undefined,
                    formats: undefined,
                }}
                component={FormImagePicker}
            ></Field>
        </FormField>
    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Trending' labelFor='is_trending'>
        <Field
            name='is_trending'
            id='is_trending'
            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('/titles/titles-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditTitles
