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/build_artifacts/build_artifactsSlice'
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";

import {hasPermission} from "../../helpers/userPermissions";



const EditBuild_artifacts = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    build_job: null,
    

    
    
    

    

    

    

    

    

    

    

    
    artifact_type: '',
    

    

    

    

    
    
    
    'file_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    file_blob: [],
    

    

    

    
    
    
    'download_url': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'checksum_sha256': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    generated_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

  const { build_artifacts } = useAppSelector((state) => state.build_artifacts)
  
  const { currentUser } = useAppSelector((state) => state.auth);
  

  const { build_artifactsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='BuildJob' labelFor='build_job'>
        <Field
            name='build_job'
            id='build_job'
            component={SelectField}
            options={initialValues.build_job}
            itemRef={'build_jobs'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ArtifactType" labelFor="artifact_type">
        <Field name="artifact_type" id="artifact_type" component="select">
          
            <option value="apk">apk</option>
          
            <option value="aab">aab</option>
          
            <option value="zip">zip</option>
          
            <option value="log">log</option>
          
            <option value="report">report</option>
          
            <option value="preview_url">preview_url</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="FileName"
    >
        <Field
            name="file_name"
            placeholder="FileName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

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



  
    <FormField
        label="DownloadURL"
    >
        <Field
            name="download_url"
            placeholder="DownloadURL"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ChecksumSHA256"
    >
        <Field
            name="checksum_sha256"
            placeholder="ChecksumSHA256"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

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

export default EditBuild_artifacts
