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

              

              

              

              

              

              

              

              

              

              
                {hasPermission(currentUser, 'READ_ORGANIZATIONS') && 
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Organization</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{storage_buckets?.organization?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                  } 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Provider</p>
                    <p>{storage_buckets?.provider}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>BucketName</p>
                    <p>{storage_buckets?.bucket_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Region</p>
                    <p>{storage_buckets?.region}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>CDNDomain</p>
                    <p>{storage_buckets?.cdn_domain}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='DefaultBucket'>
                    <SwitchField
                      field={{name: 'default_bucket', value: storage_buckets?.default_bucket}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              
                <>
                    <p className={'block font-bold mb-2'}>StorageObjects</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                    <th>ObjectKey</th>
                                
                                
                                
                                    <th>ContentType</th>
                                
                                
                                
                                    <th>SizeBytes</th>
                                
                                
                                
                                    <th>ETag</th>
                                
                                
                                
                                    <th>SHA256</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                    <th>CompressionRatio</th>
                                
                                
                                
                                    <th>Status</th>
                                
                                
                                
                                    <th>TrashedAt</th>
                                
                                
                                
                                    <th>PurgeAfter</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {storage_buckets.storage_objects && Array.isArray(storage_buckets.storage_objects) &&
                              storage_buckets.storage_objects.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/storage_objects/storage_objects-view/?id=${item.id}`)}>
                                  
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                  
                                    
                                    
                                    
                                    <td data-label="object_key">
                                        { item.object_key }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="content_type">
                                        { item.content_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="size_bytes">
                                        { item.size_bytes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="etag">
                                        { item.etag }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sha256">
                                        { item.sha256 }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="compression_ratio">
                                        { item.compression_ratio }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="trashed_at">
                                        { dataFormatter.dateTimeFormatter(item.trashed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="purge_after">
                                        { dataFormatter.dateTimeFormatter(item.purge_after) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!storage_buckets?.storage_objects?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Storage_objects Bucket</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>ObjectKey</th>
                                
                                
                                
                                <th>ContentType</th>
                                
                                
                                
                                <th>SizeBytes</th>
                                
                                
                                
                                <th>ETag</th>
                                
                                
                                
                                <th>SHA256</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>CompressionRatio</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>TrashedAt</th>
                                
                                
                                
                                <th>PurgeAfter</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {storage_buckets.storage_objects_bucket && Array.isArray(storage_buckets.storage_objects_bucket) &&
                              storage_buckets.storage_objects_bucket.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/storage_objects/storage_objects-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="object_key">
                                        { item.object_key }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="content_type">
                                        { item.content_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="size_bytes">
                                        { item.size_bytes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="etag">
                                        { item.etag }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sha256">
                                        { item.sha256 }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="compression_ratio">
                                        { item.compression_ratio }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="trashed_at">
                                        { dataFormatter.dateTimeFormatter(item.trashed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="purge_after">
                                        { dataFormatter.dateTimeFormatter(item.purge_after) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!storage_buckets?.storage_objects_bucket?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Storage_bucketsView;