import React, { ReactElement, useEffect } from 'react';
import Head from 'next/head'
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import dayjs from "dayjs";
import {useAppDispatch, useAppSelector} from "../../stores/hooks";
import {useRouter} from "next/router";
import { fetch } from '../../stores/custom_artwork_requests/custom_artwork_requestsSlice'
import {saveFile} from "../../helpers/fileSaver";
import dataFormatter from '../../helpers/dataFormatter';
import ImageField from "../../components/ImageField";
import LayoutAuthenticated from "../../layouts/Authenticated";
import {getPageTitle} from "../../config";
import SectionTitleLineWithButton from "../../components/SectionTitleLineWithButton";
import SectionMain from "../../components/SectionMain";
import CardBox from "../../components/CardBox";
import BaseButton from "../../components/BaseButton";
import BaseDivider from "../../components/BaseDivider";
import {mdiChartTimelineVariant} from "@mdi/js";
import {SwitchField} from "../../components/SwitchField";
import FormField from "../../components/FormField";


const Custom_artwork_requestsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { custom_artwork_requests } = useAppSelector((state) => state.custom_artwork_requests)
    

    const { id } = router.query;
    
    function removeLastCharacter(str) {
      console.log(str,`str`)
      return str.slice(0, -1);
    }

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


    return (
      <>
          <Head>
              <title>{getPageTitle('View custom_artwork_requests')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View custom_artwork_requests')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/custom_artwork_requests/custom_artwork_requests-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>User</p>
                    
                    
                        <p>{custom_artwork_requests?.user?.firstName ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Status</p>
                    <p>{custom_artwork_requests?.status ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SourceImages</p>
                    {custom_artwork_requests?.source_images?.length
                      ? (
                        <ImageField
                          name={'source_images'}
                          image={custom_artwork_requests?.source_images}
                          className='w-20 h-20'
                        />
                      ) : <p>No SourceImages</p>
                    }
                </div>
              

              
              

              

              

              

              

              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PatternFiles</p>
                    {custom_artwork_requests?.pattern_files?.length
                      ? dataFormatter.filesFormatter(custom_artwork_requests.pattern_files).map(link => (
                        <button
                          key={link.publicUrl}
                          onClick={(e) => saveFile(e, link.publicUrl, link.name)}
                        >
                            {link.name}
                        </button>
                      )) : <p>No PatternFiles</p>
                    }
                </div>
              
              

              

              

              

              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PatternPreviewImages</p>
                    {custom_artwork_requests?.pattern_preview_images?.length
                      ? (
                        <ImageField
                          name={'pattern_preview_images'}
                          image={custom_artwork_requests?.pattern_preview_images}
                          className='w-20 h-20'
                        />
                      ) : <p>No PatternPreviewImages</p>
                    }
                </div>
              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SelectedSize</p>
                    <p>{custom_artwork_requests?.selected_size ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SelectedDifficulty</p>
                    <p>{custom_artwork_requests?.selected_difficulty ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>EstimatedColorCount</p>
                  <p>{custom_artwork_requests?.estimated_color_count || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>EstimatedPrice</p>
                  <p>{custom_artwork_requests?.estimated_price || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              
                <FormField label='Multi Text' hasTextareaHeight>
                  <textarea className={'w-full'} disabled value={custom_artwork_requests?.instructions} />
                </FormField>
              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>AIModelVersion</p>
                    <p>{custom_artwork_requests?.ai_model_version}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='SubmittedAt'>
                    {custom_artwork_requests.submitted_at ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={custom_artwork_requests.submitted_at ?
                        new Date(
                          dayjs(custom_artwork_requests.submitted_at).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No SubmittedAt</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='ProcessedAt'>
                    {custom_artwork_requests.processed_at ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={custom_artwork_requests.processed_at ?
                        new Date(
                          dayjs(custom_artwork_requests.processed_at).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No ProcessedAt</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='ApprovedAt'>
                    {custom_artwork_requests.approved_at ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={custom_artwork_requests.approved_at ?
                        new Date(
                          dayjs(custom_artwork_requests.approved_at).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No ApprovedAt</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Ai_generation_jobs Request</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>JobStatus</th>
                                
                                
                                
                                <th>Provider</th>
                                
                                
                                
                                <th>ProviderJobRef</th>
                                
                                
                                
                                <th>RuntimeSeconds</th>
                                
                                
                                
                                <th>ErrorMessage</th>
                                
                                
                                
                                <th>QueuedAt</th>
                                
                                
                                
                                <th>StartedAt</th>
                                
                                
                                
                                <th>FinishedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {custom_artwork_requests.ai_generation_jobs_request && Array.isArray(custom_artwork_requests.ai_generation_jobs_request) &&
                              custom_artwork_requests.ai_generation_jobs_request.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/ai_generation_jobs/ai_generation_jobs-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="job_status">
                                        { item.job_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="provider">
                                        { item.provider }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="provider_job_ref">
                                        { item.provider_job_ref }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="runtime_seconds">
                                        { item.runtime_seconds }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="error_message">
                                        { item.error_message }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="queued_at">
                                        { dataFormatter.dateTimeFormatter(item.queued_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="started_at">
                                        { dataFormatter.dateTimeFormatter(item.started_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="finished_at">
                                        { dataFormatter.dateTimeFormatter(item.finished_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!custom_artwork_requests?.ai_generation_jobs_request?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                

                <BaseDivider />

                <BaseButton
                    color='info'
                    label='Back'
                    onClick={() => router.push('/custom_artwork_requests/custom_artwork_requests-list')}
                />
              </CardBox>
          </SectionMain>
      </>
    );
};

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

export default Custom_artwork_requestsView;