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/qa_checklist_items/qa_checklist_itemsSlice'
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 Qa_checklist_itemsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { qa_checklist_items } = useAppSelector((state) => state.qa_checklist_items)
    

    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 qa_checklist_items')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View qa_checklist_items')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/qa_checklist_items/qa_checklist_items-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>QAChecklist</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{qa_checklist_items?.qa_checklist?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>SequenceNumber</p>
                  <p>{qa_checklist_items?.sequence_number || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>CheckName</p>
                    <p>{qa_checklist_items?.check_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ValueType</p>
                    <p>{qa_checklist_items?.value_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>LowerLimit</p>
                  <p>{qa_checklist_items?.lower_limit || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>UpperLimit</p>
                  <p>{qa_checklist_items?.upper_limit || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Qa_measurements ChecklistItem</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Result</th>
                                
                                
                                
                                <th>NumericValue</th>
                                
                                
                                
                                <th>TextValue</th>
                                
                                
                                
                                <th>RecordedAt</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {qa_checklist_items.qa_measurements_qa_checklist_item && Array.isArray(qa_checklist_items.qa_measurements_qa_checklist_item) &&
                              qa_checklist_items.qa_measurements_qa_checklist_item.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/qa_measurements/qa_measurements-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="result">
                                        { item.result }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="numeric_value">
                                        { item.numeric_value }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="text_value">
                                        { item.text_value }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="recorded_at">
                                        { dataFormatter.dateTimeFormatter(item.recorded_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!qa_checklist_items?.qa_measurements_qa_checklist_item?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                

                <BaseDivider />

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

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

export default Qa_checklist_itemsView;