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/quality_processes/quality_processesSlice'
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 Quality_processesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { quality_processes } = useAppSelector((state) => state.quality_processes)
    

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

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ProcessType</p>
                    <p>{quality_processes?.process_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Headline</p>
                    <p>{quality_processes?.headline}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Description</p>
                  {quality_processes.description
                    ? <p dangerouslySetInnerHTML={{__html: quality_processes.description}}/>
                    : <p>No data</p>
                  }
                </div>
              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ProfileHighlight</p>
                    <p>{quality_processes?.profile_highlight}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

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

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsActive'>
                    <SwitchField
                      field={{name: 'is_active', value: quality_processes?.is_active}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Process_steps QualityProcess</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>StepNumber</th>
                                
                                
                                
                                <th>StepTitle</th>
                                
                                
                                
                                
                                
                                <th>IconName</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {quality_processes.process_steps_quality_process && Array.isArray(quality_processes.process_steps_quality_process) &&
                              quality_processes.process_steps_quality_process.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/process_steps/process_steps-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="step_number">
                                        { item.step_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="step_title">
                                        { item.step_title }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="icon_name">
                                        { item.icon_name }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!quality_processes?.process_steps_quality_process?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Quality_processesView;