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/match_players/match_playersSlice'
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 EditMatch_playersPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    match: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    
    'display_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    avatar_customization: null,
    

    
    
    

    

    

    

    

    

    

    

    
    player_status: '',
    

    

    

    

    
    
    

    

    

    
    score_points: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    correct_count: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    incorrect_count: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    yux_earned: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'race_distance_km': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    joined_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    left_at: new Date(),
    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Match' labelFor='match'>
        <Field
            name='match'
            id='match'
            component={SelectField}
            options={initialValues.match}
            itemRef={'matches'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'game_code'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  
    <FormField
        label="DisplayName"
    >
        <Field
            name="display_name"
            placeholder="DisplayName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='AvatarCustomization' labelFor='avatar_customization'>
        <Field
            name='avatar_customization'
            id='avatar_customization'
            component={SelectField}
            options={initialValues.avatar_customization}
            itemRef={'avatar_customizations'}
      
        
      
        
      
        
      
        
      
        
            showField={'profile_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="PlayerStatus" labelFor="player_status">
        <Field name="player_status" id="player_status" component="select">
          
            <option value="joined">joined</option>
          
            <option value="ready">ready</option>
          
            <option value="playing">playing</option>
          
            <option value="disconnected">disconnected</option>
          
            <option value="finished">finished</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ScorePoints"
    >
        <Field
            type="number"
            name="score_points"
            placeholder="ScorePoints"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="CorrectCount"
    >
        <Field
            type="number"
            name="correct_count"
            placeholder="CorrectCount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="IncorrectCount"
    >
        <Field
            type="number"
            name="incorrect_count"
            placeholder="IncorrectCount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="YUXEarned"
    >
        <Field
            type="number"
            name="yux_earned"
            placeholder="YUXEarned"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="RaceDistance(km)"
    >
        <Field
            type="number"
            name="race_distance_km"
            placeholder="RaceDistance(km)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

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

export default EditMatch_playersPage
