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/code_repositories/code_repositoriesSlice'
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 Code_repositoriesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { code_repositories } = useAppSelector((state) => state.code_repositories)
    
    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 code_repositories')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View code_repositories')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/code_repositories/code_repositories-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>RepositoryName</p>
                    <p>{code_repositories?.name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>RepositoryType</p>
                    <p>{code_repositories?.repo_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>RemoteURL</p>
                    <p>{code_repositories?.remote_url}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>DefaultBranch</p>
                    <p>{code_repositories?.default_branch}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              
                <>
                    <p className={'block font-bold mb-2'}>Files</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                    <th>Path</th>
                                
                                
                                
                                    <th>Language</th>
                                
                                
                                
                                    <th>Content</th>
                                
                                
                                
                                    <th>ContentHash</th>
                                
                                
                                
                                    <th>LastModifiedAt</th>
                                
                                
                                
                                    <th>IsBinary</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {code_repositories.files && Array.isArray(code_repositories.files) &&
                              code_repositories.files.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/code_files/code_files-view/?id=${item.id}`)}>
                                  
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                  
                                    
                                    
                                    
                                    <td data-label="path">
                                        { item.path }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="language">
                                        { item.language }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="content">
                                        { item.content }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="content_hash">
                                        { item.content_hash }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_modified_at">
                                        { dataFormatter.dateTimeFormatter(item.last_modified_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_binary">
                                        { dataFormatter.booleanFormatter(item.is_binary) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!code_repositories?.files?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Code_files Repository</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>Path</th>
                                
                                
                                
                                <th>Language</th>
                                
                                
                                
                                <th>Content</th>
                                
                                
                                
                                <th>ContentHash</th>
                                
                                
                                
                                <th>LastModifiedAt</th>
                                
                                
                                
                                <th>IsBinary</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {code_repositories.code_files_repository && Array.isArray(code_repositories.code_files_repository) &&
                              code_repositories.code_files_repository.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/code_files/code_files-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="path">
                                        { item.path }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="language">
                                        { item.language }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="content">
                                        { item.content }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="content_hash">
                                        { item.content_hash }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_modified_at">
                                        { dataFormatter.dateTimeFormatter(item.last_modified_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_binary">
                                        { dataFormatter.booleanFormatter(item.is_binary) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!code_repositories?.code_files_repository?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Code_repositoriesView;