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/projects/projectsSlice'
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";

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


const ProjectsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { projects } = useAppSelector((state) => state.projects)
    
    const { currentUser } = useAppSelector((state) => state.auth);
    

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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Company</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{projects?.company?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Customer</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{projects?.customer?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Site</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{projects?.site?.label ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Assignedto</p>
                    
                    
                        <p>{projects?.assigned_to?.firstName ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Projectnumber</p>
                    <p>{projects?.project_number}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Title</p>
                    <p>{projects?.title}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Stage</p>
                    <p>{projects?.stage ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Priority</p>
                    <p>{projects?.priority ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Site_visits Project</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Scheduledstart</th>
                                
                                
                                
                                <th>Scheduledend</th>
                                
                                
                                
                                <th>Check-inat</th>
                                
                                
                                
                                <th>Check-outat</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {projects.site_visits_project && Array.isArray(projects.site_visits_project) &&
                              projects.site_visits_project.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/site_visits/site_visits-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="scheduled_start">
                                        { dataFormatter.dateTimeFormatter(item.scheduled_start) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="scheduled_end">
                                        { dataFormatter.dateTimeFormatter(item.scheduled_end) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="check_in_at">
                                        { dataFormatter.dateTimeFormatter(item.check_in_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="check_out_at">
                                        { dataFormatter.dateTimeFormatter(item.check_out_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!projects?.site_visits_project?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Photo_inspections Project</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Capturedat</th>
                                
                                
                                
                                
                                
                                <th>Caption</th>
                                
                                
                                
                                <th>Manualnotes</th>
                                
                                
                                
                                <th>AIstatus</th>
                                
                                
                                
                                <th>AIconfidence</th>
                                
                                
                                
                                <th>AIsummary</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {projects.photo_inspections_project && Array.isArray(projects.photo_inspections_project) &&
                              projects.photo_inspections_project.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/photo_inspections/photo_inspections-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="captured_at">
                                        { dataFormatter.dateTimeFormatter(item.captured_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="caption">
                                        { item.caption }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="manual_notes">
                                        { item.manual_notes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ai_status">
                                        { item.ai_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ai_confidence">
                                        { item.ai_confidence }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ai_summary">
                                        { item.ai_summary }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!projects?.photo_inspections_project?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Quotes Project</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Quotenumber</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Issuedat</th>
                                
                                
                                
                                <th>Validuntil</th>
                                
                                
                                
                                <th>Subtotal</th>
                                
                                
                                
                                <th>Taxtotal</th>
                                
                                
                                
                                <th>Total</th>
                                
                                
                                
                                <th>Paymentterms</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Requiressignature</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {projects.quotes_project && Array.isArray(projects.quotes_project) &&
                              projects.quotes_project.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/quotes/quotes-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="quote_number">
                                        { item.quote_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="issued_at">
                                        { dataFormatter.dateTimeFormatter(item.issued_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="valid_until">
                                        { dataFormatter.dateTimeFormatter(item.valid_until) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="subtotal">
                                        { item.subtotal }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tax_total">
                                        { item.tax_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="total">
                                        { item.total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="payment_terms">
                                        { item.payment_terms }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="requires_signature">
                                        { dataFormatter.booleanFormatter(item.requires_signature) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!projects?.quotes_project?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Material_lists Project</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Neededby</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {projects.material_lists_project && Array.isArray(projects.material_lists_project) &&
                              projects.material_lists_project.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/material_lists/material_lists-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="needed_by">
                                        { dataFormatter.dateTimeFormatter(item.needed_by) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!projects?.material_lists_project?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Tasks Project</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Title</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Tasktype</th>
                                
                                
                                
                                <th>Dueat</th>
                                
                                
                                
                                <th>Completedat</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {projects.tasks_project && Array.isArray(projects.tasks_project) &&
                              projects.tasks_project.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/tasks/tasks-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="task_type">
                                        { item.task_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="due_at">
                                        { dataFormatter.dateTimeFormatter(item.due_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="completed_at">
                                        { dataFormatter.dateTimeFormatter(item.completed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!projects?.tasks_project?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                

                <BaseDivider />

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

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

export default ProjectsView;